Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate random dark colors in C#?

Tags:

c#

colors

I generate a random color this way:

 var random = new Random();
 var color = String.Format("#{0:X6}", random.Next(0x1000000)); 

How can I exclude colors brighter than a certain value?

like image 722
gormit Avatar asked Jun 18 '11 13:06

gormit


People also ask

How do you generate random colors in C++?

call srand() function with argument 'time(0)' to make sure the system generates a unique random number every time rand() is called. create a function rgb_color_code() that stores random integers between 0 to 255 in the array created in the first step. This is done using the rand() function and modulus operator.


2 Answers

An quite simple way to get rid of the "upper half" of brightes colors is to mask the result via

random.Next(0x1000000) & 0x7F7F7F
like image 160
Howard Avatar answered Oct 01 '22 16:10

Howard


One way to do this is to generate colours in the HSV/HSL colour-space, and then convert to RGB (the Wikipedia article tells you how to do that).

The advantage of HSV is that one of the components (V) corresponds to "brightness". So if you generate H, S and V independently and randomly, but restrict V to low values, then you will get dark colours.

like image 38
Oliver Charlesworth Avatar answered Oct 01 '22 18:10

Oliver Charlesworth