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
Random r = new Random(); BackColor = Color. FromArgb(r. Next(0, 256), 0, 0);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With