How to generate random color names in C#

2019-01-08 08:33发布

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

标签: c# random colors
14条回答
\"骚年 ilove
2楼-- · 2019-01-08 08:57

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.

查看更多
Evening l夕情丶
3楼-- · 2019-01-08 08:57

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:

public class cExample
{
    ...
    Random randomGen = new Random();
    KnownColor[] names = (KnownColor[]) Enum.GetValues(typeof(KnownColor));
    ...
    private Color get_random_color()
    {
         KnownColor randomColorName = names[randomGen.Next(names.Length)];
         return Color.FromKnownColor(randomColorName);
    }
    ...
}
查看更多
smile是对你的礼貌
4楼-- · 2019-01-08 08:59

Here, I'm generating colors based on profile completed.

public string generateColour(Decimal percentProfileComplete )
{
    if(percent < 50)
    { 
        return "#" + (0xff0000 | Convert.ToInt32(Convert.ToDouble(percentProfileComplete ) * 5.1) * 256).ToString("X6");
    }
    return "#" + (0xff00 | (255 - Convert.ToInt32((Convert.ToDouble(percentProfileComplete ) - 50) * 5.1)) * 256 * 256).ToString("X6");
}
查看更多
趁早两清
5楼-- · 2019-01-08 09:05

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;
}
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-08 09:07

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.
查看更多
做个烂人
7楼-- · 2019-01-08 09:08

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));
查看更多
登录 后发表回答