简体   繁体   English

将十六进制颜色代码转换为颜色名称(字符串)

[英]Converting Hex Color Code to Color Name (string)

I want to convert a hex color code to the suitable string color name... with the following code I was able to get the hex code of the "most used" color in a photo: 我想将十六进制颜色代码转换为合适的字符串颜色名称...使用以下代码我可以获得照片中“最常用”颜色的十六进制代码:

class ColorMath
{
    public static string getDominantColor(Bitmap bmp)
    {
        //Used for tally
        int r = 0;
        int g = 0;
        int b = 0;

        int total = 0;

        for (int x = 0; x < bmp.Width; x++)
        {
            for (int y = 0; y < bmp.Height; y++)
            {
                Color clr = bmp.GetPixel(x, y);

                r += clr.R;
                g += clr.G;
                b += clr.B;

                total++;
            }
        }

        //Calculate average
        r /= total;
        g /= total;
        b /= total;

        Color myColor = Color.FromArgb(r, g, b);
        string hex = myColor.R.ToString("X2") + myColor.G.ToString("X2") + myColor.B.ToString("X2");

        return hex;
    }
}

So I want for a hex code like: #3A322B to appear something like "dark brown" 所以我想要一个十六进制代码,如:#3A322B,看起来像“深棕色”

Assuming the colour is in the KnownColor enum you can use ToKnownColor : 假设颜色在KnownColor枚举中,您可以使用ToKnownColor

KnownColor knownColor = color.ToKnownColor();

To note is the following from the MSDN docs: 要注意以下来自MSDN文档:

When the ToKnownColor method is applied to a Color structure that is created by using the FromArgb method, ToKnownColor returns 0, even if the ARGB value matches the ARGB value of a predefined color. 当ToKnownColor方法应用于使用FromArgb方法创建的Color结构时,即使ARGB值与预定义颜色的ARGB值匹配,ToKnownColor也会返回0。

So to get your colour you could use something like the following from the hex code: 因此,要获得颜色,您可以使用十六进制代码中的以下内容:

Color color = (Color)new ColorConverter().ConvertFromString(htmlString);

Where htmlString is in the form #RRGGBB . 其中htmlString的格式为#RRGGBB

To convert KnownColor to a string simply use ToString on the enum ( see here ): 要将KnownColor转换为字符串,只需在枚举上使用ToString参见此处 ):

string name = knownColor.ToString();

Putting all of that together you can use this method: 把所有这些放在一起你可以使用这个方法:

string GetColourName(string htmlString)
{
    Color color = (Color)new ColorConverter().ConvertFromString(htmlString);
    KnownColor knownColor = color.ToKnownColor();

    string name = knownColor.ToString();
    return name.Equals("0") ? "Unknown" : name;
}

Calling it like: 把它称为:

string name = GetColourName("#00FF00");

Results in Lime . 结果在Lime

I have also found an answer to a similar question that seems to work quite well too which uses reflection and falls back to html colour names: 我也找到了一个类似问题的答案 ,似乎也很好用,它使用反射并回退到html颜色名称:

 string GetColorName(Color color) { var colorProperties = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static) .Where(p => p.PropertyType == typeof(Color)); foreach (var colorProperty in colorProperties) { var colorPropertyValue = (Color)colorProperty.GetValue(null, null); if (colorPropertyValue.R == color.R && colorPropertyValue.G == color.G && colorPropertyValue.B == color.B) { return colorPropertyValue.Name; } } //If unknown color, fallback to the hex value //(or you could return null, "Unkown" or whatever you want) return ColorTranslator.ToHtml(color); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM