Convert Hex to RGB in C#

The .NET System.Globalizaton’s NumberStyles has a parameter called AllowHexSpecifier that helps us to replace a hexString with RGB color.

public static Color HexToColor(string hexString){            //replace # occurences            if (hexString.IndexOf('#') != -1)                hexString = hexString.Replace("#", "");            int r,g,b = 0;             r = int.Parse(hexString.Substring(0, 2), NumberStyles.AllowHexSpecifier);             g = int.Parse(hexString.Substring(2, 2), NumberStyles.AllowHexSpecifier);             b = int.Parse(hexString.Substring(4, 2), NumberStyles.AllowHexSpecifier);            return Color.FromArgb(r, g, b);}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Related Posts