/
ColorHelper.cs
370 lines (331 loc) · 12.5 KB
/
ColorHelper.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using System.Reflection;
using Windows.UI;
using Color = Windows.UI.Color;
namespace Microsoft.Toolkit.Uwp.Helpers
{
/// <summary>
/// This class provides static helper methods for colors.
/// </summary>
public static class ColorHelper
{
/// <summary>
/// Creates a <see cref="Color"/> from a XAML color string.
/// Any format used in XAML should work.
/// </summary>
/// <param name="colorString">The XAML color string.</param>
/// <returns>The created <see cref="Color"/>.</returns>
public static Color ToColor(this string colorString)
{
if (string.IsNullOrEmpty(colorString))
{
ThrowArgumentException();
}
if (colorString[0] == '#')
{
switch (colorString.Length)
{
case 9:
{
var cuint = Convert.ToUInt32(colorString.Substring(1), 16);
var a = (byte)(cuint >> 24);
var r = (byte)((cuint >> 16) & 0xff);
var g = (byte)((cuint >> 8) & 0xff);
var b = (byte)(cuint & 0xff);
return Color.FromArgb(a, r, g, b);
}
case 7:
{
var cuint = Convert.ToUInt32(colorString.Substring(1), 16);
var r = (byte)((cuint >> 16) & 0xff);
var g = (byte)((cuint >> 8) & 0xff);
var b = (byte)(cuint & 0xff);
return Color.FromArgb(255, r, g, b);
}
case 5:
{
var cuint = Convert.ToUInt16(colorString.Substring(1), 16);
var a = (byte)(cuint >> 12);
var r = (byte)((cuint >> 8) & 0xf);
var g = (byte)((cuint >> 4) & 0xf);
var b = (byte)(cuint & 0xf);
a = (byte)(a << 4 | a);
r = (byte)(r << 4 | r);
g = (byte)(g << 4 | g);
b = (byte)(b << 4 | b);
return Color.FromArgb(a, r, g, b);
}
case 4:
{
var cuint = Convert.ToUInt16(colorString.Substring(1), 16);
var r = (byte)((cuint >> 8) & 0xf);
var g = (byte)((cuint >> 4) & 0xf);
var b = (byte)(cuint & 0xf);
r = (byte)(r << 4 | r);
g = (byte)(g << 4 | g);
b = (byte)(b << 4 | b);
return Color.FromArgb(255, r, g, b);
}
default: return ThrowFormatException();
}
}
if (colorString.Length > 3 && colorString[0] == 's' && colorString[1] == 'c' && colorString[2] == '#')
{
var values = colorString.Split(',');
if (values.Length == 4)
{
var scA = double.Parse(values[0].Substring(3), CultureInfo.InvariantCulture);
var scR = double.Parse(values[1], CultureInfo.InvariantCulture);
var scG = double.Parse(values[2], CultureInfo.InvariantCulture);
var scB = double.Parse(values[3], CultureInfo.InvariantCulture);
return Color.FromArgb((byte)(scA * 255), (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255));
}
if (values.Length == 3)
{
var scR = double.Parse(values[0].Substring(3), CultureInfo.InvariantCulture);
var scG = double.Parse(values[1], CultureInfo.InvariantCulture);
var scB = double.Parse(values[2], CultureInfo.InvariantCulture);
return Color.FromArgb(255, (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255));
}
return ThrowFormatException();
}
var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(colorString);
if (prop != null)
{
return (Color)prop.GetValue(null);
}
return ThrowFormatException();
static void ThrowArgumentException() => throw new ArgumentException("The parameter \"colorString\" must not be null or empty.");
static Color ThrowFormatException() => throw new FormatException("The parameter \"colorString\" is not a recognized Color format.");
}
/// <summary>
/// Converts a <see cref="Color"/> to a hexadecimal string representation.
/// </summary>
/// <param name="color">The color to convert.</param>
/// <returns>The hexadecimal string representation of the color.</returns>
public static string ToHex(this Color color)
{
return $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}";
}
/// <summary>
/// Converts a <see cref="Color"/> to a premultiplied Int32 - 4 byte ARGB structure.
/// </summary>
/// <param name="color">The color to convert.</param>
/// <returns>The int representation of the color.</returns>
public static int ToInt(this Color color)
{
var a = color.A + 1;
var col = (color.A << 24) | ((byte)((color.R * a) >> 8) << 16) | ((byte)((color.G * a) >> 8) << 8) | (byte)((color.B * a) >> 8);
return col;
}
/// <summary>
/// Converts a <see cref="Color"/> to an <see cref="HslColor"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>The converted <see cref="HslColor"/>.</returns>
public static HslColor ToHsl(this Color color)
{
const double toDouble = 1.0 / 255;
var r = toDouble * color.R;
var g = toDouble * color.G;
var b = toDouble * color.B;
var max = Math.Max(Math.Max(r, g), b);
var min = Math.Min(Math.Min(r, g), b);
var chroma = max - min;
double h1;
if (chroma == 0)
{
h1 = 0;
}
else if (max == r)
{
// The % operator doesn't do proper modulo on negative
// numbers, so we'll add 6 before using it
h1 = (((g - b) / chroma) + 6) % 6;
}
else if (max == g)
{
h1 = 2 + ((b - r) / chroma);
}
else
{
h1 = 4 + ((r - g) / chroma);
}
double lightness = 0.5 * (max + min);
double saturation = chroma == 0 ? 0 : chroma / (1 - Math.Abs((2 * lightness) - 1));
HslColor ret;
ret.H = 60 * h1;
ret.S = saturation;
ret.L = lightness;
ret.A = toDouble * color.A;
return ret;
}
/// <summary>
/// Converts a <see cref="Color"/> to an <see cref="HsvColor"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>The converted <see cref="HsvColor"/>.</returns>
public static HsvColor ToHsv(this Color color)
{
const double toDouble = 1.0 / 255;
var r = toDouble * color.R;
var g = toDouble * color.G;
var b = toDouble * color.B;
var max = Math.Max(Math.Max(r, g), b);
var min = Math.Min(Math.Min(r, g), b);
var chroma = max - min;
double h1;
if (chroma == 0)
{
h1 = 0;
}
else if (max == r)
{
// The % operator doesn't do proper modulo on negative
// numbers, so we'll add 6 before using it
h1 = (((g - b) / chroma) + 6) % 6;
}
else if (max == g)
{
h1 = 2 + ((b - r) / chroma);
}
else
{
h1 = 4 + ((r - g) / chroma);
}
double saturation = chroma == 0 ? 0 : chroma / max;
HsvColor ret;
ret.H = 60 * h1;
ret.S = saturation;
ret.V = max;
ret.A = toDouble * color.A;
return ret;
}
/// <summary>
/// Creates a <see cref="Color"/> from the specified hue, saturation, lightness, and alpha values.
/// </summary>
/// <param name="hue">0..360 range hue</param>
/// <param name="saturation">0..1 range saturation</param>
/// <param name="lightness">0..1 range lightness</param>
/// <param name="alpha">0..1 alpha</param>
/// <returns>The created <see cref="Color"/>.</returns>
public static Color FromHsl(double hue, double saturation, double lightness, double alpha = 1.0)
{
if (hue < 0 || hue > 360)
{
throw new ArgumentOutOfRangeException(nameof(hue));
}
double chroma = (1 - Math.Abs((2 * lightness) - 1)) * saturation;
double h1 = hue / 60;
double x = chroma * (1 - Math.Abs((h1 % 2) - 1));
double m = lightness - (0.5 * chroma);
double r1, g1, b1;
if (h1 < 1)
{
r1 = chroma;
g1 = x;
b1 = 0;
}
else if (h1 < 2)
{
r1 = x;
g1 = chroma;
b1 = 0;
}
else if (h1 < 3)
{
r1 = 0;
g1 = chroma;
b1 = x;
}
else if (h1 < 4)
{
r1 = 0;
g1 = x;
b1 = chroma;
}
else if (h1 < 5)
{
r1 = x;
g1 = 0;
b1 = chroma;
}
else
{
r1 = chroma;
g1 = 0;
b1 = x;
}
byte r = (byte)(255 * (r1 + m));
byte g = (byte)(255 * (g1 + m));
byte b = (byte)(255 * (b1 + m));
byte a = (byte)(255 * alpha);
return Color.FromArgb(a, r, g, b);
}
/// <summary>
/// Creates a <see cref="Color"/> from the specified hue, saturation, value, and alpha values.
/// </summary>
/// <param name="hue">0..360 range hue</param>
/// <param name="saturation">0..1 range saturation</param>
/// <param name="value">0..1 range value</param>
/// <param name="alpha">0..1 alpha</param>
/// <returns>The created <see cref="Color"/>.</returns>
public static Color FromHsv(double hue, double saturation, double value, double alpha = 1.0)
{
if (hue < 0 || hue > 360)
{
throw new ArgumentOutOfRangeException(nameof(hue));
}
double chroma = value * saturation;
double h1 = hue / 60;
double x = chroma * (1 - Math.Abs((h1 % 2) - 1));
double m = value - chroma;
double r1, g1, b1;
if (h1 < 1)
{
r1 = chroma;
g1 = x;
b1 = 0;
}
else if (h1 < 2)
{
r1 = x;
g1 = chroma;
b1 = 0;
}
else if (h1 < 3)
{
r1 = 0;
g1 = chroma;
b1 = x;
}
else if (h1 < 4)
{
r1 = 0;
g1 = x;
b1 = chroma;
}
else if (h1 < 5)
{
r1 = x;
g1 = 0;
b1 = chroma;
}
else
{
r1 = chroma;
g1 = 0;
b1 = x;
}
byte r = (byte)(255 * (r1 + m));
byte g = (byte)(255 * (g1 + m));
byte b = (byte)(255 * (b1 + m));
byte a = (byte)(255 * alpha);
return Color.FromArgb(a, r, g, b);
}
}
}