You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

375 lines
10 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Customize.Controls
  9. {
  10. public enum EnumTheme
  11. {
  12. Default = 0,
  13. BlueSea = 1,
  14. KissOfAngel = 2,
  15. NoFlower = 3,
  16. SunsetRed = 4,
  17. }
  18. public enum LinearGradientModePanel
  19. {
  20. Horizontal = 0,
  21. Vertical = 1,
  22. ForwardDiagonal = 2,
  23. BackwardDiagonal = 3,
  24. None = 4
  25. }
  26. [FlagsAttribute()]
  27. public enum CornerCurveMode
  28. {
  29. None = 0,
  30. TopLeft = 1,
  31. TopRight = 2,
  32. TopLeft_TopRight = 3,
  33. BottomLeft = 4,
  34. TopLeft_BottomLeft = 5,
  35. TopRight_BottomLeft = 6,
  36. TopLeft_TopRight_BottomLeft = 7,
  37. BottomRight = 8,
  38. BottomRight_TopLeft = 9,
  39. BottomRight_TopRight = 10,
  40. BottomRight_TopLeft_TopRight = 11,
  41. BottomRight_BottomLeft = 12,
  42. BottomRight_TopLeft_BottomLeft = 13,
  43. BottomRight_TopRight_BottomLeft = 14,
  44. All = 15
  45. }
  46. /// <summary>
  47. /// 建立圆角路径的样式。
  48. /// </summary>
  49. public enum RoundStyle
  50. {
  51. /// <summary>
  52. /// 四个角都不是圆角。
  53. /// </summary>
  54. None = 0,
  55. /// <summary>
  56. /// 四个角都为圆角。
  57. /// </summary>
  58. All = 1,
  59. /// <summary>
  60. /// 左边两个角为圆角。
  61. /// </summary>
  62. Left = 2,
  63. /// <summary>
  64. /// 右边两个角为圆角。
  65. /// </summary>
  66. Right = 3,
  67. /// <summary>
  68. /// 上边两个角为圆角。
  69. /// </summary>
  70. Top = 4,
  71. /// <summary>
  72. /// 下边两个角为圆角。
  73. /// </summary>
  74. Bottom = 5,
  75. /// <summary>
  76. /// 左下角为圆角。
  77. /// </summary>
  78. BottomLeft = 6,
  79. /// <summary>
  80. /// 右下角为圆角。
  81. /// </summary>
  82. BottomRight = 7,
  83. }
  84. /// <summary>
  85. /// 矩形的圆角半径
  86. /// </summary>
  87. public struct CornerRadius
  88. {
  89. #region Initializes
  90. /// <summary>
  91. /// (构造函数).Initializes a new instance of the <see cref="CornerRadius"/> struct.
  92. /// 设置四个角为相同的圆角半径
  93. /// </summary>
  94. /// <param name="radius">The radius.</param>
  95. public CornerRadius(int radius)
  96. : this(radius, radius, radius, radius)
  97. {
  98. }
  99. /// <summary>
  100. /// (构造函数).Initializes a new instance of the <see cref="CornerRadius"/> struct.
  101. /// 初始化四个角的圆角半径
  102. /// </summary>
  103. /// <param name="topLeft">The top left.</param>
  104. /// <param name="topRight">The top right.</param>
  105. /// <param name="bottomLeft">The bottom left.</param>
  106. /// <param name="bottomRight">The bottom right.</param>
  107. public CornerRadius(int topLeft, int topRight, int bottomLeft, int bottomRight)
  108. {
  109. this.TopLeft = topLeft;
  110. this.TopRight = topRight;
  111. this.BottomLeft = bottomLeft;
  112. this.BottomRigth = bottomRight;
  113. }
  114. #endregion
  115. #region Fields
  116. /// <summary>
  117. /// 左上角圆角半径
  118. /// </summary>
  119. public int TopLeft;
  120. /// <summary>
  121. /// 右上角圆角半径
  122. /// </summary>
  123. public int TopRight;
  124. /// <summary>
  125. /// 左下角圆角半径
  126. /// </summary>
  127. public int BottomLeft;
  128. /// <summary>
  129. /// 右下角圆角半径
  130. /// </summary>
  131. public int BottomRigth;
  132. #endregion
  133. }
  134. public enum EnumControlState
  135. {
  136. None,
  137. /// <summary>
  138. /// 默认状态
  139. /// </summary>
  140. Default,
  141. /// <summary>
  142. /// 高亮状态(鼠标悬浮)
  143. /// </summary>
  144. HeightLight,
  145. /// <summary>
  146. /// 焦点(鼠标按下、已选择、输入状态等)
  147. /// </summary>
  148. Focused,
  149. }
  150. public struct HSLColor
  151. {
  152. double m_hue;
  153. double m_saturation;
  154. double m_lightness;
  155. // http://en.wikipedia.org/wiki/HSL_color_space
  156. public double Hue
  157. {
  158. get { return m_hue; }
  159. set { m_hue = value; }
  160. }
  161. public double Saturation
  162. {
  163. get { return m_saturation; }
  164. set { m_saturation = value; }
  165. }
  166. public double Lightness
  167. {
  168. get { return m_lightness; }
  169. set
  170. {
  171. m_lightness = value;
  172. if (m_lightness < 0)
  173. m_lightness = 0;
  174. if (m_lightness > 1)
  175. m_lightness = 1;
  176. }
  177. }
  178. public HSLColor(double hue, double saturation, double lightness)
  179. {
  180. m_hue = Math.Min(360, hue);
  181. m_saturation = Math.Min(1, saturation);
  182. m_lightness = Math.Min(1, lightness);
  183. }
  184. public HSLColor(Color color)
  185. {
  186. m_hue = 0;
  187. m_saturation = 1;
  188. m_lightness = 1;
  189. FromRGB(color);
  190. }
  191. public Color Color
  192. {
  193. get { return ToRGB(); }
  194. set { FromRGB(value); }
  195. }
  196. void FromRGB(Color cc)
  197. {
  198. double r = (double)cc.R / 255d;
  199. double g = (double)cc.G / 255d;
  200. double b = (double)cc.B / 255d;
  201. double min = Math.Min(Math.Min(r, g), b);
  202. double max = Math.Max(Math.Max(r, g), b);
  203. // calulate hue according formula given in
  204. // "Conversion from RGB to HSL or HSV"
  205. m_hue = 0;
  206. if (min != max)
  207. {
  208. if (r == max && g >= b)
  209. {
  210. m_hue = 60 * ((g - b) / (max - min)) + 0;
  211. }
  212. else
  213. if (r == max && g < b)
  214. {
  215. m_hue = 60 * ((g - b) / (max - min)) + 360;
  216. }
  217. else
  218. if (g == max)
  219. {
  220. m_hue = 60 * ((b - r) / (max - min)) + 120;
  221. }
  222. else
  223. if (b == max)
  224. {
  225. m_hue = 60 * ((r - g) / (max - min)) + 240;
  226. }
  227. }
  228. // find lightness
  229. m_lightness = (min + max) / 2;
  230. // find saturation
  231. if (m_lightness == 0 || min == max)
  232. m_saturation = 0;
  233. else
  234. if (m_lightness > 0 && m_lightness <= 0.5)
  235. m_saturation = (max - min) / (2 * m_lightness);
  236. else
  237. if (m_lightness > 0.5)
  238. m_saturation = (max - min) / (2 - 2 * m_lightness);
  239. }
  240. Color ToRGB()
  241. {
  242. // convert to RGB according to
  243. // "Conversion from HSL to RGB"
  244. double r = m_lightness;
  245. double g = m_lightness;
  246. double b = m_lightness;
  247. if (m_saturation == 0)
  248. return Color.FromArgb(255, (int)(r * 255), (int)(g * 255), (int)(b * 255));
  249. double q = 0;
  250. if (m_lightness < 0.5)
  251. q = m_lightness * (1 + m_saturation);
  252. else
  253. q = m_lightness + m_saturation - (m_lightness * m_saturation);
  254. double p = 2 * m_lightness - q;
  255. double hk = m_hue / 360;
  256. // r,g,b colors
  257. double[] tc = new double[3] { hk + (1d / 3d), hk, hk - (1d / 3d) };
  258. double[] colors = new double[3] { 0, 0, 0 };
  259. for (int color = 0; color < colors.Length; color++)
  260. {
  261. if (tc[color] < 0)
  262. tc[color] += 1;
  263. if (tc[color] > 1)
  264. tc[color] -= 1;
  265. if (tc[color] < (1d / 6d))
  266. colors[color] = p + ((q - p) * 6 * tc[color]);
  267. else
  268. if (tc[color] >= (1d / 6d) && tc[color] < (1d / 2d))
  269. colors[color] = q;
  270. else
  271. if (tc[color] >= (1d / 2d) && tc[color] < (2d / 3d))
  272. colors[color] = p + ((q - p) * 6 * (2d / 3d - tc[color]));
  273. else
  274. colors[color] = p;
  275. colors[color] *= 255; // convert to value expected by Color
  276. }
  277. return Color.FromArgb(255, (int)colors[0], (int)colors[1], (int)colors[2]);
  278. }
  279. public static bool operator !=(HSLColor left, HSLColor right)
  280. {
  281. return !(left == right);
  282. }
  283. public static bool operator ==(HSLColor left, HSLColor right)
  284. {
  285. return (left.Hue == right.Hue &&
  286. left.Lightness == right.Lightness &&
  287. left.Saturation == right.Saturation);
  288. }
  289. public override string ToString()
  290. {
  291. string s = string.Format("HSL({0:f2}, {1:f2}, {2:f2})", Hue, Saturation, Lightness);
  292. return s;
  293. }
  294. }
  295. #region 阶梯渐变色彩 GradientColor
  296. /// <summary>
  297. /// 阶梯渐变色彩
  298. /// </summary>
  299. public struct GradientColor
  300. {
  301. /// <summary>
  302. /// (构造函数).Initializes a new instance of the <see cref="GradientColor"/> struct.
  303. /// </summary>
  304. /// <param name="color1">The color1.</param>
  305. /// <param name="color2">The color2.</param>
  306. /// <param name="factors">The factors.</param>
  307. /// <param name="positions">The positions.</param>
  308. public GradientColor(Color color1, Color color2, float[] factors, float[] positions)
  309. {
  310. this.First = color1;
  311. this.Second = color2;
  312. this.Factors = factors == null ? new float[] { } : factors;
  313. this.Positions = positions == null ? new float[] { } : positions;
  314. }
  315. /// <summary>
  316. /// 初始色彩
  317. /// </summary>
  318. public Color First;
  319. /// <summary>
  320. /// 结束色彩
  321. /// </summary>
  322. public Color Second;
  323. /// <summary>
  324. /// 色彩渲染系数(0到1的浮点数值)
  325. /// </summary>
  326. public float[] Factors;
  327. /// <summary>
  328. /// 色彩渲染位置(0到1的浮点数值)
  329. /// </summary>
  330. public float[] Positions;
  331. }
  332. #endregion
  333. }