Why does Color.IsNamedColor not work when I create a color using Color.FromArgb()?
- by Jon B
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?