Reputation: 1492
Whenever I run this, and open the color dialog, there are many colors that do not having a proper name, the listbox will show something like "ffff8000"(Orange-Yellow). Is there another way of pushing the proper name? Is there a proper Color Name library I can reference in code?
colorDialog1.ShowDialog();
cl.Add(colorDialog1.Color.Name);
listBox1.Items.AddRange(cl.ToArray());
Upvotes: 1
Views: 1281
Reputation: 941545
The .NET framework defines the KnownColor enum, you could use it to convert a color value to a name. It won't be a complete solution, it doesn't have "Orange Yellow". But many of the common colors are present. For example:
public static Color LookupKnownColor(uint c) {
int crgb = (int)(c & 0xffffff);
foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor))) {
Color map = Color.FromKnownColor(kc);
if (!map.IsSystemColor) {
if ((map.ToArgb() & 0xffffff) == crgb)
return map;
}
}
return Color.FromArgb(unchecked((int)(c | 0xff000000)));
}
Usage:
Color c = LookupKnownColor(0xffffff00);
Console.WriteLine(c.Name);
Output: Yellow
Upvotes: 3