ColorCodeExtension.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Windows.UI;
  7. namespace KdanCommon.Helpers
  8. {
  9. public static class ColorCodeExtension
  10. {
  11. public static Color ToColor(this string hex)
  12. {
  13. byte a = 255;
  14. byte r = 0;
  15. byte g = 0;
  16. byte b = 0;
  17. if (hex.StartsWith("#"))
  18. {
  19. hex = hex.Replace("#", string.Empty);
  20. if (hex.Length == 8)
  21. {
  22. a = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
  23. r = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
  24. g = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
  25. b = (byte)(Convert.ToUInt32(hex.Substring(6, 2), 16));
  26. }
  27. else if (hex.Length == 6)
  28. {
  29. r = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
  30. g = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
  31. b = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
  32. }
  33. }
  34. return Color.FromArgb(a, r, g, b);
  35. }
  36. }
  37. }