Posted By

vcskicks on 03/21/09


Tagged

hex to RGB


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
vali29


Hex Color to RGB Color


 / Published in: C#
 

URL: http://www.vcskicks.com/hex-to-rgb.php

  1. public static Color HexToColor(string hexColor)
  2. {
  3. //Remove # if present
  4. if (hexColor.IndexOf('#') != -1)
  5. hexColor = hexColor.Replace("#", "");
  6.  
  7. int red = 0;
  8. int green = 0;
  9. int blue = 0;
  10.  
  11. if (hexColor.Length == 6)
  12. {
  13. //#RRGGBB
  14. red = int.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier);
  15. green = int.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier);
  16. blue = int.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier);
  17. }
  18. else if (hexColor.Length == 3)
  19. {
  20. //#RGB
  21. red = int.Parse(hexColor[0].ToString() + hexColor[0].ToString(), NumberStyles.AllowHexSpecifier);
  22. green = int.Parse(hexColor[1].ToString() + hexColor[1].ToString(), NumberStyles.AllowHexSpecifier);
  23. blue = int.Parse(hexColor[2].ToString() + hexColor[2].ToString(), NumberStyles.AllowHexSpecifier);
  24. }
  25.  
  26. return Color.FromArgb(red, green, blue);
  27. }

Report this snippet  

You need to login to post a comment.