Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert "#00E4FF" to Brush in code?

Tags:

silverlight

I need to convert some color ( in Hex ) to Brush. I need to do it in the code.

How can i do it ?

like image 508
Yanshof Avatar asked Jun 02 '11 06:06

Yanshof


People also ask

What is the formula convert?

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.

How do we convert units of measurement?

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.


2 Answers

//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));
like image 148
Jordan Avatar answered Sep 28 '22 09:09

Jordan


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.

like image 41
Gone Coding Avatar answered Sep 28 '22 08:09

Gone Coding