24 April 2011

C# - Convert hex color to RGB

To convert a hexadecimal color value like #FF00FF to a RGB color value, I wrote this function:

string color = "#FF00FF"; //This would be a parameter
if (color.StartsWith("#"))
  color = color.Remove(0, 1);
byte r, g, b;
if (color.Length == 3) {
  r = Convert.ToByte(color[0] + "" + color[0], 16);
  g = Convert.ToByte(color[1] + "" + color[1], 16);
  b = Convert.ToByte(color[2] + "" + color[2], 16);
}
else if (color.Length == 6) {
  r = Convert.ToByte(color[0] + "" + color[1], 16);
  g = Convert.ToByte(color[2] + "" + color[3], 16);
  b = Convert.ToByte(color[4] + "" + color[5], 16);
}
else {
  throw new ArgumentException("Hex color " + color + " is invalid.");
}
return new Color.FromArgb(255, r, g, b);

2 comments:

  1. This is a nice article..
    Its easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial, Ms sql server

    Thanks a lot..!
    ri80

    ReplyDelete
  2. Thank you very much.
    Glad it could help you!

    ReplyDelete