ColorHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. public static class ColorHelper
  8. {
  9. private static readonly Dictionary<string, Color> _colorCache = new Dictionary<string, Color>();//颜色缓存
  10. private static readonly Dictionary<string, string> _htmlColorCache = new Dictionary<string, string>();//颜色缓存
  11. /// <summary>
  12. /// 判断color是否为HtmlColor(HtmlColor格式如#A1B2C3)
  13. /// </summary>
  14. /// <param name="color"></param>
  15. /// <returns></returns>
  16. public static bool IsHtmlColor(string color)
  17. {
  18. if (!color.StartsWith("#")) return false;
  19. if (color.Length != 7) return false;
  20. string r = color.Substring(1, 2);
  21. string g = color.Substring(3, 2);
  22. string b = color.Substring(5, 2);
  23. bool r1 = byte.TryParse(r, System.Globalization.NumberStyles.HexNumber, null, out _);
  24. bool r2 = byte.TryParse(g, System.Globalization.NumberStyles.HexNumber, null, out _);
  25. bool r3 = byte.TryParse(b, System.Globalization.NumberStyles.HexNumber, null, out _);
  26. return r1 & r2 & r3;
  27. }
  28. /// <summary>
  29. /// 根据key得到一种易识别的颜色,相同的key会得到相同的颜色(若要使多个key得到的颜色具有高对比度,请先调用GenerateColor)
  30. /// </summary>
  31. /// <param name="key"></param>
  32. /// <returns></returns>
  33. public static Color GetColor(string key)
  34. {
  35. if (key == null) key = "";
  36. if (_colorCache.ContainsKey(key))
  37. return _colorCache[key];
  38. Random random = new Random(key.GetHashCode());
  39. var H = random.Next(0, 360);//色调
  40. var S = random.NextDouble();//饱和度
  41. var L = random.NextDouble();//亮度
  42. S = 0.7 + S * 0.2; // [0.7 - 0.9] 排除过灰颜色
  43. L = 0.4 + L * 0.4; // [0.4 - 0.8] 排除过亮过暗色
  44. byte r, g, b;
  45. if (S == 0)
  46. {
  47. r = g = b = (byte)(L * 255);
  48. }
  49. else
  50. {
  51. double v1, v2;
  52. double hue = H / 360d;
  53. v2 = (L < 0.5) ? (L * (1 + S)) : ((L + S) - (L * S));
  54. v1 = 2 * L - v2;
  55. r = (byte)(255 * HueToRGB(v1, v2, hue + (1.0f / 3)));
  56. g = (byte)(255 * HueToRGB(v1, v2, hue));
  57. b = (byte)(255 * HueToRGB(v1, v2, hue - (1.0f / 3)));
  58. }
  59. var color = Color.FromArgb(r, g, b);
  60. var rgbhtml = ColorTranslator.ToHtml(color);
  61. if (!_colorCache.ContainsKey(key))
  62. _colorCache.Add(key, color);
  63. if (!_htmlColorCache.ContainsKey(key))
  64. _htmlColorCache.Add(key, rgbhtml);
  65. return color;
  66. }
  67. /// <summary>
  68. /// 根据key得到一种易识别的颜色,相同的key会得到相同的颜色(若要使多个key得到的颜色具有高对比度,请先调用GenerateColor)
  69. /// </summary>
  70. /// <param name="key"></param>
  71. /// <returns></returns>
  72. public static string GetHtmlColor(string key)
  73. {
  74. if (key == null) key = "";
  75. if (_htmlColorCache.ContainsKey(key))
  76. return _htmlColorCache[key];
  77. string rgbhtml = ColorTranslator.ToHtml(GetColor(key));
  78. return rgbhtml;
  79. }
  80. /// <summary>
  81. /// 提前生成多个易识别且不相近的颜色,生成后通过GetColor或CetHtmlColor获取
  82. /// </summary>
  83. /// <param name="keys">所有颜色对应的key,个数超过360时效果不佳</param>
  84. /// <returns></returns>
  85. public static void GenerateColor(IEnumerable<string> keys)
  86. {
  87. _colorCache.Clear();
  88. _htmlColorCache.Clear();
  89. keys = keys.Distinct();
  90. int idx = 0;
  91. List<(int H, double S, double L)> list = new List<(int H, double S, double L)>();
  92. int count = keys.Count();
  93. int value = 360 / count;
  94. if (value == 0) value = 1;
  95. foreach (var key in keys)
  96. {
  97. Random random = new Random(key.GetHashCode());
  98. int H = random.Next(idx * value, (idx + 1) * value);//色调
  99. H %= 360;
  100. double S = random.NextDouble();//饱和度
  101. double L = random.NextDouble();//亮度
  102. S = 0.7 + S * 0.2; // [0.7 - 0.9] 排除过灰颜色
  103. L = 0.4 + L * 0.4; // [0.4 - 0.8] 排除过亮过暗色
  104. byte r, g, b;
  105. if (S == 0)
  106. {
  107. r = g = b = (byte)(L * 255);
  108. }
  109. else
  110. {
  111. double v1, v2;
  112. double hue = H / 360d;
  113. v2 = (L < 0.5) ? (L * (1 + S)) : ((L + S) - (L * S));
  114. v1 = 2 * L - v2;
  115. r = (byte)(255 * HueToRGB(v1, v2, hue + (1.0f / 3)));
  116. g = (byte)(255 * HueToRGB(v1, v2, hue));
  117. b = (byte)(255 * HueToRGB(v1, v2, hue - (1.0f / 3)));
  118. }
  119. list.Add((H, S, L));
  120. var color = Color.FromArgb(r, g, b);
  121. var rgbhtml = ColorTranslator.ToHtml(color);
  122. if (!_colorCache.ContainsKey(key))
  123. _colorCache.Add(key, color);
  124. else
  125. _colorCache[key] = color;
  126. if (!_htmlColorCache.ContainsKey(key))
  127. _htmlColorCache.Add(key, rgbhtml);
  128. else
  129. _htmlColorCache[key] = rgbhtml;
  130. idx++;
  131. }
  132. }
  133. private static double HueToRGB(double v1, double v2, double vH)
  134. {
  135. if (vH < 0)
  136. vH += 1;
  137. if (vH > 1)
  138. vH -= 1;
  139. if ((6 * vH) < 1)
  140. return (v1 + (v2 - v1) * 6 * vH);
  141. if ((2 * vH) < 1)
  142. return v2;
  143. if ((3 * vH) < 2)
  144. return (v1 + (v2 - v1) * ((2.0f / 3) - vH) * 6);
  145. return v1;
  146. }
  147. }