tangxu
2024-12-29 72e75456f8b30ec5b6f355539d9c883b0f810d21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using System.Drawing;
 
namespace Microsoft.Windows.Forms
{
    public static partial class RenderEngine
    {
        /// <summary>
        /// 色调,亮度,饱和度颜色
        /// </summary>
        private struct HLSColor
        {
            private const int ShadowAdj = -333;
            private const int HilightAdj = 500;
            private const int WatermarkAdj = -50;
            private const int Range = 240;
            private const int HLSMax = Range;
            private const int RGBMax = 255;
            private const int Undefined = HLSMax * 2 / 3;
 
            private int hue;
            private int saturation;
            private int luminosity;
            private bool isSystemColors_Control;
 
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="color">RGBA颜色</param>
            public HLSColor(Color color)
            {
                isSystemColors_Control = (color.ToKnownColor() == SystemColors.Control.ToKnownColor());
                int r = color.R;
                int g = color.G;
                int b = color.B;
                int max, min;        /* max and min RGB values */
                int sum, dif;
                int Rdelta, Gdelta, Bdelta;  /* intermediate value: % of spread from max */
 
                /* calculate lightness */
                max = Math.Max(Math.Max(r, g), b);
                min = Math.Min(Math.Min(r, g), b);
                sum = max + min;
 
                luminosity = (((sum * HLSMax) + RGBMax) / (2 * RGBMax));
 
                dif = max - min;
                if (dif == 0)
                {       /* r=g=b --> achromatic case */
                    saturation = 0;                         /* saturation */
                    hue = Undefined;                 /* hue */
                }
                else
                {                           /* chromatic case */
                    /* saturation */
                    if (luminosity <= (HLSMax / 2))
                        saturation = (int)(((dif * (int)HLSMax) + (sum / 2)) / sum);
                    else
                        saturation = (int)((int)((dif * (int)HLSMax) + (int)((2 * RGBMax - sum) / 2))
                                            / (2 * RGBMax - sum));
                    /* hue */
                    Rdelta = (int)((((max - r) * (int)(HLSMax / 6)) + (dif / 2)) / dif);
                    Gdelta = (int)((((max - g) * (int)(HLSMax / 6)) + (dif / 2)) / dif);
                    Bdelta = (int)((((max - b) * (int)(HLSMax / 6)) + (dif / 2)) / dif);
 
                    if ((int)r == max)
                        hue = Bdelta - Gdelta;
                    else if ((int)g == max)
                        hue = (HLSMax / 3) + Rdelta - Bdelta;
                    else /* B == cMax */
                        hue = ((2 * HLSMax) / 3) + Gdelta - Rdelta;
 
                    if (hue < 0)
                        hue += HLSMax;
                    if (hue > HLSMax)
                        hue -= HLSMax;
                }
            }
 
            /// <summary>
            /// 色调
            /// </summary>
            public int Hue
            {
                get
                {
                    return hue;
                }
            }
 
            /// <summary>
            /// 亮度
            /// </summary>
            public int Luminosity
            {
                get
                {
                    return luminosity;
                }
            }
 
            /// <summary>
            /// 饱和度
            /// </summary>
            public int Saturation
            {
                get
                {
                    return saturation;
                }
            }
 
            /// <summary>
            /// 获取减小指定亮度后的颜色
            /// </summary>
            /// <param name="percDarker">要减小的亮度</param>
            /// <returns>新的颜色</returns>
            public Color Darker(float percDarker)
            {
                if (isSystemColors_Control)
                {
                    // With the usual color scheme, ControlDark/DarkDark is not exactly
                    // what we would otherwise calculate
                    if (percDarker == 0.0f)
                    {
                        return SystemColors.ControlDark;
                    }
                    else if (percDarker == 1.0f)
                    {
                        return SystemColors.ControlDarkDark;
                    }
                    else
                    {
                        Color dark = SystemColors.ControlDark;
                        Color darkDark = SystemColors.ControlDarkDark;
 
                        int dr = dark.R - darkDark.R;
                        int dg = dark.G - darkDark.G;
                        int db = dark.B - darkDark.B;
 
                        return Color.FromArgb((byte)(dark.R - (byte)(dr * percDarker)),
                                              (byte)(dark.G - (byte)(dg * percDarker)),
                                              (byte)(dark.B - (byte)(db * percDarker)));
                    }
                }
                else
                {
                    int oneLum = 0;
                    int zeroLum = NewLuma(ShadowAdj, true);
                    return ColorFromHLS(hue, zeroLum - (int)((zeroLum - oneLum) * percDarker), saturation);
                }
            }
 
            /// <summary>
            /// 是否相等
            /// </summary>
            /// <param name="o">要比较的对象</param>
            /// <returns>相等返回true,否则返回false</returns>
            public override bool Equals(object o)
            {
                if (!(o is HLSColor))
                {
                    return false;
                }
 
                HLSColor c = (HLSColor)o;
                return hue == c.hue &&
                       saturation == c.saturation &&
                       luminosity == c.luminosity &&
                       isSystemColors_Control == c.isSystemColors_Control;
            }
 
