locked
how to add HEX color by code RRS feed

  • Question

  • hi

    i want to add HEX color by code in metro application

    i can do that by using brushconver, but i cannot find any thing like that in metro.

    any one knows how

    thank you

    Friday, March 9, 2012 5:47 AM

Answers

  • You can create a Color from its numeric values with Color.FromArgb() :

    LayoutRoot.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));

    --Rob
    Friday, March 9, 2012 6:31 AM
    Moderator
  • If you're looking for a valueconverter that converts a hexadecimal string into a SolidColorBrush and vice-versa, then try this one:

    namespace YourNamespaceHere
    {
        using System;
        using Windows.UI;
        using Windows.UI.Xaml.Data;
        using Windows.UI.Xaml.Media;
    
        public class HexToColorConverter : IValueConverter
        {
            /// <summary>
            /// Converts a hexadecimal string value into a Brush.
            /// </summary>
            public object Convert(object value, Type targetType, object parameter, string language)
            {   
                byte alpha;
                byte pos = 0;
    
                string hex = value.ToString().Replace("#", "");
    
                if (hex.Length == 8)
                {
                    alpha = System.Convert.ToByte(hex.Substring(pos, 2), 16);
                    pos = 2;
                }
                else 
                {
                    alpha = System.Convert.ToByte("ff", 16);
                }
    
                byte red = System.Convert.ToByte(hex.Substring(pos, 2), 16);
                
                pos += 2;
                byte green = System.Convert.ToByte(hex.Substring(pos, 2), 16);
    
                pos += 2;
                byte blue = System.Convert.ToByte(hex.Substring(pos, 2), 16);
    
                return new SolidColorBrush(Color.FromArgb(alpha, red, green, blue));
            }
    
            /// <summary>
            /// And back again.
            /// </summary>
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                SolidColorBrush val = value as SolidColorBrush;
                return "#" + val.Color.A.ToString() + val.Color.R.ToString() + val.Color.G.ToString() + val.Color.B.ToString();
            }
        }
    }

    It only works for hexadecimal values. It does not know what to do with a string such as 'LightBlue'.
    • Marked as answer by John83 Zheng Sunday, March 11, 2012 2:34 AM
    Friday, March 9, 2012 6:54 PM

All replies

  • You can create a Color from its numeric values with Color.FromArgb() :

    LayoutRoot.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));

    --Rob
    Friday, March 9, 2012 6:31 AM
    Moderator
  • If you're looking for a valueconverter that converts a hexadecimal string into a SolidColorBrush and vice-versa, then try this one:

    namespace YourNamespaceHere
    {
        using System;
        using Windows.UI;
        using Windows.UI.Xaml.Data;
        using Windows.UI.Xaml.Media;
    
        public class HexToColorConverter : IValueConverter
        {
            /// <summary>
            /// Converts a hexadecimal string value into a Brush.
            /// </summary>
            public object Convert(object value, Type targetType, object parameter, string language)
            {   
                byte alpha;
                byte pos = 0;
    
                string hex = value.ToString().Replace("#", "");
    
                if (hex.Length == 8)
                {
                    alpha = System.Convert.ToByte(hex.Substring(pos, 2), 16);
                    pos = 2;
                }
                else 
                {
                    alpha = System.Convert.ToByte("ff", 16);
                }
    
                byte red = System.Convert.ToByte(hex.Substring(pos, 2), 16);
                
                pos += 2;
                byte green = System.Convert.ToByte(hex.Substring(pos, 2), 16);
    
                pos += 2;
                byte blue = System.Convert.ToByte(hex.Substring(pos, 2), 16);
    
                return new SolidColorBrush(Color.FromArgb(alpha, red, green, blue));
            }
    
            /// <summary>
            /// And back again.
            /// </summary>
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                SolidColorBrush val = value as SolidColorBrush;
                return "#" + val.Color.A.ToString() + val.Color.R.ToString() + val.Color.G.ToString() + val.Color.B.ToString();
            }
        }
    }

    It only works for hexadecimal values. It does not know what to do with a string such as 'LightBlue'.
    • Marked as answer by John83 Zheng Sunday, March 11, 2012 2:34 AM
    Friday, March 9, 2012 6:54 PM