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


advertisement
 

Tip: Convert Hex to RGB in C#

Learn how to replace a hex string with RGB color in C# using the system.globalization namespace.


advertisement

WEBINAR:

On-Demand

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


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

 

Visit the DevX Tip Bank

 





   
Srinath M. S. says: "I would love to leave a footprint in this flat world."
Thanks for your registration, follow us on our social networks to keep up-to-date