For my new apps, I was given stringed colors.

These were either Hexadecimal or ColorName.

So I had to create a little helper to convert the strings to SolidColorBrush.

Be careful if you tie the string directly to your UI no need to convert XAML does it alone.

To recover a color from its name I had to use the reflection on the Colors type.

So I share it with you:


using System.Reflection;
using Windows.UI;
using Windows.UI.Xaml.Media;
namespace AngryCat.MVVMToolkit
{
    ///<summary>
    /// Helper permettant de convertir des couleur string en SolidColorBrush
    /// </summary>
    public static class StringColorToSolidBrushHelper
    {
        ///<summary>
        /// Obtient un SolidColorBrush à partir d'une couleur string
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static SolidColorBrush GetSolidColorBrush(string color)
        {
            if (string.IsNullOrEmpty(color))
                return null;
            SolidColorBrush result = FromHex(color);
            if (result == null)
                result = FromName(color);
            return result;
        }

        ///<summary>
        /// Obtient un SolidColorBrush à partir d'une couleur string
        /// </summary>
        /// <param name="colorName">Nom de la couleur ex: White, Red, Pink</param>
        /// <returns>Retourne une brush. Si la couleur n'est pas un nom valide retourne null</returns>
        public static SolidColorBrush FromName(string colorName)
        {
            if (string.IsNullOrEmpty(colorName))
                return null;
            if (hexColor[0] == '#')
                return null;
            var colorProperty = typeof(Colors).GetRuntimeProperty(colorName);

            if (colorProperty != null)
                return new SolidColorBrush((Color)colorProperty.GetValue(null));
            else
                return null;
        }

        ///<summary>
        /// Convertit un code couleur heaxadecimal en SolidColorBrush
        /// </summary>
        /// <param name="hexColor">code hexa d'une couleur exemple #FFFFFF</param>
        /// <returns>Retourne une brush. Si la couleur n'est pas un code hexadecimal valide retourne null</returns>
        public static SolidColorBrush FromHex(string hexColor)
        {
            if(sting.IsNullOrEmpty(hexColor))
                return null;
            if (hexColor[0] != '#')
                return null;
            byte a, r, g, b;
            if (hexColor.Length == 7)
            {
                a = 255;
                r = ToByte(hexColor.Substring(1, 2));
                g = ToByte(hexColor.Substring(3, 2));
                b = ToByte(hexColor.Substring(5, 2));
                return new SolidColorBrush(Color.FromArgb(a, r, g, b));
            }

            else if (hexColor.Length == 9)
            {
                a = ToByte(hexColor.Substring(1, 2));
                r = ToByte(hexColor.Substring(3, 2));
                g = ToByte(hexColor.Substring(5, 2));
                b = ToByte(hexColor.Substring(7, 2));
                return new SolidColorBrush(Color.FromArgb(a, r, g, b));
            }
            else
            {
                return null;
            }
        }
        ///<summary>
        /// Convertit une couleur hexadecimal en Alpha, Red, Blue, Green
        /// </summary>
        /// <param name="colorCodePart">Hexadecimal color code channel (Alpha, Red, Blue, Green)</param>
        /// <returns>byte value parsed from Hexadecimal color code channel</returns>
        static byte ToByte(string colorCodePart)
        {
            return byte.Parse(colorCodePart, System.Globalization.NumberStyles.HexNumber);
        }
    }
}