Link to home
Start Free TrialLog in
Avatar of eelou
eelou

asked on

How to translate string color name to hexadecimil value

asp.net 2.0, c#.

I am doing a survey on line.  I have a drop down list of colors, and I allow an 'other' string value text box where they can enter in the string name of a color.  For those colors in the drop down list, I will already have the hexadecimal values in the code.  When the user enters in a string value for a different color, I want to be able to translate that string to a hexadecimal value that I can then use in a Google bar chart, to actually change the color of each bar in the chart.

I guess I could take all the values in this link...http://www.w3schools.com/css/css_colornames.asp, and put key them into a table in my database, and then do a look-up on that table, but are there other ways to do this?   Can I do a call to anything anywhere that would get the hexadecimal value that I would need, or what?
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

I think this is what you're looking for:

string hexcolor = System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.Yellow);
Just forgot, if you don't have already, add a reference to System.Drawing.dll
ASKER CERTIFIED SOLUTION
Avatar of k_swapnil
k_swapnil
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In case you also want Alpha component in the color

            Color color = Color.FromName("Red");
            string hexColorValue = string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color.A, color.R, color.G, color.B);

Thx!
Swaps...
Avatar of eelou
eelou

ASKER

I made a mistake, Google charts is looking for "RRGGBB format hexadecimal number"...I forgot to include the "RRGGBB" format.
Avatar of eelou

ASKER

I got some help elsewhere. The answer is that I just needed to remove the '#' from the string.format
Avatar of eelou

ASKER

It would have been an "A", but no one took the time to answer that one further additional question