Reputation: 1844
Is there any way to convert a string to hexa and then to convert it to a .Net color?
I would like to know how to convert a string of color ,say Black, to its Hexa '#000000'
?
i.e. if my input is "Black", i should return "#000000"
My issue is:
i'm setting color and storing its name in an object. So, if it is white, the object keeps "white" ,but for certain shades, it is keeping the name as f12a12 ( an example). I appended "0x" befor such strings and it worked fine with the colortranslator. In case of the normal colors in Color object, i dont want to append this. I can make the string to search through the Colors but i would like to know whether there is any other way to do this?
Upvotes: 1
Views: 519
Reputation: 1566
Color c = Color.Black;
string strColor = System.Drawing.ColorTranslator.ToHtml(c);
//returns 000000
Edit:
In reverse
Color c = System.Drawing.ColorTranslator.FromHtml("#000000");
Upvotes: 1
Reputation: 966
There is no way to get the HEX from the color name. You need to create a lookup table which holds the name of the color and also the HEX of that color. And only then you can get the HEX of that color.
For your solution I am not sure, but I think to get the correct RGB values you need to have HEX of that color.
Upvotes: 0