ColorHelper.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Text;
  5. namespace HTEXLib
  6. {
  7. /// <summary>
  8. /// 类 名:ColorHelper
  9. /// 功 能:提供从RGB到HSV/HSL色彩空间的相互转换
  10. /// </summary>
  11. public static class ColorHelper
  12. {
  13. /// <summary>
  14. /// RGB转换HSV
  15. /// </summary>
  16. /// <param name="rgb"></param>
  17. /// <returns></returns>
  18. public static ColorHSV RgbToHsv(this ColorRGB rgb)
  19. {
  20. double min, max, tmp, H, S, V;
  21. double R = rgb.R * 1.0f / 255, G = rgb.G * 1.0f / 255, B = rgb.B * 1.0f / 255;
  22. tmp = System.Math.Min(R, G);
  23. min = System.Math.Min(tmp, B);
  24. tmp = System.Math.Max(R, G);
  25. max = System.Math.Max(tmp, B);
  26. // H
  27. H = 0;
  28. if (max == min)
  29. {
  30. H = 0;
  31. }
  32. else if (max == R && G > B)
  33. {
  34. H = 60 * (G - B) * 1.0f / (max - min) + 0;
  35. }
  36. else if (max == R && G < B)
  37. {
  38. H = 60 * (G - B) * 1.0f / (max - min) + 360;
  39. }
  40. else if (max == G)
  41. {
  42. H = H = 60 * (B - R) * 1.0f / (max - min) + 120;
  43. }
  44. else if (max == B)
  45. {
  46. H = H = 60 * (R - G) * 1.0f / (max - min) + 240;
  47. }
  48. // S
  49. if (max == 0)
  50. {
  51. S = 0;
  52. }
  53. else
  54. {
  55. S = (max - min) * 1.0f / max;
  56. }
  57. // V
  58. V = max;
  59. return new ColorHSV((int)H, (int)(S * 255), (int)(V * 255));
  60. }
  61. /// <summary>
  62. /// HSV转换RGB
  63. /// </summary>
  64. /// <param name="hsv"></param>
  65. /// <returns></returns>
  66. public static ColorRGB HsvToRgb( this ColorHSV hsv)
  67. {
  68. if (hsv.H == 360) hsv.H = 359; // 360为全黑,原因不明
  69. double R = 0f, G = 0f, B = 0f;
  70. if (hsv.S == 0)
  71. {
  72. return new ColorRGB(hsv.V, hsv.V, hsv.V);
  73. }
  74. double S = hsv.S * 1.0f / 255, V = hsv.V * 1.0f / 255;
  75. int H1 = (int)(hsv.H * 1.0f / 60), H = hsv.H;
  76. double F = H * 1.0f / 60 - H1;
  77. double P = V * (1.0f - S);
  78. double Q = V * (1.0f - F * S);
  79. double T = V * (1.0f - (1.0f - F) * S);
  80. switch (H1)
  81. {
  82. case 0: R = V; G = T; B = P; break;
  83. case 1: R = Q; G = V; B = P; break;
  84. case 2: R = P; G = V; B = T; break;
  85. case 3: R = P; G = Q; B = V; break;
  86. case 4: R = T; G = P; B = V; break;
  87. case 5: R = V; G = P; B = Q; break;
  88. }
  89. R = R * 255;
  90. G = G * 255;
  91. B = B * 255;
  92. while (R > 255) R -= 255;
  93. while (R < 0) R += 255;
  94. while (G > 255) G -= 255;
  95. while (G < 0) G += 255;
  96. while (B > 255) B -= 255;
  97. while (B < 0) B += 255;
  98. return new ColorRGB((int)R, (int)G, (int)B);
  99. }
  100. /// <summary>
  101. /// RGB转换HSL
  102. /// </summary>
  103. /// <param name="rgb"></param>
  104. /// <returns></returns>
  105. public static ColorHSL RgbToHsl( this ColorRGB rgb)
  106. {
  107. double min, max, tmp, H = 0, S = 0, L = 0;
  108. double R = rgb.R * 1.0f / 255, G = rgb.G * 1.0f / 255, B = rgb.B * 1.0f / 255;
  109. tmp = System.Math.Min(R, G);
  110. min = System.Math.Min(tmp, B);
  111. tmp = System.Math.Max(R, G);
  112. max = System.Math.Max(tmp, B);
  113. L = (max + min) / 2.0d;
  114. if (min == max)
  115. {
  116. H = S = 0;
  117. }
  118. else
  119. {
  120. double d = max - min;
  121. S = L > 0.5 ? d / (2 - max - min) : d / (max + min);
  122. if (max == R)
  123. {
  124. H = (G - B) / d + (G < B ? 6 : 0);
  125. }
  126. else if (max == G)
  127. {
  128. H = (G - R) / d + 2;
  129. }
  130. else if (max == B)
  131. {
  132. H = (R - G) / d + 4;
  133. }
  134. }
  135. H = H / 6.0F;
  136. return new ColorHSL() { H = H, S = S, L = L };
  137. }
  138. public static double hue2rgb(double p, double q, double t)
  139. {
  140. if (t < 0) t += 1;
  141. if (t > 1) t -= 1;
  142. if (t < 1.0 / 6.0d) return p + (q - p) * 6 * t;
  143. if (t < 1.0 / 2.0d) return q;
  144. if (t < 2.0 / 3.0d) return p + (q - p) * (2.0d / 3.0d - t) * 6.0d;
  145. return p;
  146. }
  147. /// <summary>
  148. /// HSL转换RGB
  149. /// </summary>
  150. /// <param name="hsl"></param>
  151. /// <returns></returns>
  152. public static ColorRGB HslToRgb( this ColorHSL hsl)
  153. {
  154. var l = hsl.L;
  155. var s = hsl.S;
  156. var h = hsl.H;
  157. double R = 0, G = 0, B = 0;
  158. if (hsl.S == 0)
  159. {
  160. R = G = B = hsl.L;
  161. }
  162. else
  163. {
  164. var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
  165. var p = 2 * l - q;
  166. R = hue2rgb(p, q, h + 1.0d / 3.0d);
  167. G = hue2rgb(p, q, h);
  168. B = hue2rgb(p, q, h - 1.0d / 3.0d);
  169. }
  170. R = R * 255; G = G * 255; B = B * 255;
  171. return new ColorRGB(int.Parse(System.Math.Round(R, 0) + ""), int.Parse(System.Math.Round(G, 0) + ""), int.Parse(System.Math.Round(B, 0) + ""));
  172. }
  173. private static double HueToRGB(double p, double q, double h)
  174. {
  175. if (h < 0) h += 1;
  176. if (h > 1) h -= 1;
  177. if (6 * h < 1)
  178. {
  179. return p + ((q - p) * 6 * h);
  180. }
  181. if (2 * h < 1)
  182. {
  183. return q;
  184. }
  185. if (3 * h < 2)
  186. {
  187. return p + ((q - p) * 6 * ((2.0f / 3.0f) - h));
  188. }
  189. return p;
  190. }
  191. /// <summary>
  192. /// 处理图像饱和度
  193. /// </summary>
  194. /// <param name="color"></param>
  195. /// <param name="lumMod"></param>
  196. /// <param name="lumOff"></param>
  197. /// <returns></returns>
  198. public static string GetColorLumModAndLumOff(Color color, int lumMod = 0, int lumOff = 0)
  199. {
  200. ColorHSL colorHSL = RgbToHsl(new ColorRGB(color.R, color.G, color.B));
  201. colorHSL.L = colorHSL.L * lumMod / 100_000.0 + lumOff / 100_000.0;
  202. ColorRGB colorRGB = HslToRgb(colorHSL);
  203. return ColorTranslator.ToHtml(Color.FromArgb(colorRGB.R, colorRGB.G, colorRGB.B)).Replace("#","");
  204. }
  205. /// <summary>
  206. ///
  207. /// </summary>
  208. /// <param name="color">原色RGB</param>
  209. /// <param name="pa">混合原色占百分比/100% </param>
  210. /// <param name="Type">Shade 与黑色混合 Tint与白色混合</param>
  211. /// <returns></returns>
  212. public static string GetShadeOrTintColor(Color color, double val, string Type)
  213. {
  214. ColorConverter converter = new ColorConverter();
  215. if (Type.Equals("Shade"))
  216. {
  217. return converter.SetShade(color, val);
  218. }
  219. else if (Type.Equals("Tint"))
  220. {
  221. return converter.SetTint(color, val);
  222. }
  223. else { return ColorTranslator.ToHtml(color).Replace("#",""); }
  224. }
  225. }
  226. }