dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » .NET » C#
Language: .NET
Expertise: Intermediate
Jul 4, 2019

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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);
}
Srinath M S
 
Thanks for your registration, follow us on our social networks to keep up-to-date