RenderEngine.2.Color.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. using Microsoft.Drawing;
  4. namespace Microsoft.Windows.Forms
  5. {
  6. public static partial class RenderEngine
  7. {
  8. /// <summary>
  9. /// 颜色c1,相对c2是否为暗色
  10. /// </summary>
  11. /// <param name="c1">颜色c1</param>
  12. /// <param name="c2">颜色c2</param>
  13. /// <returns>是否为暗</returns>
  14. private static bool IsDarker(Color c1, Color c2)
  15. {
  16. HLSColor color = new HLSColor(c1);
  17. HLSColor color2 = new HLSColor(c2);
  18. return (color.Luminosity < color2.Luminosity);
  19. }
  20. /// <summary>
  21. /// 获取无效时文本颜色
  22. /// </summary>
  23. /// <param name="backColor">控件背景色</param>
  24. /// <returns>无效颜色</returns>
  25. public static Color GetGrayColor(Color backColor)
  26. {
  27. Color controlDark = SystemColors.ControlDark;
  28. if (RenderEngine.IsDarker(backColor, SystemColors.Control))
  29. {
  30. controlDark = ControlPaint.Dark(backColor);
  31. }
  32. return controlDark;
  33. }
  34. /// <summary>
  35. /// HSV/HSB颜色 转 RGB颜色
  36. /// </summary>
  37. /// <param name="hue">色调[0-1]</param>
  38. /// <param name="saturation">饱和度[0-1]</param>
  39. /// <param name="brightness">亮度[0-1]</param>
  40. /// <returns>RGB颜色</returns>
  41. public static Color FromHsv(float hue, float saturation, float brightness)
  42. {
  43. if (saturation == 0)
  44. {
  45. byte v = (byte)(brightness * 255f + 0.5f);
  46. return Color.FromArgb(v, v, v);
  47. }
  48. else
  49. {
  50. float h = hue * 6f;
  51. int nh = (int)h;
  52. float sf = saturation * (h - nh);
  53. byte p = (byte)(brightness * (1f - saturation) * 255f + 0.5f);
  54. byte q = (byte)(brightness * (1f - sf) * 255f + 0.5f);
  55. byte t = (byte)(brightness * (1f - saturation + sf) * 255f + 0.5f);
  56. byte v = (byte)(brightness * 255.0f + 0.5f);
  57. switch (nh)
  58. {
  59. case 0:
  60. return Color.FromArgb(v, t, p);
  61. case 1:
  62. return Color.FromArgb(q, v, p);
  63. case 2:
  64. return Color.FromArgb(p, v, t);
  65. case 3:
  66. return Color.FromArgb(p, q, v);
  67. case 4:
  68. return Color.FromArgb(t, p, v);
  69. case 5:
  70. return Color.FromArgb(v, p, q);
  71. default:
  72. return Color.Empty;
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// 获取渐变颜色位置数组
  78. /// </summary>
  79. /// <param name="baseColor">基色</param>
  80. /// <param name="pos1">位置1</param>
  81. /// <param name="pos2">位置2</param>
  82. /// <param name="reverse">是否反转</param>
  83. /// <param name="avg">是否均分位置</param>
  84. /// <param name="colors">颜色数组</param>
  85. /// <param name="positions">位置数组</param>
  86. public static void GetColorPosGradient(Color baseColor, float pos1, float pos2, bool reverse, bool avg, out Color[] colors, out float[] positions)
  87. {
  88. ColorVector vector = ColorVector.FromArgb(8, 8, 8);
  89. Color outerColor = baseColor + vector;
  90. Color innerColor = baseColor - vector;
  91. if (reverse)
  92. {
  93. colors = new Color[] { outerColor, innerColor, innerColor, outerColor };
  94. if (avg)
  95. positions = new float[] { 0.0f, 0.333333f, 0.666667f, 1.0f };//均分时.反转与不反转相同
  96. else
  97. positions = new float[] { 0.0f, 1.0f - pos2, 1.0f - pos1, 1.0f };//非均分时.1-各个元素,再反转顺序
  98. }
  99. else
  100. {
  101. colors = new Color[] { outerColor, innerColor, innerColor, outerColor };
  102. if (avg)
  103. positions = new float[] { 0.0f, 0.333333f, 0.666667f, 1.0f };
  104. else
  105. positions = new float[] { 0.0f, pos1, pos2, 1.0f };
  106. }
  107. }
  108. /// <summary>
  109. /// 获取渐显颜色位置数组
  110. /// </summary>
  111. /// <param name="baseColor">基色</param>
  112. /// <param name="pos1">位置1</param>
  113. /// <param name="pos2">位置2</param>
  114. /// <param name="reverse">是否反转</param>
  115. /// <param name="avg">是否均分位置</param>
  116. /// <param name="colors">颜色数组</param>
  117. /// <param name="positions">位置数组</param>
  118. public static void GetColorPosFadeIn(Color baseColor, float pos1, float pos2, bool reverse, bool avg, out Color[] colors, out float[] positions)
  119. {
  120. if (reverse)
  121. {
  122. colors = new Color[] { baseColor, baseColor, Color.Transparent };
  123. if (avg)
  124. positions = new float[] { 0.0f, 0.5f, 1.0f };
  125. else
  126. positions = new float[] { 0.0f, 1.0f - pos1, 1.0f };
  127. }
  128. else
  129. {
  130. colors = new Color[] { Color.Transparent, baseColor, baseColor };
  131. if (avg)
  132. positions = new float[] { 0.0f, 0.5f, 1.0f };
  133. else
  134. positions = new float[] { 0.0f, pos1, 1.0f };
  135. }
  136. }
  137. /// <summary>
  138. /// 获取渐隐颜色位置数组
  139. /// </summary>
  140. /// <param name="baseColor">基色</param>
  141. /// <param name="pos1">位置1</param>
  142. /// <param name="pos2">位置2</param>
  143. /// <param name="reverse">是否反转</param>
  144. /// <param name="avg">是否均分位置</param>
  145. /// <param name="colors">颜色数组</param>
  146. /// <param name="positions">位置数组</param>
  147. public static void GetColorPosFadeOut(Color baseColor, float pos1, float pos2, bool reverse, bool avg, out Color[] colors, out float[] positions)
  148. {
  149. if (reverse)
  150. {
  151. colors = new Color[] { Color.Transparent, baseColor, baseColor };
  152. if (avg)
  153. positions = new float[] { 0.0f, 0.5f, 1.0f };
  154. else
  155. positions = new float[] { 0.0f, 1.0f - pos1, 1.0f };
  156. }
  157. else
  158. {
  159. colors = new Color[] { baseColor, baseColor, Color.Transparent };
  160. if (avg)
  161. positions = new float[] { 0.0f, 0.5f, 1.0f };
  162. else
  163. positions = new float[] { 0.0f, pos1, 1.0f };
  164. }
  165. }
  166. /// <summary>
  167. /// 获取渐隐渐显颜色位置数组
  168. /// </summary>
  169. /// <param name="baseColor">基色</param>
  170. /// <param name="pos1">位置1</param>
  171. /// <param name="pos2">位置2</param>
  172. /// <param name="reverse">是否反转</param>
  173. /// <param name="avg">是否均分位置</param>
  174. /// <param name="colors">颜色数组</param>
  175. /// <param name="positions">位置数组</param>
  176. public static void GetColorPosFadeInFadeOut(Color baseColor, float pos1, float pos2, bool reverse, bool avg, out Color[] colors, out float[] positions)
  177. {
  178. if (reverse)
  179. {
  180. colors = new Color[] { Color.Transparent, baseColor, baseColor, Color.Transparent };
  181. if (avg)
  182. positions = new float[] { 0.0f, 0.333333f, 0.666667f, 1.0f };
  183. else
  184. positions = new float[] { 0.0f, 1.0f - pos2, 1.0f - pos1, 1.0f };
  185. }
  186. else
  187. {
  188. colors = new Color[] { Color.Transparent, baseColor, baseColor, Color.Transparent };
  189. if (avg)
  190. positions = new float[] { 0.0f, 0.333333f, 0.666667f, 1.0f };
  191. else
  192. positions = new float[] { 0.0f, pos1, pos2, 1.0f };
  193. }
  194. }
  195. }
  196. }