I need to convert some color ( in Hex ) to Brush. I need to do it in the code.
How can i do it ?
Here are 3 conversion rate formulas to use:Conversion Rate = Total number of conversions / Total number of sessions * 100. Conversion Rate = Total number of conversions / Total number of unique visitors * 100. Conversion Rate = Total number of conversions / Total number of leads * 100.
To convert a smaller unit to a larger unit (eg to ), divide it by the number of smaller units which are needed to make larger unit. To convert from a larger unit to a smaller one, multiply. To convert from a smaller unit to a larger one, divide.
//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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With