            /// <summary>
            /// 是否相等
            /// </summary>
            /// <param name="a">颜色a</param>
            /// <param name="b">颜色b</param>
            /// <returns>相等返回true,否则返回false</returns>
            public static bool operator ==(HLSColor a, HLSColor b)
            {
                return a.Equals(b);
            }
 
            /// <summary>
            /// 是否不等
            /// </summary>
            /// <param name="a">颜色a</param>
            /// <param name="b">颜色b</param>
            /// <returns>不等返回true,否则返回false</returns>
            public static bool operator !=(HLSColor a, HLSColor b)
            {
                return !a.Equals(b);
            }
 
            /// <summary>
            /// 获取哈希码
            /// </summary>
            /// <returns>哈希码</returns>
            public override int GetHashCode()
            {
                return hue << 6 | saturation << 2 | luminosity;
            }
 
            /// <summary>
            /// 获取增加指定亮度后的颜色
            /// </summary>
            /// <param name="percLighter">要增加的亮度</param>
            /// <returns>新的颜色</returns>
            public Color Lighter(float percLighter)
            {
                if (isSystemColors_Control)
                {
                    // With the usual color scheme, ControlLight/LightLight is not exactly
                    // what we would otherwise calculate
                    if (percLighter == 0.0f)
                    {
                        return SystemColors.ControlLight;
                    }
                    else if (percLighter == 1.0f)
                    {
                        return SystemColors.ControlLightLight;
                    }
                    else
                    {
                        Color light = SystemColors.ControlLight;
                        Color lightLight = SystemColors.ControlLightLight;
 
                        int dr = light.R - lightLight.R;
                        int dg = light.G - lightLight.G;
                        int db = light.B - lightLight.B;
 
                        return Color.FromArgb((byte)(light.R - (byte)(dr * percLighter)),
                                              (byte)(light.G - (byte)(dg * percLighter)),
                                              (byte)(light.B - (byte)(db * percLighter)));
                    }
                }
                else
                {
                    int zeroLum = luminosity;
                    int oneLum = NewLuma(HilightAdj, true);
                    return ColorFromHLS(hue, zeroLum + (int)((oneLum - zeroLum) * percLighter), saturation);
                }
            }
 
            private int NewLuma(int n, bool scale)
            {
                return NewLuma(luminosity, n, scale);
            }
 
            private int NewLuma(int luminosity, int n, bool scale)
            {
                if (n == 0)
                    return luminosity;
 
                if (scale)
                {
                    if (n > 0)
                    {
                        return (int)(((int)luminosity * (1000 - n) + (Range + 1L) * n) / 1000);
                    }
                    else
                    {
                        return (int)(((int)luminosity * (n + 1000)) / 1000);
                    }
                }
 
                int newLum = luminosity;
                newLum += (int)((long)n * Range / 1000);
 
                if (newLum < 0)
                    newLum = 0;
                if (newLum > HLSMax)
                    newLum = HLSMax;
 
                return newLum;
            }
 
            private Color ColorFromHLS(int hue, int luminosity, int saturation)
            {
                byte r, g, b;                      /* RGB component values */
                int magic1, magic2;       /* calculated magic numbers (really!) */
 
                if (saturation == 0)
                {                /* achromatic case */
                    r = g = b = (byte)((luminosity * RGBMax) / HLSMax);
                    if (hue != Undefined)
                    {
                        /* ERROR */
                    }
                }
                else
                {                         /* chromatic case */
                    /* set up magic numbers */
                    if (luminosity <= (HLSMax / 2))
                        magic2 = (int)((luminosity * ((int)HLSMax + saturation) + (HLSMax / 2)) / HLSMax);
                    else
                        magic2 = luminosity + saturation - (int)(((luminosity * saturation) + (int)(HLSMax / 2)) / HLSMax);
                    magic1 = 2 * luminosity - magic2;
 
                    /* get RGB, change units from HLSMax to RGBMax */
                    r = (byte)(((HueToRGB(magic1, magic2, (int)(hue + (int)(HLSMax / 3))) * (int)RGBMax + (HLSMax / 2))) / (int)HLSMax);
                    g = (byte)(((HueToRGB(magic1, magic2, hue) * (int)RGBMax + (HLSMax / 2))) / HLSMax);
                    b = (byte)(((HueToRGB(magic1, magic2, (int)(hue - (int)(HLSMax / 3))) * (int)RGBMax + (HLSMax / 2))) / (int)HLSMax);
                }
                return Color.FromArgb(r, g, b);
            }
 
            private int HueToRGB(int n1, int n2, int hue)
            {
                /* range check: note values passed add/subtract thirds of range */
 
                /* The following is redundant for WORD (unsigned int) */
                if (hue < 0)
                    hue += HLSMax;
 
                if (hue > HLSMax)
                    hue -= HLSMax;
 
                /* return r,g, or b value from this tridrant */
                if (hue < (HLSMax / 6))
                    return (n1 + (((n2 - n1) * hue + (HLSMax / 12)) / (HLSMax / 6)));
                if (hue < (HLSMax / 2))
                    return (n2);
                if (hue < ((HLSMax * 2) / 3))
                    return (n1 + (((n2 - n1) * (((HLSMax * 2) / 3) - hue) + (HLSMax / 12)) / (HLSMax / 6)));
                else
                    return (n1);
 
            }
        }
    }
}