I need to generate random color names e.g. "Red", "White" etc. How can I do it? I am able to generate random color like this:
Random randonGen = new Random();
Color randomColor = Color.FromArgb(randonGen.Next(255), randonGen.Next(255),
randonGen.Next(255));
but I need the names and not all colors generated like this have a known name.
Thanks
I would build a lookup table. Especially since some colors are up to personal interpretation.
Go through each color value in the Color struct ( http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx ) and map it to the RGB values. Then to convert back, lookup the RGB value to see if it has a named color.
I would have commented on Pih's answer; however, not enough karma. Anywho, I tried implementing this and ran into the issue of the same color being generated from multiple calls as the code was called repeatedly in quick succession (i.e. the randomGen was the same and since it is based on the clock = same results ensued).
Try this instead:
Here, I'm generating colors based on profile completed.
Copied code from Retrieve a list of colors in C#
CODE:
Take a random value and get from KnownColor enum.
May be by this way:
To clear up the syntax errors in the original question, it's
Color.FromRgb, and
(Byte)random2.Next(255)
converts the integer to a byte value needed by Color: