Convertir des couleurs Hexa et des ColorName en SolidColorBrush

Convertir des couleurs Hexa et des ColorName en SolidColorBrush

Pour ma nouvelle applis, je recevais des couleur stocké en string.
Celle ci étaient ou en format Hexadécimale ou en format ColorName.

J’ai donc du créer un petit helper pour convertir les strings en SolidColorBrush.

Attention si vous bindé directement la string à votre UI pas besoin de convertir XAML le fait tout seul.

Pour retrouvé une couleur à partir de son nom j’ai du utiliser la réflexion sur le type Colors.

Je vous le partage donc :

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);
        }
    }
}

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Pin It on Pinterest