-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rgb.cs
1318 lines (1197 loc) · 42.2 KB
/
Rgb.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// A readonly struct to store colors in standard and linear red, green, blue
/// and alpha. Supports conversion to and from integers.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Explicit, Pack = 16)]
public readonly struct Rgb : IComparable<Rgb>, IEquatable<Rgb>
{
/// <summary>
/// The alpha (transparency) channel.
/// </summary>
[FieldOffset(0)] private readonly float a;
/// <summary>
/// The blue color channel.
/// </summary>
[FieldOffset(12)] private readonly float b;
/// <summary>
/// The green color channel.
/// </summary>
[FieldOffset(8)] private readonly float g;
/// <summary>
/// The red color channel.
/// </summary>
[FieldOffset(4)] private readonly float r;
/// <summary>
/// The alpha channel.
/// </summary>
/// <value>alpha</value>
public float Alpha { get { return this.a; } }
/// <summary>
/// The blue channel.
/// </summary>
/// <value>blue</value>
public float B { get { return this.b; } }
/// <summary>
/// The green channel.
/// </summary>
/// <value>green</value>
public float G { get { return this.g; } }
/// <summary>
/// The red channel.
/// </summary>
/// <value>red</value>
public float R { get { return this.r; } }
/// <summary>
/// Creates a color from unsigned bytes. Converts each to a single precision
/// real number in the range [0.0, 1.0].
/// </summary>
/// <param name="r">red channel</param>
/// <param name="g">green channel</param>
/// <param name="b">blue channel</param>
/// <param name="a">alpha channel</param>
public Rgb(in byte r, in byte g, in byte b, in byte a = 255)
{
this.r = r / 255.0f;
this.g = g / 255.0f;
this.b = b / 255.0f;
this.a = a / 255.0f;
}
/// <summary>
/// Creates a color from signed bytes. Converts each to a single precision
/// real number in the range [0.0, 1.0].
/// </summary>
/// <param name="r">red channel</param>
/// <param name="g">green channel</param>
/// <param name="b">blue channel</param>
/// <param name="a">alpha channel</param>
public Rgb(in sbyte r, in sbyte g, in sbyte b, in sbyte a = -1)
{
this.r = (r & 0xff) / 255.0f;
this.g = (g & 0xff) / 255.0f;
this.b = (b & 0xff) / 255.0f;
this.a = (a & 0xff) / 255.0f;
}
/// <summary>
/// Creates a color from single precision real numbers.
/// </summary>
/// <param name="r">red channel</param>
/// <param name="g">green channel</param>
/// <param name="b">blue channel</param>
/// <param name="a">alpha channel</param>
public Rgb(in float r, in float g, in float b, in float a = 1.0f)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
/// <summary>
/// Tests this color for equivalence with an object.
/// </summary>
/// <param name="value">the object</param>
/// <returns>equivalence</returns>
public override bool Equals(object value)
{
if (Object.ReferenceEquals(this, value)) { return true; }
if (value is null) { return false; }
if (value is Rgb clr) { return this.Equals(clr); }
return false;
}
/// <summary>
/// Returns a hash code representing this color.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
return (int)Rgb.ToHexArgb(this);
}
/// <summary>
/// Returns a string representation of this color.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Rgb.ToString(this);
}
/// <summary>
/// Compares this color to another in compliance with the IComparable
/// interface.
/// </summary>
/// <param name="c">comparisand</param>
/// <returns>evaluation</returns>
public int CompareTo(Rgb c)
{
uint left = Rgb.ToHexArgb(this);
uint right = Rgb.ToHexArgb(c);
return (left < right) ? -1 : (left > right) ? 1 : 0;
}
/// <summary>
/// Tests this color for equivalence with another in compliance with the
/// IEquatable interface.
/// </summary>
/// <param name="c">color</param>
/// <returns>equivalence</returns>
public bool Equals(Rgb c)
{
return Rgb.EqSatArith(this, c);
}
/// <summary>
/// Converts a color to a boolean by returning whether its alpha is greater
/// than zero.
/// </summary>
/// <param name="c">color</param>
/// <returns>evaluation</returns>
public static explicit operator bool(in Rgb c)
{
return Rgb.Any(c);
}
/// <summary>
/// A color evaluates to true if its alpha channel is greater than zero.
/// </summary>
/// <param name="c">color</param>
/// <returns>evaluation</returns>
public static bool operator true(in Rgb c)
{
return Rgb.Any(c);
}
/// <summary>
/// A color evaluates to false if its alpha channel is less than or equal
/// to zero.
/// </summary>
/// <param name="c">color</param>
/// <returns>evaluation</returns>
public static bool operator false(in Rgb c)
{
return Rgb.None(c);
}
/// <summary>
/// Converts a color to an integer, performs the bitwise not operation on
/// it, then converts the result to a color.
/// </summary>
/// <param name="c">color</param>
/// <returns>negated color</returns>
public static Rgb operator ~(in Rgb c)
{
return Rgb.FromHexArgb(~Rgb.ToHexArgb(c));
}
/// <summary>
/// Converts two colors to integers, performs the bitwise and operation on
/// them, then converts the result to a color.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>color</returns>
public static Rgb operator &(in Rgb a, in Rgb b)
{
return Rgb.FromHexArgb(Rgb.ToHexArgb(a) & Rgb.ToHexArgb(b));
}
/// <summary>
/// Converts two colors to integers, performs the bitwise inclusive or
/// operation on them, then converts the result to a color.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>color</returns>
public static Rgb operator |(in Rgb a, in Rgb b)
{
return Rgb.FromHexArgb(Rgb.ToHexArgb(a) | Rgb.ToHexArgb(b));
}
/// <summary>
/// Converts two colors to integers, performs the bitwise exclusive or
/// operation on them, then converts the result to a color.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>color</returns>
public static Rgb operator ^(in Rgb a, in Rgb b)
{
return Rgb.FromHexArgb(Rgb.ToHexArgb(a) ^ Rgb.ToHexArgb(b));
}
/// <summary>
/// Tests to see if all color channels are greater than zero.
/// </summary>
/// <param name="c">color</param>
/// <returns>evaluation</returns>
public static bool All(in Rgb c)
{
return (c.a > 0.0f) &&
(c.r > 0.0f) &&
(c.g > 0.0f) &&
(c.b > 0.0f);
}
/// <summary>
/// Tests to see if the alpha channel of the color is greater than zero,
/// i.e., if it has some opacity.
/// </summary>
/// <param name="c">color</param>
/// <returns>evaluation</returns>
public static bool Any(in Rgb c)
{
return c.a > 0.0f;
}
/// <summary>
/// Clamps a color to a lower and upper bound.
/// </summary>
/// <param name="c">color</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>the clamped color</returns>
public static Rgb Clamp(in Rgb c, in float lb = 0.0f, in float ub = 1.0f)
{
return new(
Utils.Clamp(c.r, lb, ub),
Utils.Clamp(c.g, lb, ub),
Utils.Clamp(c.b, lb, ub),
Utils.Clamp(c.a, lb, ub));
}
/// <summary>
/// Tests to see if a color contains a value.
/// </summary>
/// <param name="c">color</param>
/// <param name="v">value</param>
/// <returns>evaluation</returns>
public static bool Contains(in Rgb c, in float v)
{
return Utils.Approx(c.a, v) ||
Utils.Approx(c.b, v) ||
Utils.Approx(c.g, v) ||
Utils.Approx(c.r, v);
}
/// <summary>
/// Returns the first color argument with the alpha of the second.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>color</returns>
public static Rgb CopyAlpha(in Rgb a, in Rgb b)
{
return new(a.r, a.g, a.b, b.a);
}
/// <summary>
/// Checks if two colors have equivalent alpha channels when converted to
/// bytes in [0, 255]. Uses saturation arithmetic.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static bool EqAlphaSatArith(in Rgb a, in Rgb b)
{
return (int)(Utils.Clamp(a.a) * 255.0f + 0.5f) ==
(int)(Utils.Clamp(b.a) * 255.0f + 0.5f);
}
/// <summary>
/// Checks if two colors have equivalent red, green and blue channels when
/// converted to bytes in [0, 255]. Uses saturation arithmetic.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static bool EqRgbSatArith(in Rgb a, in Rgb b)
{
return (int)(Utils.Clamp(a.b) * 255.0f + 0.5f) ==
(int)(Utils.Clamp(b.b) * 255.0f + 0.5f) &&
(int)(Utils.Clamp(a.g) * 255.0f + 0.5f) ==
(int)(Utils.Clamp(b.g) * 255.0f + 0.5f) &&
(int)(Utils.Clamp(a.r) * 255.0f + 0.5f) ==
(int)(Utils.Clamp(b.r) * 255.0f + 0.5f);
}
/// <summary>
/// Checks if two colors have equivalent red, green, blue and alpha
/// channels when converted to bytes in [0, 255]. Uses saturation
/// arithmetic.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static bool EqSatArith(in Rgb a, in Rgb b)
{
return Rgb.EqAlphaSatArith(a, b) &&
Rgb.EqRgbSatArith(a, b);
}
/// <summary>
/// Converts a 3D vector to a color, as used in normal maps. If the
/// vector's magnitude is less than or equal to zero, returns the color
/// representation of up.
/// </summary>
/// <param name="v">vector</param>
/// <returns>color</returns>
public static Rgb FromDir(in Vec3 v)
{
float mSq = Vec3.MagSq(v);
if (mSq > 0.0f)
{
float mInv = 0.5f / MathF.Sqrt(mSq);
return new(
v.X * mInv + 0.5f,
v.Y * mInv + 0.5f,
v.Z * mInv + 0.5f,
1.0f);
}
return Rgb.Up;
}
/// <summary>
/// Converts a hexadecimal representation of a color into RGB.
/// </summary>
/// <param name="c">integer</param>
/// <param name="o">color channel order</param>
/// <returns>color</returns>
public static Rgb FromHex(in uint c, in RgbFormat o = RgbFormat.ARGB)
{
switch (o)
{
case RgbFormat.ABGR:
{ return Rgb.FromHexAbgr(c); }
case RgbFormat.RGBA:
{ return Rgb.FromHexRgba(c); }
case RgbFormat.ARGB:
default:
{ return Rgb.FromHexArgb(c); }
}
}
/// <summary>
/// Converts a hexadecimal representation of a color into RGB.
/// The unsigned integer is expected to be ordered as 0xAABBGGRR.
/// </summary>
/// <param name="c">integer</param>
/// <returns>color</returns>
public static Rgb FromHexAbgr(in uint c)
{
return new(
(c & 0xff) / 255.0f,
((c >> 0x08) & 0xff) / 255.0f,
((c >> 0x10) & 0xff) / 255.0f,
((c >> 0x18) & 0xff) / 255.0f);
}
/// <summary>
/// Converts a hexadecimal representation of a color into RGB.
/// The unsigned integer is expected to be ordered as 0xAARRGGBB.
/// </summary>
/// <param name="c">integer</param>
/// <returns>color</returns>
public static Rgb FromHexArgb(in uint c)
{
return new(
((c >> 0x10) & 0xff) / 255.0f,
((c >> 0x08) & 0xff) / 255.0f,
(c & 0xff) / 255.0f,
((c >> 0x18) & 0xff) / 255.0f);
}
/// <summary>
/// Converts a hexadecimal representation of a color into RGB.
/// The unsigned integer is expected to be ordered as 0xRRGGBBAA.
/// </summary>
/// <param name="c">integer</param>
/// <returns>color</returns>
public static Rgb FromHexRgba(in uint c)
{
return new(
((c >> 0x18) & 0xff) / 255.0f,
((c >> 0x10) & 0xff) / 255.0f,
((c >> 0x08) & 0xff) / 255.0f,
(c & 0xff) / 255.0f);
}
/// <summary>
/// Generates a 3D array of colors representing a grid in standard RGB.
/// </summary>
/// <param name="cols">number of columns</param>
/// <param name="rows">number of rows</param>
/// <param name="layers">number of layers</param>
/// <param name="alpha">alpha channel</param>
/// <returns>array</returns>
public static Rgb[,,] GridStandard(
in int cols = 8,
in int rows = 8,
in int layers = 8,
in float alpha = 1.0f)
{
int lVrf = layers < 1 ? 1 : layers;
int rVrf = rows < 1 ? 1 : rows;
int cVrf = cols < 1 ? 1 : cols;
bool oneLayer = lVrf == 1;
bool oneRow = rVrf == 1;
bool oneCol = cVrf == 1;
float hToStep = oneLayer ? 0.0f : 1.0f / (lVrf - 1.0f);
float iToStep = oneRow ? 0.0f : 1.0f / (rVrf - 1.0f);
float jToStep = oneCol ? 0.0f : 1.0f / (cVrf - 1.0f);
Rgb[,,] result = new Rgb[lVrf, rVrf, cVrf];
int rcVrf = rVrf * cVrf;
int len3 = lVrf * rcVrf;
for (int k = 0; k < len3; ++k)
{
int h = k / rcVrf;
int m = k - h * rcVrf;
int i = m / cVrf;
int j = m % cVrf;
result[h, i, j] = new(
j * jToStep,
i * iToStep,
h * hToStep,
alpha);
}
return result;
}
/// <summary>
/// Evaluates whether a color's red, green and blue channels are within the
/// range [0.0, 1.0].
/// </summary>
/// <param name="c">color</param>
/// <param name="eps">tolerance</param>
/// <returns>evaluation</returns>
public static bool IsInGamut(in Rgb c, in float eps = 0.0f)
{
float oneEps = 1.0f + eps;
return c.r >= -eps && c.r <= oneEps
&& c.g >= -eps && c.g <= oneEps
&& c.b >= -eps && c.b <= oneEps;
}
/// <summary>
/// Returns the relative luminance of the color.
/// Assumes the color is in linear RGB.
/// </summary>
/// <param name="c">color</param>
/// <returns>luminance</returns>
public static float LinearLuminance(in Rgb c)
{
return 0.21264935f * c.r +
0.71516913f * c.g +
0.07218152f * c.b;
}
/// <summary>
/// Converts a color from linear RGB to the XYZ
/// coordinates of SR LAB 2. See
/// See Jan Behrens, https://www.magnetkern.de/srlab2.html .
/// The alpha channel is unaffected by the transformation.
/// </summary>
/// <param name="c">linear color</param>
/// <returns>XYZ color</returns>
public static Vec4 LinearToSrXyz(in Rgb c)
{
double cr = c.r;
double cg = c.g;
double cb = c.b;
return new(
(float)(0.32053d * cr + 0.63692d * cg + 0.04256d * cb),
(float)(0.161987d * cr + 0.756636d * cg + 0.081376d * cb),
(float)(0.017228d * cr + 0.10866d * cg + 0.874112d * cb),
c.a);
}
/// <summary>
/// Converts a color from linear RGB to standard RGB (sRGB).
/// If the alpha flag is true, also transforms the alpha channel.
/// </summary>
/// <param name="c">linear color</param>
/// <param name="alpha">transform the alpha channel</param>
/// <returns>standard color</returns>
public static Rgb LinearToStandard(in Rgb c, in bool alpha = false)
{
const float inv24 = 1.0f / 2.4f;
return new(
c.r > 0.0031308f ?
MathF.Pow(c.r, inv24) * 1.055f - 0.055f :
c.r * 12.92f,
c.g > 0.0031308f ?
MathF.Pow(c.g, inv24) * 1.055f - 0.055f :
c.g * 12.92f,
c.b > 0.0031308f ?
MathF.Pow(c.b, inv24) * 1.055f - 0.055f :
c.b * 12.92f,
alpha ? c.a > 0.0031308f ?
MathF.Pow(c.a, inv24) * 1.055f - 0.055f :
c.a * 12.92f :
c.a);
}
/// <summary>
/// Mixes two colors by a step in the range [0.0, 1.0] .
/// Assumes the colors are in standard RGB; converts them to
/// linear, then mixes them, then converts to standard.
/// </summary>
/// <param name="o">origin color</param>
/// <param name="d">destination color</param>
/// <param name="t">step</param>
/// <returns>mixed color</returns>
public static Rgb MixRgbaLinear(in Rgb o, in Rgb d, in float t = 0.5f)
{
return Rgb.LinearToStandard(
Rgb.MixRgbaStandard(
Rgb.StandardToLinear(o),
Rgb.StandardToLinear(d), t));
}
/// <summary>
/// Mixes two colors by a step in the range [0.0, 1.0] .
/// Does not transform the colors in any way.
/// </summary>
/// <param name="o">origin color</param>
/// <param name="d">destination color</param>
/// <param name="t">step</param>
/// <returns>mixed color</returns>
public static Rgb MixRgbaStandard(in Rgb o, in Rgb d, in float t = 0.5f)
{
float u = 1.0f - t;
return new(
u * o.r + t * d.r,
u * o.g + t * d.g,
u * o.b + t * d.b,
u * o.a + t * d.a);
}
/// <summary>
/// Mixes two colors by a step in the range [0.0, 1.0] .
/// Converts each color from sRGB to CIE LAB, mixes according
/// to the step, then converts back to sRGB.
/// </summary>
/// <param name="o">origin color</param>
/// <param name="d">destination color</param>
/// <param name="t">step</param>
/// <returns>mixed color</returns>
public static Rgb MixSrLab2(in Rgb o, in Rgb d, in float t = 0.5f)
{
Lab oLab = Rgb.StandardToSrLab2(o);
Lab dLab = Rgb.StandardToSrLab2(d);
return Rgb.SrLab2ToStandard(Lab.Mix(oLab, dLab, t));
}
/// <summary>
/// Mixes two colors by a step in the range [0.0, 1.0] .
/// Converts each color from sRGB to CIE LCH, mixes according
/// to the step, then converts back to sRGB.
/// </summary>
/// <param name="o">origin color</param>
/// <param name="d">destination color</param>
/// <param name="t">step</param>
/// <returns>mixed color</returns>
public static Rgb MixSrLch(in Rgb o, in Rgb d, in float t = 0.5f)
{
return Rgb.MixSrLch(o, d, t,
(x, y, z, w) => Utils.LerpAngleNear(x, y, z, w));
}
/// <summary>
/// Mixes two colors by a step in the range [0.0, 1.0] .
/// Converts each color from sRGB to CIE LCH, mixes according
/// to the step, then converts back to sRGB.
/// The easing function is expected to ease from an origin
/// hue to a destination by a factor according to a range.
/// </summary>
/// <param name="o">origin color</param>
/// <param name="d">destination color</param>
/// <param name="t">step</param>
/// <param name="easing">easing function</param>
/// <returns>mixed color</returns>
public static Rgb MixSrLch(
in Rgb o,
in Rgb d,
in float t,
in Func<float, float, float, float, float> easing)
{
Lab oLab = Rgb.StandardToSrLab2(o);
float oa = oLab.A;
float ob = oLab.B;
float oChrSq = oa * oa + ob * ob;
Lab dLab = Rgb.StandardToSrLab2(d);
float da = dLab.A;
float db = dLab.B;
float dChrSq = da * da + db * db;
Lab cLab;
if (oChrSq < Utils.Epsilon || dChrSq < Utils.Epsilon)
{
cLab = Lab.Mix(oLab, dLab, t);
}
else
{
float oChr = MathF.Sqrt(oChrSq);
float oHue = Utils.OneTau * Utils.WrapRadians(
MathF.Atan2(ob, oa));
float dChr = MathF.Sqrt(dChrSq);
float dHue = Utils.OneTau * Utils.WrapRadians(
MathF.Atan2(db, da));
float u = 1.0f - t;
Lch cLch = new(
easing(oHue, dHue, t, 1.0f),
u * oChr + t * dChr,
u * oLab.L + t * dLab.L,
u * oLab.Alpha + t * dLab.Alpha);
cLab = Lab.FromLch(cLch);
}
return Rgb.SrLab2ToStandard(cLab);
}
/// <summary>
/// Tests to see if the alpha channel of this color is less than or equal to
/// zero, i.e., if it is completely transparent.
/// </summary>
/// <param name="c">color</param>
/// <returns>evaluation</returns>
public static bool None(in Rgb c)
{
return c.a <= 0.0f;
}
/// <summary>
/// Returns an opaque version of the color, i.e., where its alpha is 1.0.
/// </summary>
/// <param name="c">color</param>
/// <returns>opaque</returns>
public static Rgb Opaque(in Rgb c)
{
return new(c.r, c.g, c.b, 1.0f);
}
/// <summary>
/// Multiplies the red, green and blue color channels of a color by the
/// alpha channel. If alpha is less than or equal to zero, returns
/// clear black.
/// </summary>
/// <param name="c">color</param>
/// <returns>premultiplied color</returns>
public static Rgb Premul(in Rgb c)
{
if (c.a <= 0.0f) { return Rgb.ClearBlack; }
if (c.a >= 1.0f) { return Rgb.Opaque(c); }
return new(
c.r * c.a,
c.g * c.a,
c.b * c.a,
c.a);
}
/// <summary>
/// Reduces the signal, or granularity, of a color's channels.
/// </summary>
/// <param name="c">color</param>
/// <param name="levels">levels</param>
/// <returns>posterized color</returns>
public static Rgb Quantize(in Rgb c, in int levels)
{
return Rgb.QuantizeUnsigned(c, levels);
}
/// <summary>
/// Reduces the signal, or granularity, of a color's channels.
/// </summary>
/// <param name="c">color</param>
/// <param name="levels">levels</param>
/// <returns>posterized color</returns>
public static Rgb QuantizeUnsigned(in Rgb c, in int levels)
{
return Rgb.QuantizeUnsigned(c, levels, levels, levels, levels);
}
/// <summary>
/// Reduces the signal, or granularity, of a color's red, green and blue
/// channels. Does not alter the alpha channel.
/// </summary>
/// <param name="c">color</param>
/// <param name="rLevels">red levels</param>
/// <param name="gLevels">green levels</param>
/// <param name="bLevels">blue levels</param>
/// <returns>posterized color</returns>
public static Rgb QuantizeUnsigned(
in Rgb c,
in int rLevels,
in int gLevels,
in int bLevels)
{
return new(
Utils.QuantizeUnsigned(c.r, rLevels),
Utils.QuantizeUnsigned(c.g, gLevels),
Utils.QuantizeUnsigned(c.b, bLevels), c.a);
}
/// <summary>
/// Reduces the signal, or granularity, of a color's channels.
/// </summary>
/// <param name="c">color</param>
/// <param name="rLevels">red levels</param>
/// <param name="gLevels">green levels</param>
/// <param name="bLevels">blue levels</param>
/// <param name="aLevels">alpha levels</param>
/// <returns>posterized color</returns>
public static Rgb QuantizeUnsigned(
in Rgb c,
in int rLevels,
in int gLevels,
in int bLevels,
in int aLevels)
{
return new(
Utils.QuantizeUnsigned(c.r, rLevels),
Utils.QuantizeUnsigned(c.g, gLevels),
Utils.QuantizeUnsigned(c.b, bLevels),
Utils.QuantizeUnsigned(c.a, aLevels));
}
/// <summary>
/// Converts a color from SR LAB 2 to standard RGB (sRGB).
/// </summary>
/// <param name="lab">LAB color</param>
/// <returns>sRGB color</returns>
public static Rgb SrLab2ToStandard(in Lab lab)
{
return Rgb.LinearToStandard(
Rgb.SrXyzToLinear(
Lab.ToSrXyz(lab)));
}
/// <summary>
/// Converts a color from SR LCH to standard RGB (sRGB).
/// </summary>
/// <param name="lch">LCH color</param>
/// <returns>LAB color</returns>
public static Rgb SrLchToStandard(in Lch lch)
{
return Rgb.LinearToStandard(
Rgb.SrXyzToLinear(
Lab.ToSrXyz(
Lab.FromLch(lch))));
}
/// <summary>
/// Converts a color from SR LAB 2 XYZ to linear RGB.
/// The alpha channel is unaffected by the transformation.
/// </summary>
/// <param name="v">XYZ color</param>
/// <returns>linear color</returns>
public static Rgb SrXyzToLinear(in Vec4 v)
{
double vx = v.X;
double vy = v.Y;
double vz = v.Z;
return new(
(float)(5.435679d * vx - 4.599131d * vy + 0.163593d * vz),
(float)(-1.16809d * vx + 2.327977d * vy - 0.159798d * vz),
(float)(0.03784d * vx - 0.198564d * vy + 1.160644d * vz),
v.W);
}
/// <summary>
/// Returns the relative luminance of the color.
/// Converts the color from standard RGB to linear.
/// </summary>
/// <param name="c">color</param>
/// <returns>luminance</returns>
public static float StandardLuminance(in Rgb c)
{
return Rgb.LinearLuminance(Rgb.StandardToLinear(c));
}
/// <summary>
/// Converts a color from standard RGB (sRGB) to SR LAB 2.
/// </summary>
/// <param name="c">color</param>
/// <returns>LAB color</returns>
public static Lab StandardToSrLab2(in Rgb c)
{
return Lab.FromSrXyz(
Rgb.LinearToSrXyz(
Rgb.StandardToLinear(c)));
}
/// <summary>
/// Converts a color from standard RGB (sRGB) to SR LCH.
/// </summary>
/// <param name="c">color</param>
/// <returns>LCH color</returns>
public static Lch StandardToSrLch(in Rgb c)
{
return Lch.FromLab(
Lab.FromSrXyz(
Rgb.LinearToSrXyz(
Rgb.StandardToLinear(c))));
}
/// <summary>
/// Converts a color from standard RGB (sRGB) to linear RGB.
/// If the alpha flag is true, also transforms the alpha channel.
/// </summary>
/// <param name="c">the standard color</param>
/// <param name="alpha">transform the alpha channel</param>
/// <returns>linear color</returns>
public static Rgb StandardToLinear(in Rgb c, in bool alpha = false)
{
const float inv1_055 = 1.0f / 1.055f;
const float inv12_92 = 1.0f / 12.92f;
return new(
c.r > 0.04045f ?
MathF.Pow((c.r + 0.055f) * inv1_055, 2.4f) :
c.r * inv12_92,
c.g > 0.04045f ?
MathF.Pow((c.g + 0.055f) * inv1_055, 2.4f) :
c.g * inv12_92,
c.b > 0.04045f ?
MathF.Pow((c.b + 0.055f) * inv1_055, 2.4f) :
c.b * inv12_92,
alpha ? c.a > 0.04045f ?
MathF.Pow((c.a + 0.055f) * inv1_055, 2.4f) :
c.a * inv12_92 :
c.a);
}
/// <summary>
/// Converts a color to an integer.
/// </summary>
/// <param name="c">the input color</param>
/// <param name="order">color channel order</param>
/// <returns>hexadecimal color</returns>
public static uint ToHex(in Rgb c, in RgbFormat order = RgbFormat.ARGB)
{
switch (order)
{
case RgbFormat.ABGR:
{ return Rgb.ToHexAbgr(c); }
case RgbFormat.RGBA:
{ return Rgb.ToHexRgba(c); }
case RgbFormat.ARGB:
default:
{ return Rgb.ToHexArgb(c); }
}
}
/// <summary>
/// Converts a color to an integer. Returns an integer ordered as
/// 0xAABBGGRR.
/// </summary>
/// <param name="c">color</param>
/// <returns>hexadecimal color</returns>
public static uint ToHexAbgr(in Rgb c)
{
return Rgb.ToHexAbgrUnchecked(Rgb.Clamp(c, 0.0f, 1.0f));
}
/// <summary>
/// Converts a color to an integer. Returns an integer ordered as
/// 0xAARRGGBB.
/// </summary>
/// <param name="c">color</param>
/// <returns>hexadecimal color</returns>
public static uint ToHexArgb(in Rgb c)
{
return Rgb.ToHexArgbUnchecked(Rgb.Clamp(c, 0.0f, 1.0f));
}
/// <summary>
/// Converts a color to an integer. Returns an integer ordered as
/// 0xRRGGBBAA.
/// </summary>
/// <param name="c">color</param>
/// <returns>hexadecimal color</returns>
public static uint ToHexRgba(in Rgb c)
{
return Rgb.ToHexRgbaUnchecked(Rgb.Clamp(c, 0.0f, 1.0f));
}
/// <summary>
/// Converts a color to an integer. Does not check if the color's components
/// are in a valid range, [0.0, 1.0].
/// </summary>
/// <param name="c">color</param>
/// <param name="o">color channel order</param>
/// <returns>hexadecimal color</returns>
public static uint ToHexUnchecked(in Rgb c, in RgbFormat o = RgbFormat.ARGB)
{
switch (o)
{
case RgbFormat.ABGR:
{ return Rgb.ToHexAbgrUnchecked(c); }
case RgbFormat.RGBA:
{ return Rgb.ToHexRgbaUnchecked(c); }
case RgbFormat.ARGB:
default:
{ return Rgb.ToHexArgbUnchecked(c); }
}
}
/// <summary>
/// Converts a color to an integer. Does not check if the color's components
/// are in a valid range, [0.0, 1.0]. Returns an integer ordered as
/// 0xAABBGGRR.
/// </summary>
/// <param name="c">color</param>
/// <returns>hexadecimal color</returns>
public static uint ToHexAbgrUnchecked(in Rgb c)
{
return (uint)(c.a * 255.0f + 0.5f) << 0x18 |
(uint)(c.b * 255.0f + 0.5f) << 0x10 |
(uint)(c.g * 255.0f + 0.5f) << 0x08 |
(uint)(c.r * 255.0f + 0.5f);
}
/// <summary>
/// Converts a color to an integer. Does not check if the color's components
/// are in a valid range, [0.0, 1.0]. Returns an integer ordered as
/// 0xAARRGGBB.
/// </summary>
/// <param name="c">color</param>
/// <returns>hexadecimal color</returns>
public static uint ToHexArgbUnchecked(in Rgb c)
{
return (uint)(c.a * 255.0f + 0.5f) << 0x18 |
(uint)(c.r * 255.0f + 0.5f) << 0x10 |
(uint)(c.g * 255.0f + 0.5f) << 0x08 |
(uint)(c.b * 255.0f + 0.5f);
}
/// <summary>
/// Converts a color to an integer. Does not check if the color's components