-
Notifications
You must be signed in to change notification settings - Fork 349
/
ColorConversionExtensions.shared.cs
470 lines (426 loc) · 23.6 KB
/
ColorConversionExtensions.shared.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
464
465
466
467
468
469
470
using System.Globalization;
namespace CommunityToolkit.Maui.Core.Extensions;
/// <summary>
/// Extension methods for Microsoft.Maui.Graphics.Color
/// </summary>
public static class ColorConversionExtensions
{
/// <summary>
/// Converts this <see cref="Color"/> to a <see cref="string"/> containing the red, green and blue components.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>
/// A <see cref="string"/> in the format: <c>RGB(red,green,blue)</c> where <b>red</b>, <b>green</b> and <b>blue</b> will be a value between 0 and 255.
/// (e.g. <c>RGB(255,0,0)</c> for <see cref="Colors.Red"/>).
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static string ToRgbString(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return $"RGB({color.GetByteRed()},{color.GetByteGreen()},{color.GetByteBlue()})";
}
/// <summary>
/// Converts this <see cref="Color"/> to a <see cref="string"/> containing the red, green, blue and alpha components.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <param name="cultureInfo">The <see cref="CultureInfo"/>.</param>
/// <returns>
/// A <see cref="string"/> in the format: <c>RGBA(red,green,blue,alpha)</c> where <b>red</b>, <b>green</b> and <b>blue</b> will be a value between 0 and 255,
/// and <b>alpha</b> is a value between 0 and 1. (e.g. <c>RGBA(255,0,0,1)</c> for <see cref="Colors.Red"/>).
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static string ToRgbaString(this Color color, CultureInfo? cultureInfo = null)
{
ArgumentNullException.ThrowIfNull(color);
return $"RGBA({color.GetByteRed()},{color.GetByteGreen()},{color.GetByteBlue()},{color.Alpha.ToString(cultureInfo)})";
}
/// <summary>
/// Converts this <see cref="Color"/> to a <see cref="string"/> containing the cyan, magenta, yellow and key components.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>
/// A <see cref="string"/> in the format: <c>CMYK(cyan,magenta,yellow,key)</c> where <b>cyan</b>, <b>magenta</b>, <b>yellow</b> and <b>key</b> will be a value between 0% and 100%.
/// (e.g. <c>CMYK(0%,100%,100%,0%)</c> for <see cref="Colors.Red"/>).
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static string ToCmykString(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return $"CMYK({color.GetPercentCyan():P0},{color.GetPercentMagenta():P0},{color.GetPercentYellow():P0},{color.GetPercentBlackKey():P0})";
}
/// <summary>
/// Converts this <see cref="Color"/> to a <see cref="string"/> containing the cyan, magenta, yellow, key and alpha components.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <param name="cultureInfo">The <see cref="CultureInfo"/>.</param>
/// <returns>
/// A <see cref="string"/> in the format: <c>CMYKA(cyan,magenta,yellow,key,alpha)</c> where <b>cyan</b>, <b>magenta</b>, <b>yellow </b>and <b>key</b> will be a value between
/// 0% and 100% and <b>alpha</b> will be a value between 0 and 1. (e.g. <c>CMYKA(100%,100%,0%,100%,1)</c> for <see cref="Colors.Red"/>).
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static string ToCmykaString(this Color color, CultureInfo? cultureInfo = null)
{
ArgumentNullException.ThrowIfNull(color);
return $"CMYKA({color.GetPercentCyan():P0},{color.GetPercentMagenta():P0},{color.GetPercentYellow():P0},{color.GetPercentBlackKey():P0},{color.Alpha.ToString(cultureInfo)})";
}
/// <summary>
/// Converts this <see cref="Color"/> to a <see cref="string"/> containing the hue, saturation and lightness components.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>
/// A <see cref="string"/> in the format: <c>HSL(hue,saturation,lightness)</c> where <b>hue</b> will be a value between 0 and 360, and <b>saturation</b> and <b>lightness</b>
/// will be a value between 0% and 100%. (e.g. <c>HSL(0,100%,50%)</c> for <see cref="Colors.Red"/>).
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static string ToHslString(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return $"HSL({color.GetDegreeHue():0},{color.GetSaturation():P0},{color.GetLuminosity():P0})";
}
/// <summary>
/// Converts this <see cref="Color"/> to a <see cref="string"/> containing the hue, saturation, lightness and alpha components.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <param name="cultureInfo">The <see cref="CultureInfo"/>.</param>
/// <returns>
/// A <see cref="string"/> in the format: <c>HSLA(hue,saturation,lightness,alpha)</c> where <b>hue</b> will be a value between 0 and 360, <b>saturation</b> and <b>lightness</b>
/// will be a value between 0% and 100%, and <b>alpha</b> will be a value between 0 and 1. (e.g. <c>HSLA(0,100%,50%,1)</c> for <see cref="Colors.Red"/>).
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static string ToHslaString(this Color color, CultureInfo? cultureInfo = null)
{
ArgumentNullException.ThrowIfNull(color);
return $"HSLA({color.GetDegreeHue():0},{color.GetSaturation():P0},{color.GetLuminosity():P0},{color.Alpha.ToString(cultureInfo)})";
}
/// <summary>
/// Applies the supplied <paramref name="redComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="redComponent">The red component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 1.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="redComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="redComponent"/> is <b>not</b> between 0 and 1.</exception>
public static Color WithRed(this Color color, double redComponent)
{
ArgumentNullException.ThrowIfNull(color);
return redComponent is < 0 or > 1
? throw new ArgumentOutOfRangeException(nameof(redComponent))
: Color.FromRgba(redComponent, color.Green, color.Blue, color.Alpha);
}
/// <summary>
/// Applies the supplied <paramref name="greenComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="greenComponent">The green component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 1.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="greenComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="greenComponent"/> is <b>not</b> between 0 and 1.</exception>
public static Color WithGreen(this Color color, double greenComponent)
{
ArgumentNullException.ThrowIfNull(color);
return greenComponent is < 0 or > 1
? throw new ArgumentOutOfRangeException(nameof(greenComponent))
: Color.FromRgba(color.Red, greenComponent, color.Blue, color.Alpha);
}
/// <summary>
/// Applies the supplied <paramref name="blueComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="blueComponent">The blue component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 1.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="blueComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="blueComponent"/> is <b>not</b> between 0 and 1.</exception>
public static Color WithBlue(this Color color, double blueComponent)
{
ArgumentNullException.ThrowIfNull(color);
return blueComponent is < 0 or > 1
? throw new ArgumentOutOfRangeException(nameof(blueComponent))
: Color.FromRgba(color.Red, color.Green, blueComponent, color.Alpha);
}
/// <summary>
/// Applies the supplied <paramref name="redComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="redComponent">The red component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 255.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="redComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color WithRed(this Color color, byte redComponent)
{
ArgumentNullException.ThrowIfNull(color);
return Color.FromRgba((double)redComponent / 255, color.Green, color.Blue, color.Alpha);
}
/// <summary>
/// Applies the supplied <paramref name="greenComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="greenComponent">The green component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 255.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="greenComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color WithGreen(this Color color, byte greenComponent)
{
ArgumentNullException.ThrowIfNull(color);
return Color.FromRgba(color.Red, (double)greenComponent / 255, color.Blue, color.Alpha);
}
/// <summary>
/// Applies the supplied <paramref name="blueComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="blueComponent">The blue component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 255.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="blueComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color WithBlue(this Color color, byte blueComponent)
{
ArgumentNullException.ThrowIfNull(color);
return Color.FromRgba(color.Red, color.Green, (double)blueComponent / 255, color.Alpha);
}
/// <summary>
/// Applies the supplied <paramref name="cyanComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="cyanComponent">The cyan component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 1.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="cyanComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color WithCyan(this Color color, double cyanComponent)
{
ArgumentNullException.ThrowIfNull(color);
return Color.FromRgba((1 - cyanComponent) * (1 - color.GetPercentBlackKey()),
(1 - color.GetPercentMagenta()) * (1 - color.GetPercentBlackKey()),
(1 - color.GetPercentYellow()) * (1 - color.GetPercentBlackKey()),
color.Alpha);
}
/// <summary>
/// Applies the supplied <paramref name="magentaComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="magentaComponent">The magenta component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 1.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="magentaComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color WithMagenta(this Color color, double magentaComponent)
{
ArgumentNullException.ThrowIfNull(color);
return Color.FromRgba((1 - color.GetPercentCyan()) * (1 - color.GetPercentBlackKey()),
(1 - magentaComponent) * (1 - color.GetPercentBlackKey()),
(1 - color.GetPercentYellow()) * (1 - color.GetPercentBlackKey()),
color.Alpha);
}
/// <summary>
/// Applies the supplied <paramref name="yellowComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="yellowComponent">The yellow component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 1.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="yellowComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color WithYellow(this Color color, double yellowComponent)
{
ArgumentNullException.ThrowIfNull(color);
return Color.FromRgba((1 - color.GetPercentCyan()) * (1 - color.GetPercentBlackKey()),
(1 - color.GetPercentMagenta()) * (1 - color.GetPercentBlackKey()),
(1 - yellowComponent) * (1 - color.GetPercentBlackKey()),
color.Alpha);
}
/// <summary>
/// Applies the supplied <paramref name="blackKeyComponent"/> to this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to modify.</param>
/// <param name="blackKeyComponent">The black key component to apply to the existing <see cref="Color"/>. Note this value must be between 0 and 1.</param>
/// <returns>A <see cref="Color"/> with the supplied <paramref name="blackKeyComponent"/> applied.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color WithBlackKey(this Color color, double blackKeyComponent)
{
ArgumentNullException.ThrowIfNull(color);
return Color.FromRgba((1 - color.GetPercentCyan()) * (1 - blackKeyComponent),
(1 - color.GetPercentMagenta()) * (1 - blackKeyComponent),
(1 - color.GetPercentYellow()) * (1 - blackKeyComponent),
color.Alpha);
}
/// <summary>
/// Gets the red component of <see cref="Color"/> as a value between 0 and 255.
/// </summary>
/// <param name="color">The <see cref="Color"/> to retrieve the component value from.</param>
/// <returns>The red component of <see cref="Color"/> as a value between 0 and 255.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static byte GetByteRed(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return ToByte(color.Red * 255);
}
/// <summary>
/// Gets the green component of <see cref="Color"/> as a value between 0 and 255.
/// </summary>
/// <param name="color">The <see cref="Color"/> to retrieve the component value from.</param>
/// <returns>The green component of <see cref="Color"/> as a value between 0 and 255.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static byte GetByteGreen(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return ToByte(color.Green * 255);
}
/// <summary>
/// Gets the blue component of <see cref="Color"/> as a value between 0 and 255.
/// </summary>
/// <param name="color">The <see cref="Color"/> to retrieve the component value from.</param>
/// <returns>The blue component of <see cref="Color"/> as a value between 0 and 255.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static byte GetByteBlue(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return ToByte(color.Blue * 255);
}
/// <summary>
/// Gets the alpha component of <see cref="Color"/> as a value between 0 and 255.
/// </summary>
/// <param name="color">The <see cref="Color"/> to retrieve the component value from.</param>
/// <returns>The alpha component of <see cref="Color"/> as a value between 0 and 255.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static byte GetByteAlpha(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return ToByte(color.Alpha * 255);
}
/// <summary>
/// Gets the hue component of <see cref="Color"/> as a value between 0 and 360.
/// </summary>
/// <param name="color">The <see cref="Color"/> to retrieve the component value from.</param>
/// <returns>The hue component of <see cref="Color"/> as a value between 0 and 360.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static double GetDegreeHue(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return color.GetHue() * 360;
}
/// <summary>
/// Gets the black key component of <see cref="Color"/> as a value between 0 and 1.
/// </summary>
/// <param name="color">The <see cref="Color"/> to retrieve the component value from.</param>
/// <returns>The black key component of <see cref="Color"/> as a value between 0 and 1.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static float GetPercentBlackKey(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return 1 - Math.Max(Math.Max(color.Red, color.Green), color.Blue);
}
/// <summary>
/// Gets the cyan component of <see cref="Color"/> as a value between 0 and 1.
/// </summary>
/// <param name="color">The <see cref="Color"/> to retrieve the component value from.</param>
/// <returns>The cyan component of <see cref="Color"/> as a value between 0 and 1.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static float GetPercentCyan(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return (1 - color.GetPercentBlackKey() is 0)
? 0
: (1 - color.Red - color.GetPercentBlackKey()) / (1 - color.GetPercentBlackKey());
}
/// <summary>
/// Gets the magenta component of <see cref="Color"/> as a value between 0 and 1.
/// </summary>
/// <param name="color">The <see cref="Color"/> to retrieve the component value from.</param>
/// <returns>The magenta component of <see cref="Color"/> as a value between 0 and 1.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static float GetPercentMagenta(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return (1 - color.GetPercentBlackKey() is 0)
? 0
: (1 - color.Green - color.GetPercentBlackKey()) / (1 - color.GetPercentBlackKey());
}
/// <summary>
/// Gets the yellow component of <see cref="Color"/> as a value between 0 and 1.
/// </summary>
/// <param name="color">The <see cref="Color"/> to retrieve the component value from.</param>
/// <returns>The yellow component of <see cref="Color"/> as a value between 0 and 1.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static float GetPercentYellow(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return (1 - color.GetPercentBlackKey() is 0)
? 0
: (1 - color.Blue - color.GetPercentBlackKey()) / (1 - color.GetPercentBlackKey());
}
/// <summary>
/// Inverts this <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to invert.</param>
/// <returns>The inverse of this <see cref="Color"/></returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color ToInverseColor(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return Color.FromRgb(1 - color.Red, 1 - color.Green, 1 - color.Blue);
}
/// <summary>
/// Converts this <see cref="Color"/> to a monochrome value of <see cref="Colors.Black"/> or <see cref="Colors.White"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>
/// This <see cref="Color"/> to a monochrome value of <see cref="Colors.Black"/> or <see cref="Colors.White"/>.
/// If the incoming <see cref="Color"/> is light, it will be converted to <see cref="Colors.White"/>.
/// If the incoming <see cref="Color"/> is dark, it will be converted to <see cref="Colors.Black"/>.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color ToBlackOrWhite(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return color.IsDark() ? Colors.Black : Colors.White;
}
/// <summary>
/// Converts this <see cref="Color"/> to a monochrome value of <see cref="Colors.Black"/> or <see cref="Colors.White"/> based on
/// whether this <see cref="Color"/> is determined as being dark for the human eye.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>
/// This <see cref="Color"/> to a monochrome value of <see cref="Colors.Black"/> or <see cref="Colors.White"/>.
/// If the incoming <see cref="Color"/> is light, it will be converted to <see cref="Colors.White"/>.
/// If the incoming <see cref="Color"/> is dark, it will be converted to <see cref="Colors.Black"/>.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color ToBlackOrWhiteForText(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return color.IsDarkForTheEye() ? Colors.White : Colors.Black;
}
/// <summary>
/// Converts this <see cref="Color"/> to a grayscale <see cref="Color"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert.</param>
/// <returns>This <see cref="Color"/> converted to a grayscale <see cref="Color"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static Color ToGrayScale(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
var avg = (color.Red + color.Blue + color.Green) / 3;
return Color.FromRgb(avg, avg, avg);
}
/// <summary>
/// Determines if this <see cref="Color"/> is dark for the human eye.
/// </summary>
/// <param name="color">The <see cref="Color"/> to check.</param>
/// <returns>
/// Whether this <see cref="Color"/> is dark for the human eye.
/// Returns <c>true</c> if the <see cref="Color"/> is determined to be dark, <c> false otherwise.</c></returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static bool IsDarkForTheEye(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return (color.GetByteRed() * 0.299) + (color.GetByteGreen() * 0.587) + (color.GetByteBlue() * 0.114) <= 186;
}
/// <summary>
/// Determines if this <see cref="Color"/> is dark.
/// </summary>
/// <param name="color">The <see cref="Color"/> to check.</param>
/// <returns>
/// Whether this <see cref="Color"/> is dark.
/// Returns <c>true</c> if the <see cref="Color"/> is determined to be dark, <c> false otherwise.</c></returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="color"/> is null.</exception>
public static bool IsDark(this Color color)
{
ArgumentNullException.ThrowIfNull(color);
return color.GetByteRed() + color.GetByteGreen() + color.GetByteBlue() <= 127 * 3;
}
static byte ToByte(float input)
{
var clampedInput = Math.Clamp(input, 0, 255);
return (byte)Math.Round(clampedInput);
}
}