Convert String value To Color : Color « 2D Graphics « C# / C Sharp






Convert String value To Color

  

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Media;

namespace ApuntaNotas.Utilities
{
    /// <summary>
    /// This a helper Extension Method that help us transforming a string like #ffffff to a Color instance
    /// </summary>
    public static class StringToColorExtensionMethod
    {
        /// <summary>
        /// The EM itself that does the job
        /// </summary>
        /// <param name="colorString">The color string.</param>
        /// <returns></returns>
        public static Color ToColor(this string colorString)
        {
            colorString = ExtractHexDigits(colorString);

            Color color = Colors.White;

            if (colorString.Length == 6)
            {
                var r = colorString.Substring(0, 2);
                var g = colorString.Substring(2, 2);
                var b = colorString.Substring(4, 2);

                try
                {
                    byte rc = Byte.Parse(r, NumberStyles.HexNumber);
                    byte gc = Byte.Parse(g, NumberStyles.HexNumber);
                    byte bc = Byte.Parse(b, NumberStyles.HexNumber);
                    color = Color.FromRgb(rc, gc, bc);
                }
                catch (Exception)
                {
                    return Colors.White;
                    throw;
                }
            }
            if (colorString.Length == 8)
            {
                var a = colorString.Substring(0, 2);
                var r = colorString.Substring(2, 2);
                var g = colorString.Substring(4, 2);
                var b = colorString.Substring(6, 2);

                try
                {
                    byte ac = Byte.Parse(a, NumberStyles.HexNumber);
                    byte rc = Byte.Parse(r, NumberStyles.HexNumber);
                    byte gc = Byte.Parse(g, NumberStyles.HexNumber);
                    byte bc = Byte.Parse(b, NumberStyles.HexNumber);
                    color = Color.FromArgb(ac, rc, gc, bc);
                }
                catch (Exception)
                {
                    return Colors.White;
                    throw;
                }
            }
            return color;
        }

        /// <summary>
        /// Extracts the hex digits from the string.
        /// </summary>
        /// <param name="colorString">The color string.</param>
        /// <returns></returns>
        private static string ExtractHexDigits(string colorString)
        {
            Regex HexDigits = new Regex(@"[abcdefABCDEF\d]+", RegexOptions.Compiled);

            var hexnum = new StringBuilder();
            foreach (char c in colorString)
                if (HexDigits.IsMatch(c.ToString()))
                    hexnum.Append(c.ToString());

            return hexnum.ToString();
        }
    }
}

   
    
  








Related examples in the same category

1.Transparent colorTransparent color
2.List all known color in a systemList all known color in a system
3.Draw each of 100 cells with randomly chosen colorsDraw each of 100 cells with randomly chosen colors
4.Filled with the semi transparent and transparent colorFilled with the semi transparent and transparent color
5.All the colors that are supported in C# according
6.Color ChangerColor Changer
7.Known ColorsKnown Colors
8.Five yellow squares with different alpha values(Transparensy)
9.Create two color instances with different alpha components
10.Color.Chocolate
11.Use Color.FromArgb to create Color
12.Get all known color
13.Color representation in r,g,b with values [0.0 - 1.0].
14.Color representation in h,s,v with h = [0 - 360], s,v = [0.0 - 1.0].
15.Return a randomly-generated color
16.Color to RGB value
17.Get color outof String
18.Parse Color
19.Color To Rgb
20.Rgb To Color
21.Returns an HTML #XXXXXX format for a color.
22.Convert color name to Color object
23.Hex Color Util
24.Helper method to get a color based on it's string value
25.Converts a color string to a hex value string
26.Color to Hue,Lightness,Saturation value
27.Get Color From String
28.Color to String
29.Masks an image and returns a two color bitmap.
30.Convert Hex To Color
31.Create Color From HLS value
32.Parse Color with Color.FromArgb
33.Create Color Object from RGB value
34.Get Rainbow Colors
35.Convert RGB to HSL
36.HSL to RGB conversion.
37.Hsv To Rgb
38.Rgb Linear Interpolate
39.Get Luminance
40.To Grey scale
41.Heat Map background colour calculations