ColorHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace TEAMModelOS.Test.PPTX.ColorLibrary
  5. {
  6. /// <summary>
  7. /// 类 名:ColorHelper
  8. /// 功 能:提供从RGB到HSV/HSL色彩空间的相互转换
  9. /// </summary>
  10. public static class ColorHelper
  11. {
  12. /// <summary>
  13. /// RGB转换HSV
  14. /// </summary>
  15. /// <param name="rgb"></param>
  16. /// <returns></returns>
  17. public static ColorHSV RgbToHsv(ColorRGB rgb)
  18. {
  19. float min, max, tmp, H, S, V;
  20. float R = rgb.R * 1.0f / 255, G = rgb.G * 1.0f / 255, B = rgb.B * 1.0f / 255;
  21. tmp = Math.Min(R, G);
  22. min = Math.Min(tmp, B);
  23. tmp = Math.Max(R, G);
  24. max = Math.Max(tmp, B);
  25. // H
  26. H = 0;
  27. if (max == min)
  28. {
  29. H = 0;
  30. }
  31. else if (max == R && G > B)
  32. {
  33. H = 60 * (G - B) * 1.0f / (max - min) + 0;
  34. }
  35. else if (max == R && G < B)
  36. {
  37. H = 60 * (G - B) * 1.0f / (max - min) + 360;
  38. }
  39. else if (max == G)
  40. {
  41. H = H = 60 * (B - R) * 1.0f / (max - min) + 120;
  42. }
  43. else if (max == B)
  44. {
  45. H = H = 60 * (R - G) * 1.0f / (max - min) + 240;
  46. }
  47. // S
  48. if (max == 0)
  49. {
  50. S = 0;
  51. }
  52. else
  53. {
  54. S = (max - min) * 1.0f / max;
  55. }
  56. // V
  57. V = max;
  58. return new ColorHSV((int)H, (int)(S * 255), (int)(V * 255));
  59. }
  60. /// <summary>
  61. /// HSV转换RGB
  62. /// </summary>
  63. /// <param name="hsv"></param>
  64. /// <returns></returns>
  65. public static ColorRGB HsvToRgb(ColorHSV hsv)
  66. {
  67. if (hsv.H == 360) hsv.H = 359; // 360为全黑,原因不明
  68. float R = 0f, G = 0f, B = 0f;
  69. if (hsv.S == 0)
  70. {
  71. return new ColorRGB(hsv.V, hsv.V, hsv.V);
  72. }
  73. float S = hsv.S * 1.0f / 255, V = hsv.V * 1.0f / 255;
  74. int H1 = (int)(hsv.H * 1.0f / 60), H = hsv.H;
  75. float F = H * 1.0f / 60 - H1;
  76. float P = V * (1.0f - S);
  77. float Q = V * (1.0f - F * S);
  78. float T = V * (1.0f - (1.0f - F) * S);
  79. switch (H1)
  80. {
  81. case 0: R = V; G = T; B = P; break;
  82. case 1: R = Q; G = V; B = P; break;
  83. case 2: R = P; G = V; B = T; break;
  84. case 3: R = P; G = Q; B = V; break;
  85. case 4: R = T; G = P; B = V; break;
  86. case 5: R = V; G = P; B = Q; break;
  87. }
  88. R = R * 255;
  89. G = G * 255;
  90. B = B * 255;
  91. while (R > 255) R -= 255;
  92. while (R < 0) R += 255;
  93. while (G > 255) G -= 255;
  94. while (G < 0) G += 255;
  95. while (B > 255) B -= 255;
  96. while (B < 0) B += 255;
  97. return new ColorRGB((int)R, (int)G, (int)B);
  98. }
  99. /// <summary>
  100. /// RGB转换HSL
  101. /// </summary>
  102. /// <param name="rgb"></param>
  103. /// <returns></returns>
  104. public static ColorHSL RgbToHsl(ColorRGB rgb)
  105. {
  106. float min, max, tmp, H, S, L;
  107. float R = rgb.R * 1.0f / 255, G = rgb.G * 1.0f / 255, B = rgb.B * 1.0f / 255;
  108. tmp = Math.Min(R, G);
  109. min = Math.Min(tmp, B);
  110. tmp = Math.Max(R, G);
  111. max = Math.Max(tmp, B);
  112. // H
  113. H = 0;
  114. if (max == min)
  115. {
  116. H = 0; // 此时H应为未定义,通常写为0
  117. }
  118. else if (max == R && G > B)
  119. {
  120. H = 60 * (G - B) * 1.0f / (max - min) + 0;
  121. }
  122. else if (max == R && G < B)
  123. {
  124. H = 60 * (G - B) * 1.0f / (max - min) + 360;
  125. }
  126. else if (max == G)
  127. {
  128. H = H = 60 * (B - R) * 1.0f / (max - min) + 120;
  129. }
  130. else if (max == B)
  131. {
  132. H = H = 60 * (R - G) * 1.0f / (max - min) + 240;
  133. }
  134. // L
  135. L = 0.5f * (max + min);
  136. // S
  137. S = 0;
  138. if (L == 0 || max == min)
  139. {
  140. S = 0;
  141. }
  142. else if (0 < L && L < 0.5)
  143. {
  144. S = (max - min) / (L * 2);
  145. }
  146. else if (L > 0.5)
  147. {
  148. S = (max - min) / (2 - 2 * L);
  149. }
  150. return new ColorHSL((int)H, (int)(S * 255), (int)(L * 255));
  151. }
  152. /// <summary>
  153. /// HSL转换RGB
  154. /// </summary>
  155. /// <param name="hsl"></param>
  156. /// <returns></returns>
  157. public static ColorRGB HslToRgb(ColorHSL hsl)
  158. {
  159. float R = 0f, G = 0f, B = 0f;
  160. float S = hsl.S * 1.0f / 255, L = hsl.L * 1.0f / 255;
  161. float temp1, temp2, temp3;
  162. if (S == 0f) // 灰色
  163. {
  164. R = L;
  165. G = L;
  166. B = L;
  167. }
  168. else
  169. {
  170. if (L < 0.5f)
  171. {
  172. temp2 = L * (1.0f + S);
  173. }
  174. else
  175. {
  176. temp2 = L + S - L * S;
  177. }
  178. temp1 = 2.0f * L - temp2;
  179. float H = hsl.H * 1.0f / 360;
  180. // R
  181. temp3 = H + 1.0f / 3.0f;
  182. if (temp3 < 0) temp3 += 1.0f;
  183. if (temp3 > 1) temp3 -= 1.0f;
  184. R = temp3;
  185. // G
  186. temp3 = H;
  187. if (temp3 < 0) temp3 += 1.0f;
  188. if (temp3 > 1) temp3 -= 1.0f;
  189. G = temp3;
  190. // B
  191. temp3 = H - 1.0f / 3.0f;
  192. if (temp3 < 0) temp3 += 1.0f;
  193. if (temp3 > 1) temp3 -= 1.0f;
  194. B = temp3;
  195. }
  196. R = R * 255;
  197. G = G * 255;
  198. B = B * 255;
  199. return new ColorRGB((int)R, (int)G, (int)B);
  200. }
  201. }
  202. }