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 Ran
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.
Ignore the fact that you're after colors - you really just want a list of possible values, and then take a random value from that list.
The only tricky bit then is working out which set of colors you're after. As Pih mentioned, there's KnownColor - or you could find out all the public static properties of type Color
within the Color structure, and get their names. It depends on what you're trying to do.
Note that randomness itself can be a little bit awkward - if you're selecting multiple random colors, you probably want to use a single instance of Random`. Unfortunately it's not thread-safe, which makes things potentially even more complicated. See my article on randomness for more information.
Copied code from Retrieve a list of colors in C#
CODE:
private List<string> GetColors()
{
//create a generic list of strings
List<string> colors = new List<string>();
//get the color names from the Known color enum
string[] colorNames = Enum.GetNames(typeof(KnownColor));
//iterate thru each string in the colorNames array
foreach (string colorName in colorNames)
{
//cast the colorName into a KnownColor
KnownColor knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), colorName);
//check if the knownColor variable is a System color
if (knownColor > KnownColor.Transparent)
{
//add it to our list
colors.Add(colorName);
}
}
//return the color list
return colors;
}
Use Enum.GetValue
to retrieve the values of the KnownColor
enumeration and get a random value:
Random randomGen = new Random();
KnownColor[] names = (KnownColor[]) Enum.GetValues(typeof(KnownColor));
KnownColor randomColorName = names[randomGen.Next(names.Length)];
Color randomColor = Color.FromKnownColor(randomColorName);
Take a random value and get from KnownColor enum.
May be by this way:
System.Array colorsArray = Enum.GetValues(typeof(KnownColor));
KnownColor[] allColors = new KnownColor[colorsArray.Length];
Array.Copy(colorsArray, allColors, colorsArray.Length);
// get a randon position from the allColors and print its name.
Put the colors into an array and then choose a random index:
class RandomColorSelector
{
static readonly Color[] Colors =
typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static)
.Select(propInfo => propInfo.GetValue(null, null))
.Cast<Color>()
.ToArray();
static readonly string[] ColorNames =
typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static)
.Select(propInfo => propInfo.Name)
.ToArray();
private Random rand = new Random();
static void Main(string[] args)
{
var colorSelector = new RandomColorSelector();
var color = colorSelector.GetRandomColor();
// in case you are only after the *name*
var colorName = colorSelector.GetRandomColorName();
}
public Color GetRandomColor()
{
return Colors[rand.Next(0, Colors.Length)];
}
public string GetRandomColorName()
{
return ColorNames[rand.Next(0, Colors.Length)];
}
}
Note that the sample above simply looks up all static properties of the Color
type. You might want to improve this by checking that the actual return type of the property is a Color
.