user7140850
user7140850

Reputation:

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"

Upvotes: 5

Views: 5781

Answers (1)

TheLethalCoder
TheLethalCoder

Reputation: 6744

Assuming the colour is in the KnownColor enum you can use ToKnownColor:

KnownColor knownColor = color.ToKnownColor();

To note is the following from the MSDN docs:

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.

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.

To convert KnownColor to a string simply use ToString on the enum (see here):

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.

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:

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);
}

Upvotes: 3

Related Questions