I am trying to convert a hex value to its corresponding color name, but the ColorConverter’s ConvertFromString method is inserting extra characters:
string s1 = "#000000";
string s2 = "#ccff33";
string s3 = "#990000";
Color color;
ColorConverter converter = new ColorConverter();
color = (Color)converter.ConvertFromString(s1);
Console.Write(color.Name); // Outputs "Black"
color = (Color)converter.ConvertFromString(s2);
Console.Write(color.Name); // Outputs "ffccff33";
color = (Color)converter.ConvertFromString(s3);
Console.Write(color.Name); // Outputs "ff990000";
The extra “ff” inexplicably happens for every color but Black and White. Does anyone have any idea why this is happening?
We have similar functionality in one of our projects, you could use the following