-2

I need to get the corresponding color name based on the color's hexadecimal palette code.

I tried:

 brush = (new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(dialog.Color.A, dialog.Color.R, dialog.Color.G, dialog.Color.B)));

 TheColor = Convert.ToString(brush);  //here i get the hexadecimal only

eg. Red ,cyan,blue things like that.

Chelseawillrecover
  • 2,596
  • 1
  • 31
  • 51
Anoushka Seechurn
  • 2,166
  • 7
  • 35
  • 52
  • what if that hex value does not have any kind of name? How would you define *RED*? Your definition of red could be very different from mine. – Liam Dec 16 '13 at 11:45
  • 2
    this may help.. http://stackoverflow.com/questions/2109756/how-to-get-color-from-hexadecimal-color-code-using-net?rq=1 – Ric Dec 16 '13 at 11:46
  • 1
    You can make millions of colors with RGB, there's no way the computer knows what you call every color. – Tobberoth Dec 16 '13 at 11:47
  • This [answer](http://stackoverflow.com/a/7791803/122005) may also be of help. – chridam Dec 16 '13 at 11:47

1 Answers1

4

In .NET there is the concept of a "known color".

public string ColorName(Color toCheck)
{
    string result = "";
    foreach (KnownColor known in Enum.GetValues(typeof(KnownColor)))
    {
            Color c = Color.FromKnownColor(kc);
            if (toCheck.ToArgb() == known.ToArgb())
            {
                result = known.Name;
            }
    }

    return result;
}

Obviously this cannot recognize any color that you know, only those that come predefined in the .NET framework.

Otherwise you'll have to write your own recognizer, which should be pretty easy to do, using a Dictionary<string, string> for instance, where the key would be the RGB value and the value would be the name.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • some of this would be dependant on the source of the RGB values. I don't think these would tie up with, for example, the HTML definition of *"Aqua"*. Not criticising what is an interesting answer, just pointing out that *".Net colours"* will not match all definitions. – Liam Dec 16 '13 at 11:56