locked
Set BackGround of Grid Color in c# RRS feed

  • Question

  • Hello,

    I need to set my Grid's background by C# Code the Color is in the form shown below

    #FFA9ABAC

    How I Can Achieve it?

    I know Following code But how I can parse

    #FFA9ABAC to specified colour?

    MyGrid.BackGround = new SolidColorBrush(Colors.Red);


    Santana George

    Monday, April 29, 2013 8:13 PM

Answers

  • Hi.

    I made a small sample of code you can use to convert HEX string codes to XAML colors.

    public Color GetColorFromHex(string pHex)
            {
                string hex = pHex.Replace("#", string.Empty);
    
                int offset = 0;
                byte alpha = 255;
                byte r = 0;
                byte g = 0;
                byte b = 0;
                if (hex.Length == 8)
                {
                    alpha = (byte)Convert.ToInt32(hex.Substring(0, 2), 16);
                    offset += 2;
                }
                
                if (hex.Length >= 6)
                {
                    r = (byte)Convert.ToInt32(hex.Substring(offset, 2), 16);
                    offset += 2;
    
                    g = (byte)Convert.ToInt32(hex.Substring(offset, 2), 16);
                    offset += 2;
    
                    b = (byte)Convert.ToInt32(hex.Substring(offset, 2), 16);
                    offset += 2;
                }
                else if (hex.Length == 3)
                {
                    r = (byte)Convert.ToInt32(hex.Substring(offset, 1) + "0", 16);
                    offset += 1;
    
                    g = (byte)Convert.ToInt32(hex.Substring(offset, 1) + "0", 16);
                    offset += 1;
    
                    b = (byte)Convert.ToInt32(hex.Substring(offset, 1) + "0", 16);
                    offset += 1;
                }
    
                return ColorHelper.FromArgb(alpha, r, g, b);
            }

    It supports #aarrggbb, #rrggbb and #rgb formats and can use it like this:

    Color color = GetColorFromHex("#77A9ABAC");
    color = GetColorFromHex("#A9ABAC");
    color = GetColorFromHex("#123");
    color = GetColorFromHex("369456");

    I hope this will help you.
    Monday, April 29, 2013 10:46 PM
  • What is the specific scenario that you want this for? Where does your color actually come from?

    Given the ARGB data in your string you can use Color.FromArgb(0xFF,0xA9,0xAB,0xAC) to create your color object. If you have this in string format you would need to parse it.

    If this is an app-set color and not one chosen by the user then consider storing it in a resource rather than hard-coding it. This will make it easier to use appropriate colors for normal and for high contrast modes.

    --Rob


    Monday, April 29, 2013 8:39 PM
    Moderator

All replies

  • What is the specific scenario that you want this for? Where does your color actually come from?

    Given the ARGB data in your string you can use Color.FromArgb(0xFF,0xA9,0xAB,0xAC) to create your color object. If you have this in string format you would need to parse it.

    If this is an app-set color and not one chosen by the user then consider storing it in a resource rather than hard-coding it. This will make it easier to use appropriate colors for normal and for high contrast modes.

    --Rob


    Monday, April 29, 2013 8:39 PM
    Moderator
  • Hi.

    I made a small sample of code you can use to convert HEX string codes to XAML colors.

    public Color GetColorFromHex(string pHex)
            {
                string hex = pHex.Replace("#", string.Empty);
    
                int offset = 0;
                byte alpha = 255;
                byte r = 0;
                byte g = 0;
                byte b = 0;
                if (hex.Length == 8)
                {
                    alpha = (byte)Convert.ToInt32(hex.Substring(0, 2), 16);
                    offset += 2;
                }
                
                if (hex.Length >= 6)
                {
                    r = (byte)Convert.ToInt32(hex.Substring(offset, 2), 16);
                    offset += 2;
    
                    g = (byte)Convert.ToInt32(hex.Substring(offset, 2), 16);
                    offset += 2;
    
                    b = (byte)Convert.ToInt32(hex.Substring(offset, 2), 16);
                    offset += 2;
                }
                else if (hex.Length == 3)
                {
                    r = (byte)Convert.ToInt32(hex.Substring(offset, 1) + "0", 16);
                    offset += 1;
    
                    g = (byte)Convert.ToInt32(hex.Substring(offset, 1) + "0", 16);
                    offset += 1;
    
                    b = (byte)Convert.ToInt32(hex.Substring(offset, 1) + "0", 16);
                    offset += 1;
                }
    
                return ColorHelper.FromArgb(alpha, r, g, b);
            }

    It supports #aarrggbb, #rrggbb and #rgb formats and can use it like this:

    Color color = GetColorFromHex("#77A9ABAC");
    color = GetColorFromHex("#A9ABAC");
    color = GetColorFromHex("#123");
    color = GetColorFromHex("369456");

    I hope this will help you.
    Monday, April 29, 2013 10:46 PM
  • Awesome! Thanks.

    Santana George

    Tuesday, April 30, 2013 2:22 AM