Coder Perfect

Using.NET, how can I extract the color from a hexadecimal color code?


What color can I get from a hexadecimal color code (for example, #FFDFD991)?

I’m obtaining a hexadecimal color code from a file I’m reading. I need to make the appropriate System. Windows. Media. The hexadecimal color code has a color instance. Is there a built-in technique to do this in the framework?

Asked by viky

Solution #1

That’s probably an ARGB code… Are you talking about System? Drawing. System or color. Windows. Media.Color? WPF, for example, makes advantage of the latter. I haven’t seen anyone mention it yet, so in case you’re looking for it, here it is:

using System.Windows.Media;

Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");

Answered by Thorarin

Solution #2

Use the ColorTranslator class if you mean the HTML type RGB codes (also known as Hex codes, such as #FFCC66).

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

If you’re working with an ARGB hex code, though, the ColorConverter class from the System can help. Windows. Namespace for media:

Color col = ColorConverter.ConvertFromString("#FFDFD991") as Color;
//or      = (Color) ColorConverter.ConvertFromString("#FFCC66") ;

Answered by Oded

Solution #3

You can do it yourself if you don’t want to utilize the ColorTranslator:

string colorcode = "#FFFFFF00";
int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
Color clr = Color.FromArgb(argb);

The colorcode is simply the ARGB value’s hexadecimal equivalent.


You can use this (combining many comments) if you need to use four values instead of a single integer:

string colorcode = "#FFFFFF00";    
colorcode = colorcode.TrimStart('#');

Color col; // from System.Drawing or System.Windows.Media
if (colorcode.Length == 6)
    col = Color.FromArgb(255, // hardcoded opaque
                int.Parse(colorcode.Substring(0,2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(2,2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(4,2), NumberStyles.HexNumber));
else // assuming length of 8
    col = Color.FromArgb(
                int.Parse(colorcode.Substring(0, 2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(2, 2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(4, 2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(6, 2), NumberStyles.HexNumber));

NumberStyles is located in System.Globalization. Note 2: Please check for errors on your own (colorcode should be a hexadecimal value of either 6 or 8 characters)

Answered by Hans Kesting

Solution #4

The three options below all produce the same color. The last one has the advantage of being highlighted with right color in the Visual Studio 2010 IDE (perhaps due to ReSharper).

var cc1 = System.Drawing.ColorTranslator.FromHtml("#479DEE");

var cc2 = System.Drawing.Color.FromArgb(0x479DEE);

var cc3 = System.Drawing.Color.FromArgb(0x47, 0x9D, 0xEE);

Answered by demp

Solution #5

There’s also this handy approach for adding extensions:

static class ExtensionMethods
    public static Color ToColor(this uint argb)
        return Color.FromArgb((byte)((argb & -16777216)>> 0x18),      
                              (byte)((argb & 0xff0000)>> 0x10),   
                              (byte)((argb & 0xff00) >> 8),
                              (byte)(argb & 0xff));

In use:

Color color = 0xFFDFD991.ToColor();

Answered by Jink

Post is based on