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?
eelouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
I think this is what you're looking for:

string hexcolor = System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.Yellow);
0
Alexandre SimõesManager / Technology SpecialistCommented:
Just forgot, if you don't have already, add a reference to System.Drawing.dll
0
k_swapnilCommented:
Do you mean the user can enter the color name in the text box and then you have to convert that color to its corresponding hex code?
If I am getting you correctly... foloow the below mentioned code:

Assuming txtColor is the text box in which user can enter color string
            Color color = Color.FromName(txtColor.Text);
            string hexColorValue = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);

in case the user enters an invalid color then by default it will return black i.e #000000

Thx!
Swaps...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

k_swapnilCommented:
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...
0
eelouAuthor Commented:
I made a mistake, Google charts is looking for "RRGGBB format hexadecimal number"...I forgot to include the "RRGGBB" format.
0
eelouAuthor Commented:
I got some help elsewhere. The answer is that I just needed to remove the '#' from the string.format
0
eelouAuthor Commented:
It would have been an "A", but no one took the time to answer that one further additional question
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.