Why does Color.IsNamedColor not work when I create a color using Color.FromArgb()?

Posted by Jon B on Stack Overflow See other posts from Stack Overflow or by Jon B
Published on 2010-01-09T04:57:03Z Indexed on 2010/04/17 22:53 UTC
Read the original article Hit count: 285

Filed under:
|
|
|

In my app I allow the user to build a color, and then show him the name or value of the color later on. If the user picks red (full red, not red-ish), I want to show him "red". If he picks some strange color, then the hex value would be just fine. Here's sample code that demonstrates the problem:

static string GetName(int r, int g, int b)
{
    Color c = Color.FromArgb(r, g, b);  // Note that specifying a = 255 doesn't make a difference
    if (c.IsNamedColor)
    {
        return c.Name;
    }
    else
    {
        // return hex value
    }
}

Even with very obvious colors like red IsNamedColor never returns true. Looking at the ARGB values for my color and Color.Red, I see no difference. However, calling Color.Red.GetHashCode() returns a different hash code than Color.FromArgb(255, 0, 0).GetHashCode().

How can I create a color using user specified RGB values and have the Name property come out right?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET