• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

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?
0
eelou
Asked:
eelou
  • 3
  • 2
  • 2
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now