Guest User

Untitled

a guest
May 26th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. public SolidColorBrush GetSolidColorBrush(string hex)
  2. {
  3. hex = hex.Replace("#", string.Empty);
  4.  
  5. //#FFDFD991
  6. //#DFD991
  7. //#FD92
  8. //#DAC
  9.  
  10. bool existAlpha = hex.Length == 8 || hex.Length == 4;
  11. bool isDoubleHex = hex.Length == 8 || hex.Length == 6;
  12.  
  13. if (!existAlpha && hex.Length != 6 && hex.Length != 3)
  14. {
  15. throw new ArgumentException("Input string is invalid color");
  16. }
  17.  
  18. int n = 0;
  19. byte a;
  20. int hexCount = isDoubleHex ? 2 : 1;
  21. if (existAlpha)
  22. {
  23. n = hexCount;
  24. a = (byte) ConvertHexToByte(hex, 0, hexCount);
  25. if (!isDoubleHex)
  26. {
  27. a = (byte) (a * 16 + a);
  28. }
  29. }
  30. else
  31. {
  32. a = 0xFF;
  33. }
  34.  
  35. var r = (byte) ConvertHexToByte(hex, n, hexCount);
  36. var g = (byte) ConvertHexToByte(hex, n + hexCount, hexCount);
  37. var b = (byte) ConvertHexToByte(hex, n + 2 * hexCount, hexCount);
  38. if (!isDoubleHex)
  39. {
  40. //#FD92 = #FFDD9922
  41.  
  42. r = (byte) (r * 16 + r);
  43. g = (byte) (g * 16 + g);
  44. b = (byte) (b * 16 + b);
  45. }
  46.  
  47. return new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
  48. }
  49.  
  50. private static uint ConvertHexToByte(string hex, int n, int count = 2)
  51. {
  52. return Convert.ToUInt32(hex.Substring(n, count), 16);
  53. }
Add Comment
Please, Sign In to add comment