Hex Color to RGB Color


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.