Found solution: Using Hex colors in style config files

Topics: Developer Forum
Oct 29, 2009 at 1:33 PM

I was creating my own coloring file where I needed to put hex colors in format (FFFFFF). When I was testing it, an exception was thrown - Configuration.cs, line 1909 (color conversion function). I figured out that this function didn't support hex colors. So I googled around and came up with this:

private Color getColor(string s)
			Regex hexFilter = new Regex("^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
 			if (hexFilter.IsMatch(s))
				return HexStringToColor(s);
				return (Color)new ColorConverter().ConvertFromString(s);

		protected Color HexStringToColor (string hex)
			hex = hex.Replace("#", "");
			if (hex.Length != 6)
				throw new Exception(hex +
					" is not a valid 6-place hexadecimal color code.");
			string r, g, b;

			r = hex.Substring(0, 2);
			g = hex.Substring(2, 2);
			b = hex.Substring(4, 2);

			return System.Drawing.Color.FromArgb(HexStringToBase10Int(r),

		protected int HexStringToBase10Int(string hex)
			int base10value = 0;

			try   { base10value = System.Convert.ToInt32(hex, 16); }
			catch { base10value = 0; }

			return base10value;
I suggest integrating this code with next release. If you want to handle colors like this one (#000000), try modifying regular expression to this:

Regex hexFilter = new Regex("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
I hope it will help!

Oct 29, 2009 at 1:45 PM

For me a Tag like

<Style Name="WORD" ForeColor="#C0BED1"/>

works perfect.

Oct 29, 2009 at 5:13 PM

Yeah ColorConverter is what ASP.NET uses to parse .aspx so it should work for you. Are you missing the # in your config?


Oct 30, 2009 at 11:51 AM

Yeah, that's very likely. I just converted Notepad++ generated file, and it doesn't use #. Anyway, I think my code will do some sort of contribution.