How to convert "#00E4FF" to Brush in code?
I need t开发者_Python百科o convert some color ( in Hex ) to Brush. I need to do it in the code.
How can i do it ?
//this would be initialized somewhere else, I assume
string hex = "#00E4FF";
//strip out any # if they exist
hex = hex.Replace("#", string.Empty);
byte r = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
byte g = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
byte b = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
SolidColorBrush myBrush = new SolidColorBrush(Color.FromArgb(255, r, g, b));
Enhanced version (includes all colour formats);
I needed one of these that also works with the 3 digit "Shorthand hexadecimal form" and the MS alpha channel versions (for Silverlight/WPF), so came up with this version to cover all numeric colour formats:
/// <summary>
/// Convert a Hex color string to a Color object
/// </summary>
/// <param name="htmlColor">Color string in #rgb, #argb, #rrggbb or #aarrggbb format</param>
/// <returns>A color object</returns>
public static Color ColorFromString(string htmlColor)
{
htmlColor = htmlColor.Replace("#", "");
byte a = 0xff, r = 0, g = 0, b = 0;
switch (htmlColor.Length)
{
case 3:
r = byte.Parse(htmlColor.Substring(0, 1), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(htmlColor.Substring(1, 1), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(htmlColor.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
break;
case 4:
a = byte.Parse(htmlColor.Substring(0, 1), System.Globalization.NumberStyles.HexNumber);
r = byte.Parse(htmlColor.Substring(1, 1), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(htmlColor.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(htmlColor.Substring(3, 1), System.Globalization.NumberStyles.HexNumber);
break;
case 6:
r = byte.Parse(htmlColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(htmlColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(htmlColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
break;
case 8:
a = byte.Parse(htmlColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
r = byte.Parse(htmlColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(htmlColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(htmlColor.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
break;
}
return Color.FromArgb(a, r, g, b);
}
For a brush you use it like this:
return new SolidColorBrush(ColorFromString(colorString));
Using byte.Parse is more efficient than Convert and requires no casting.
Update: Fixed sub-string offsets for case 8.
public static SolidColorBrush GetColorFromHexa(string hexaColor)
{
return new SolidColorBrush(Color.FromArgb(
Convert.ToByte(hexaColor.Substring(1, 2), 16),
Convert.ToByte(hexaColor.Substring(3, 2), 16),
Convert.ToByte(hexaColor.Substring(5, 2), 16),
Convert.ToByte(hexaColor.Substring(7, 2), 16)
)
);
}
Hope this helps
精彩评论