1

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?

2
  • Just a note, #ffffff is the hex code for white. Commented Oct 21, 2011 at 8:53
  • The colors mentioned in this page would work for sure. But if you need anything else, I'm afraid as @Prashant said, you need to have your own look up table. Commented Oct 21, 2011 at 9:20

3 Answers 3

1
Color c = Color.Black;
string strColor = System.Drawing.ColorTranslator.ToHtml(c);
//returns 000000

Edit:

In reverse

Color c =  System.Drawing.ColorTranslator.FromHtml("#000000");
Sign up to request clarification or add additional context in comments.

1 Comment

how can i do this in reverse? i.e. to give 000000 and return black
0

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.

Comments

0
  ColorTranslator.FromHtml(  "#ffffff")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.