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
Or you could try out this: For .NET 4.5
public Windows.UI.Color GetRandomColor()
{
Random randonGen = new Random();
Windows.UI.Color randomColor =
Windows.UI.Color.FromArgb(
(byte)randonGen.Next(255),
(byte)randonGen.Next(255),
(byte)randonGen.Next(255),
(byte)randonGen.Next(255));
return randomColor;
}
private string getRandColor()
{
Random rnd = new Random();
string hexOutput = String.Format("{0:X}", rnd.Next(0, 0xFFFFFF));
while (hexOutput.Length < 6)
hexOutput = "0" + hexOutput;
return "#" + hexOutput;
}
generate a random number and cast it to KnownColor Type
((KnownColor)Random.Next());
Sounds like you just need a random color from the KnownColor enumeration.
If you wanted to get random color from specific colors list,
then try below one
Color[] colors = new[] { Color.Red, Color.Blue, Color.FromArgb(128, 128, 255) }; //Add your favorite colors list
Random randonGen = new Random();
Color randomColor = colors[Generator.Next(0, colors.Length)]; //It will choose random color from "colors" array
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:
Random random2 = new Random();
public int nn = 0x128;
public int ff = 0x512;
Color randomColor = Color.FromRgb((Byte)random2.Next(nn, ff), (Byte)random2.Next(nn, ff), (Byte)random2.Next(nn, ff));