Converting a Hexadecimal Color to a System.Drawing.Color Object

Tags tagged as   Code: C#
How to convert a web-style hex color (#FFFFFF) to a Color struct instance in C#.

Summary

As part of a larger project, I needed to convert a hexadecimal color string like those used in webpages (#000000, #FFFFFF, #FF0000, etc.) to a .NET System.Drawing.Color object that represented that color. In my case, I am parsing elements out of a webpage, and then drawing GIF images based on the parsed data, so I need to use the System.Drawing color representation for the colors, but my inputs are input in hex.

The easiest way to do this is to use the System.Drawing.ColorTranslator class, which provides static methods for many common color conversions. Note that with this method, you must include the leading '#' sign, or the conversion will produce a runtime error. Additionally, this method can accept color names instead of hexadecimal. Note that the MSDN documentation claims that this method can only translate color names, and not hex strings, although hex conversions do work.

1 System.Drawing.Color myOrangeColor 2 = System.Drawing.ColorTranslator.FromHtml("#CE560C");

You can also translate colors without the ColorTranslator class (which may be useful if you are using the Compact Framework).

System.Drawing.Color only supports creating custom colors using it's FromArgb method, which requires base-10 inputs. Therefore, two conversion functions are needed - a conversion from decimal to hexadecimal, and a conversion from a 6 character string to three numeric parts (one each for red, green, and blue).

Articles and downloads sponsored by:
Thanks! Amazon commissions help me pay for textbooks.

Source Code

The first function handles the safe conversion of a hexadecimal (base-16) number to decimal (base-10).

1 protected int HexStringToBase10Int(string hex) 2 { 3 int base10value = 0; 4 5 try { base10value = System.Convert.ToInt32(hex, 16); } 6 catch { base10value = 0; } 7 8 return base10value; 9 }

The next function breaks the hexadecimal string into parts, removes a leading # (so you can pass it #FF00FF or FF00FF with the same result), and then converts the hexadecimal parts into decimal, and creates a color using the red, green, and blue value integers.

1 protected System.Drawing.Color HexStringToColor(string hex) 2 { 3 hex = hex.Replace("#", ""); 4 5 if (hex.Length != 6) 6 throw new Exception(hex + 7 " is not a valid 6-place hexadecimal color code."); 8 9 string r, g, b; 10 11 r = hex.Substring(0, 2); 12 g = hex.Substring(2, 2); 13 b = hex.Substring(4, 2); 14 15 return System.Drawing.Color.FromArgb(HexStringToBase10Int(r), 16 HexStringToBase10Int(g), 17 HexStringToBase10Int(b)); 18 }

To use the hex-string to color-object converter, you just need to pass the hex color string to HexStringToColor, like this:

1 System.Drawing.Color myOrangeColor = HexStringToColor("#CE560C");

Comments & Feedback


Anonymous says:
Monday, July 30, 2007 @ 1:06 AM
adfas fdasf
Amit says:
Monday, May 12, 2008 @ 1:38 AM
Good Artical
Adam says:
Tuesday, May 13, 2008 @ 8:03 AM
Thanks, Amit!
Leave this field blank:
Comment on this Entry
This work is licensed under a Creative Commons Attribution 3.0 United States License.
Please link to this article in your source code comments if you use this content.

Article Info

Posted January 23, 2007
Viewed 6211 times

User Rating:

Share

Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati
Coffee Counter
Current Coffee:
 Peet's Malawi Songwe River

Current Count:
Akxl Coffee Meter

Create Your Own »

The Real-Time Coffee Meter is a free Website App from Akxl Labs. Text-only and badge versions available.