Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the color from a hexadecimal color code using .NET?

Tags:

c#

hex

colors

wpf

How can I get a color from a hexadecimal color code (e.g. #FFDFD991)?

I am reading a file and am getting a hexadecimal color code. I need to create the corresponding System.Windows.Media.Color instance for the hexadecimal color code. Is there an inbuilt method in the framework to do this?

like image 284
viky Avatar asked Jan 21 '10 14:01

viky


People also ask

How do you convert hexadecimal to color?

Hex color codes start with a pound sign or hashtag (#) and are followed by six letters and/or numbers. The first two letters/numbers refer to red, the next two refer to green, and the last two refer to blue. The color values are defined in values between 00 and FF (instead of from 0 to 255 in RGB).

How do I find the color code from RGB values?

Click on the color selector icon (the eyedropper), and then click on the color of in- terest to select it, then click on 'edit color'. 3. The RGB values for that color will appear in a dialogue box.

Can you use hexadecimal colors in HTML?

In HTML, colors can also be specified using RGB values, HEX values, HSL values, RGBA values, and HSLA values.


2 Answers

I'm assuming that's an ARGB code... Are you referring to System.Drawing.Color or System.Windows.Media.Color? The latter is used in WPF for example. I haven't seen anyone mention it yet, so just in case you were looking for it:

using System.Windows.Media;  Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991"); 
like image 197
Thorarin Avatar answered Oct 10 '22 03:10

Thorarin


Assuming you mean the HTML type RGB codes (called Hex codes, such as #FFCC66), use the ColorTranslator class:

System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#FFCC66"); 

If, however you are using an ARGB hex code, you can use the ColorConverter class from the System.Windows.Media namespace:

Color col = ColorConverter.ConvertFromString("#FFDFD991") as Color; //or      = (Color) ColorConverter.ConvertFromString("#FFCC66") ; 
like image 38
Oded Avatar answered Oct 10 '22 04:10

Oded