-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathColors.cs
463 lines (388 loc) · 11.6 KB
/
Colors.cs
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Globalization;
using Windows.UI;
namespace Telegram.Common
{
public struct HSV
{
private double _h;
private double _s;
private double _v;
public HSV(double h, double s, double v)
{
_h = h;
_s = s;
_v = v;
}
public double H
{
get => _h;
set => _h = value;
}
public double S
{
get => _s;
set => _s = value;
}
public double V
{
get => _v;
set => _v = value;
}
public bool Equals(HSV hsv)
{
return (H == hsv.H) && (S == hsv.S) && (V == hsv.V);
}
public Color ToRGB(byte alpha = 255)
{
HSV hsv = this;
double r;
double g;
double b;
if (hsv.S == 0)
{
r = hsv.V;
g = hsv.V;
b = hsv.V;
}
else
{
int i;
double f, p, q, t;
if (hsv.H == 360)
{
hsv.H = 0;
}
else
{
hsv.H /= 60;
}
i = (int)Math.Truncate(hsv.H);
f = hsv.H - i;
p = hsv.V * (1.0 - hsv.S);
q = hsv.V * (1.0 - hsv.S * f);
t = hsv.V * (1.0 - hsv.S * (1.0 - f));
switch (i)
{
case 0:
r = hsv.V;
g = t;
b = p;
break;
case 1:
r = q;
g = hsv.V;
b = p;
break;
case 2:
r = p;
g = hsv.V;
b = t;
break;
case 3:
r = p;
g = q;
b = hsv.V;
break;
case 4:
r = t;
g = p;
b = hsv.V;
break;
default:
r = hsv.V;
g = p;
b = q;
break;
}
}
return Color.FromArgb(alpha, (byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
}
public struct HSL
{
private int _h;
private double _s;
private double _l;
public HSL(int h, double s, double l)
{
_h = h;
_s = s;
_l = l;
}
public int H
{
get => _h;
set => _h = value;
}
public double S
{
get => _s;
set => _s = value;
}
public double L
{
get => _l;
set => _l = value;
}
public bool Equals(HSL hsl)
{
return (H == hsl.H) && (S == hsl.S) && (L == hsl.L);
}
public Color ToRGB(byte alpha = 255)
{
var hsl = this;
byte r;
byte g;
byte b;
if (hsl.S == 0)
{
r = g = b = (byte)(hsl.L * 255);
}
else
{
double v1, v2;
double hue = (float)hsl.H / 360;
v2 = (hsl.L < 0.5) ? (hsl.L * (1 + hsl.S)) : (hsl.L + hsl.S - hsl.L * hsl.S);
v1 = 2 * hsl.L - v2;
r = (byte)(255 * HueToRGB(v1, v2, hue + 1.0f / 3));
g = (byte)(255 * HueToRGB(v1, v2, hue));
b = (byte)(255 * HueToRGB(v1, v2, hue - 1.0f / 3));
}
return Color.FromArgb(255, r, g, b);
}
private static double HueToRGB(double v1, double v2, double vH)
{
if (vH < 0)
{
vH += 1;
}
if (vH > 1)
{
vH -= 1;
}
if ((6 * vH) < 1)
{
return v1 + (v2 - v1) * 6 * vH;
}
if ((2 * vH) < 1)
{
return v2;
}
if ((3 * vH) < 2)
{
return v1 + (v2 - v1) * (2.0f / 3 - vH) * 6;
}
return v1;
}
}
public static class ColorEx
{
public static Color WithAlpha(this Color color, byte alpha)
{
return Color.FromArgb(alpha, color.R, color.G, color.B);
}
public static bool TryParse(string hex, out Color color)
{
if (int.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int value))
{
color = FromHex(value, false);
return true;
}
color = default;
return false;
}
public static Color FromHex(uint hexValue, bool allowDefault = false)
{
byte a = (byte)((hexValue & 0xff000000) >> 24);
byte r = (byte)((hexValue & 0x00ff0000) >> 16);
byte g = (byte)((hexValue & 0x0000ff00) >> 8);
byte b = (byte)(hexValue & 0x000000ff);
if (a == 0 && r == 0 && g == 0 && b == 0 && allowDefault)
{
return default;
}
return Color.FromArgb(a > 0 ? a : (byte)0xFF, r, g, b);
}
public static Color FromHex(int hexValue, bool allowDefault = false)
{
byte a = (byte)((hexValue & 0xff000000) >> 24);
byte r = (byte)((hexValue & 0x00ff0000) >> 16);
byte g = (byte)((hexValue & 0x0000ff00) >> 8);
byte b = (byte)(hexValue & 0x000000ff);
if (a == 0 && r == 0 && g == 0 && b == 0 && allowDefault)
{
return default;
}
return Color.FromArgb(a > 0 ? a : (byte)0xFF, r, g, b);
}
public static int ToHex(Color color)
{
return (color.A << 24) + (color.R << 16) + (color.G << 8) + color.B;
}
public static HSV ToHSV(this Color rgb)
{
double delta, min;
double h = 0, s, v;
min = Math.Min(Math.Min(rgb.R, rgb.G), rgb.B);
v = Math.Max(Math.Max(rgb.R, rgb.G), rgb.B);
delta = v - min;
if (v == 0.0)
{
s = 0;
}
else
{
s = delta / v;
}
if (s == 0)
{
h = 0.0;
}
else
{
if (rgb.R == v)
{
h = (rgb.G - rgb.B) / delta;
}
else if (rgb.G == v)
{
h = 2 + (rgb.B - rgb.R) / delta;
}
else if (rgb.B == v)
{
h = 4 + (rgb.R - rgb.G) / delta;
}
h *= 60;
if (h < 0.0)
{
h += 360;
}
}
return new HSV(h, s, v / 255);
}
public static HSL ToHSL(this Color rgb)
{
HSL hsl = new HSL();
float r = rgb.R / 255.0f;
float g = rgb.G / 255.0f;
float b = rgb.B / 255.0f;
float min = Math.Min(Math.Min(r, g), b);
float max = Math.Max(Math.Max(r, g), b);
float delta = max - min;
hsl.L = (max + min) / 2;
if (delta == 0)
{
hsl.H = 0;
hsl.S = 0.0f;
}
else
{
hsl.S = (hsl.L <= 0.5) ? (delta / (max + min)) : (delta / (2 - max - min));
float hue;
if (r == max)
{
hue = (g - b) / 6 / delta;
}
else if (g == max)
{
hue = 1.0f / 3 + (b - r) / 6 / delta;
}
else
{
hue = 2.0f / 3 + (r - g) / 6 / delta;
}
if (hue < 0)
{
hue += 1;
}
if (hue > 1)
{
hue -= 1;
}
hsl.H = (int)(hue * 360);
}
return hsl;
}
public static Color GetPatternColor(Color rgb, bool alwaysDark = false)
{
var hsb = rgb.ToHSV();
if (hsb.S > 0.0f || (hsb.V < 1.0f && hsb.V > 0.0f))
{
hsb.S = Math.Min(1.0f, hsb.S + (alwaysDark ? 0.15f : 0.05f) + 0.1f * (1.0f - hsb.S));
}
if (alwaysDark || hsb.V > 0.5f)
{
hsb.V = Math.Max(0.0f, hsb.V * 0.65f);
}
else
{
hsb.V = Math.Max(0.0f, Math.Min(1.0f, 1.0f - hsb.V * 0.65f));
}
var result = hsb.ToRGB();
return Color.FromArgb(0x64, result.R, result.G, result.B);
}
public static Color GetAverageColor(Color color1, Color color2)
{
double r1 = color1.R;
double r2 = color2.R;
double g1 = color1.G;
double g2 = color2.G;
double b1 = color1.B;
double b2 = color2.B;
return Color.FromArgb(255, (byte)(r1 / 2d + r2 / 2d), (byte)(g1 / 2d + g2 / 2d), (byte)(b1 / 2d + b2 / 2d));
}
public static Color GetAverageColor(int color1, int color2)
{
return GetAverageColor(color1.ToColor(), color2.ToColor());
}
public static Color GetAverageColor(Color color1, int color2)
{
return GetAverageColor(color1, color2.ToColor());
}
public static Color GetAverageColor(int color1, Color color2)
{
return GetAverageColor(color1.ToColor(), color2);
}
public static bool IsDark(int color1, int color2, int color3, int color4)
{
Color averageColor = GetAverageColor(color1, color2);
if (color3 != 0)
{
averageColor = GetAverageColor(averageColor, color3);
}
if (color4 != 0)
{
averageColor = GetAverageColor(averageColor, color4);
}
HSV hsb = averageColor.ToHSV();
return hsb.V < 0.3f;
}
public static Color WithBrightness(this Color color, float correctionFactor)
{
float red = (float)color.R;
float green = (float)color.G;
float blue = (float)color.B;
if (correctionFactor < 0)
{
correctionFactor = 1 + correctionFactor;
red *= correctionFactor;
green *= correctionFactor;
blue *= correctionFactor;
}
else
{
red = (255 - red) * correctionFactor + red;
green = (255 - green) * correctionFactor + green;
blue = (255 - blue) * correctionFactor + blue;
}
return Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
}
}
}