image-canvas 0.5.1

A color-aware texel buffer.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
mod oklab;
mod srlab2;
mod transfer;
mod yuv;

use crate::color_matrix::{ColMatrix, RowMatrix};

/// Identifies a color representation.
///
/// This names the model by which the numbers in the channels relate to a physical model. How
/// exactly depends on the variant as presented below. Some of them can be customized further with
/// parameters.
///
/// Notably, there are _NOT_ the numbers which we will use in image operations. Generally, we will
/// use an associated _linear_ representation of those colors instead. The choice here depends on
/// the color and is documented for each variants. It is chosen to provide models for faithful
/// linear operations on these colors such as mixing etc.
///
/// TODO: colors describe _paths_ to linear display, so we should somehow implement direction
/// conversions such as "BT.2087 : Colour conversion from Recommendation ITU-R BT.709 to
/// Recommendation ITU-R BT.2020" in a separate manner.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Color {
    /// An rgb-ish, additive model based on the CIE 1931 XYZ observers.
    ///
    /// The _linear_ representation is the screen space linear RGB, which depends on primaries,
    /// whitepoint and reference luminance. It is derived from the encoded form through the
    /// transfer function.
    Rgb {
        primary: Primaries,
        transfer: Transfer,
        whitepoint: Whitepoint,
        luminance: Luminance,
    },
    /// A pure lightness color based on linear stimulus interpolation.
    ///
    /// This is a `Yuv` without reference to any particular differencing scheme for the two chroma
    /// channels.
    Luma {
        transfer: Transfer,
        whitepoint: Whitepoint,
        luminance: Luminance,
    },
    /// A lightness color based on transferred primary values.
    ///
    /// The advantage of this scheme is, in theory, we can convert between such a luma and its RGB
    /// representation efficiently in its encoded form. This made its use appealing in older
    /// television signals where the non-linearity conversion would have to be achieved by
    /// expensive processing while the mixing is a much simpler, even analog, possibility.
    ///
    /// In quantized signals you can also approximate the coefficients in this processing and still
    /// get correctly quantized results across the whole domain, without the non-linearity
    /// necessitating some form of splitting.
    ///
    /// This library does not reliably implement all optimizations that are possible. Please refer
    /// to test and benchmark coverage.
    ///
    /// Standards that define such a lightness scheme typically do so through a `Yuv` scheme. This
    /// color model only has an `L` component.
    #[cfg(any())] // Disabled until better test coverage and reference.
    LumaDigital {
        primary: Primaries,
        transfer: Transfer,
        whitepoint: Whitepoint,
        luminance: Luminance,
        // FIXME: do we need an argument describing how to derive coefficients from the given
        // primaries? We use the xYz transfer matrix directly but there may be a compensation for
        // the inaccurate dark colors from the transfer function. Uncertain how to represent this
        // in the type system. This question must be resolved before enabling.
    },
    /// A lightness, chroma difference scheme.
    ///
    /// For the `L` component, this is equivalent to either a `Luma` or `LumaDigital` model
    /// depending on the `Differencing` scheme used.
    ///
    /// This library does not reliably implement all conversion optimizations that are possible.
    /// Please refer to test and benchmark coverage.
    Yuv {
        primary: Primaries,
        whitepoint: Whitepoint,
        transfer: Transfer,
        luminance: Luminance,
        differencing: Differencing,
    },
    /// The simple but perceptual space Oklab by Björn Ottoson.
    ///
    /// The _linear_ representation of this color is Lab but its quantized components are may be
    /// either Lab or LCh.
    ///
    /// It's based on a combination of two linear transforms and one non-linear power-function
    /// between them. Coefficients of these transforms are based on optimization against matching
    /// pairs in the detailed CAM16 model, trying to predict the parameters in those pairs as
    /// precisely as possible. For details see [the post's derivation][derivation].
    ///
    /// Reference: <https://bottosson.github.io/posts/oklab/>
    ///
    /// [derivation]: https://bottosson.github.io/posts/oklab/#how-oklab-was-derived
    Oklab,
    /// A group of scalar values, with no assigned relation to physical quantities.
    ///
    /// The purpose of this color is to simplify the process of creating color ramps and sampling
    /// functions, which do not have any interpretation themselves but are just coefficients to be
    /// used somewhere else.
    ///
    /// The only `SampleParts` that are allowed to be paired with this are `XYZ`.
    ///
    /// Additionally, you might use the images created with this color as an input or an
    /// intermediate step of a `transmute` to create images with chosen values in the linear
    /// representation without the need to manually calculate their texel encoding.
    Scalars {
        /// The transfer to use for points, as if they are RGB-ish colors.
        /// You can simply use `Linear` if you do not want to encode and rgb texel.
        transfer: Transfer,
    },
    /// A LAB space based on contemporary perceptual understanding.
    ///
    /// > The newly defined SRLAB2 color model is a compromise between the simplicity of CIELAB and
    /// the correctness of CIECAM02.
    ///
    /// By combining whitepoint adaption in the (more) precise model of CIECAM02 while performing
    /// the transfer function in the cone response space, this achieves a good uniformity by
    /// simply modelling the human perception properly. It just leaves out the surround luminance
    /// model in the vastly more complex CIECAM02.
    ///
    /// This is lacking for HDR. This is because its based on L*ab which is inherently optimized
    /// for the small gamut of SDR. It's not constant luminance at exceedingly bright colors where
    /// ICtCp might provide a better estimate (compare ΔEITP, ITU-R Rec. BT.2124).
    ///
    /// Reference: <https://www.magnetkern.de/srlab2.html>
    SrLab2 { whitepoint: Whitepoint },
}

/// How to interpret channels as physical quantities.
///
/// Each color model consists of a set of color channels, each of which may occur or be omitted in
/// buffers using that model. Each model defines one canonical _channel order_. This is the order
/// they appear in within 'shader units' when pixels are decoded from texels.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ColorChannelModel {
    /// An additive model consisting of a redish, greenish, blueish channel.
    ///
    /// Not all models have truly red, green, or blue channels. More specifically we refer to any
    /// color representation that uses three observer functions (weight functions on the visible
    /// portion of the spectrum of light) and represents color as the simplex defined from mixing
    /// them.
    ///
    /// The most common, or nearly universal in computer imagery, choice is a linear combination of
    /// the three CIE XYZ standard observers at 2° center of vision.
    ///
    /// Example: sRGB, CIE XYZ.
    Rgb,
    /// A lightness model without any color representations.
    ///
    /// This is a one-dimensional color where all representable colors fall on the line segment
    /// between full darkness and the whitepoint. Any Rgb and Yuv can be reduced to this.
    Luma,
    /// A lightness, and two color difference components.
    ///
    /// Also sometimes called YUV but that is easily confused is the specific color model called
    /// 'YUV', a common analog encoding for several PAL systems (now outdated). Don't confuse with
    /// CIE Yuv (1960) or CIE L*u*v* which is different thing entirely. Yes, confusing.
    ///
    /// Based on an Rgb color spaces, with a linear transform to express the color in terms of a
    /// total luminance and the difference of blue, red luminance relative to the total one. The
    /// linear transform is most often applied to non-linear (aka. gamma pre-corrected, or
    /// electric) R'G'B' values but sometimes (Rec.709) such correct is applied after
    /// transformation. Coefficients differ between systems.
    ///
    /// As can be read from the terms, the intensity is given as a _photometric_ definition in
    /// terms of luminance and not as a perceptual 'lightness' which differentiates it from Lab/Lch
    /// as defined below.
    // TODO: figure out if we want to call ICtCp `Yuv`.. After all there is a non-linear transform
    // involved that is not evaluated independently for each channel. But we do not _need_ to add a
    // corresponding `Color` variant that captures all the models.
    // ICtCp
    Yuv,
    /// A lightness, and two chroma components.
    ///
    /// Differs from xyY spaces by a non-linear transform, commonly with the goal of generating
    /// perceptually uniform values. Example: CIE La*b*.
    ///
    /// The uniformity permits a perceptual distance metric as Euclidean distance, although this
    /// proves imprecise under in-depth investigation. Good for a decent estimate though.
    Lab,
    /// A lightness and two chroma components as polar coordinates.
    ///
    /// Polar transform of a Lab model. Example: Oklab
    Lch,
    /// A subtractive model consisting of fours inks defining absorbed colors.
    ///
    /// Example: ISO 2846 (Euroskala)
    Cmyk,
    // Deprecate as a joke?
    /// HSV (Hue, saturation, value).
    ///
    /// On closer inspection, a model that is neither physical nor perceptual nor based on
    /// correctness merits and its use should be strongly reconsidered in favor of a proper Lab-like
    /// color model. Please stop, please. <https://en.wikipedia.org/wiki/HSL_and_HSV#Disadvantages>
    Hsv,
    /// HSL (Hue, saturation, lightness).
    ///
    /// Careful, lightness means neither luminance nor perceptual lightness and is a mere
    /// arithmetic mean of color values. Some recommend using Luma (based on primary weights)
    /// instead but neglect to mention a specific standard. Really research what definition was
    /// used when the pixel color was computed. Good luck.
    ///
    /// On closer inspection, a model that is neither physical nor perceptual nor based on
    /// correctness merits and its use should be strongly reconsidered in favor of a proper Lab-like
    /// color model. Please stop, please. <https://en.wikipedia.org/wiki/HSL_and_HSV#Disadvantages>
    Hsl,
}

/// Describes a single channel from an image.
///
/// This can be thought of as an index into a vector of channels relating to a color. Combine with
/// a concrete [`ColorChannelModel`] for the canonical index in a 4-sample color representation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ColorChannel {
    /// The weight of the red primary.
    R,
    /// The weight of the green primary.
    G,
    /// The weight of the blue primary.
    B,
    /// A luminescence.
    /// Note that `YCbCr` will be composed of Luma and Cb, Cr. This avoids the gnarly overlap
    /// between it and `Y` as the standard observer (even though this Y is often used to define the
    /// Luma relative to standard illuminant).
    Luma,
    /// An alpha/translucence component.
    Alpha,
    /// Blue-channel difference.
    Cb,
    /// Red-channel difference.
    Cr,
    /// Lightness. Not to be confused with luminescence as this is perceptual.
    L,
    /// The component a (green/red) of a LAB color.
    LABa,
    /// The component b (green/red) of a LAB color.
    LABb,
    /// Chroma of a LAB color, polar distance, `hypot(a, b)`.
    C,
    /// Hue of a LAB based color, polar angle, `atan2(b, a).
    LABh,
    /// The first CIE standard observer.
    X,
    /// The second CIE standard observer.
    Y,
    /// The second CIE standard observer.
    Z,
    Scalar0,
    Scalar1,
    Scalar2,
}

/// Transfer functions from encoded chromatic samples to physical quantity.
///
/// Ignoring viewing environmental effects, this describes a pair of functions that are each others
/// inverse: An electro-optical transfer (EOTF) and opto-electronic transfer function (OETF) that
/// describes how scene lighting is encoded as an electric signal. These are applied to each
/// stimulus value.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
#[non_exhaustive]
pub enum Transfer {
    /// Non-linear electrical data of Bt.709
    Bt709,
    /// Non-linear electrical data of Bt.470 M/NTSC.
    ///
    /// Check if you meant to use BT.470 M/PAL instead, which is the more commonly used RGB color
    /// space 'ITU-R BT.470 - 625'. A pure gamma of `2.2`.
    Bt470M,
    /// Non-linear electrical data of Bt.470 PAL, SECAM, ….
    ///
    /// A pure gamma of `2.8`.
    Bt470,
    /// Non-linear electrical data of Bt.601
    Bt601,
    /// Non-linear electrical data of Smpte-240
    Smpte240,
    /// Linear color in display luminance.
    Linear,
    /// Non-linear electrical data of Srgb
    ///
    /// Technically, we're implementing scRGB since we handle negative primaries just well enough.
    Srgb,
    /// Non-linear electrical data of Bt2020 that was 10-bit quantized
    Bt2020_10bit,
    /// Non-linear electrical data of Bt2020 that was 12-bit quantized
    /// FIXME(color): not yet supported, panics on use.
    Bt2020_12bit,
    /// Non-linear electrical data of Smpte-2048
    Smpte2084,
    /// Another name for Smpte2084.
    /// FIXME(color): not yet supported, panics on use.
    Bt2100Pq,
    /// Non-linear electrical data of Bt2100 Hybrid-Log-Gamma.
    /// FIXME(color): not yet supported, panics on use.
    Bt2100Hlg,
    /// Linear color in scene luminance of Bt2100.
    /// This is perfect for an artistic composition pipeline. The rest of the type system will
    /// ensure this is not accidentally and unwittingly mixed with `Linear` but otherwise this is
    /// treated as `Linear`. You might always transmute.
    /// FIXME(color): not yet supported, panics on use.
    Bt2100Scene,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LightnessModel {
    /// Lightness is calculated in linear color space, then transferred.
    Linear,
    /// Lightness is calculated in transferred ('electrical') values.
    Digital,
}

/// The reference brightness of the color specification.
///
/// FIXME(color): scaling to reference luminance doesn't have an interface yet.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Luminance {
    /// 100cd/m².
    Sdr,
    /// 10_000cd/m².
    /// Known as high-dynamic range.
    Hdr,
    /// 160cd/m².
    AdobeRgb,
    /// 1000 nits, optimized for projector use.
    DciP3,
}
/// The relative stimuli of the three corners of a triangular RGBish gamut.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Primaries {
    /// The CIE XYZ 'primaries'.
    /// FIXME(color): does this really make sense?
    Xyz,
    /// First set of primaries specified in Bt/Rec.601.
    ///
    /// These are actually the same as in SMPTE240M.
    Bt601_525,
    /// Second set of primaries specified in Bt/Rec.601.
    Bt601_625,
    /// Primaries specified in Bt/Rec.709.
    Bt709,
    /// Primaries specified in SMPTE240-M.
    ///
    /// There are actually the same as BT.601.
    Smpte240,
    /// Primaries specified in Bt/Rec.2020.
    ///
    /// Also known as Wide Color Gamut.
    Bt2020,
    /// Primaries specified in Bt/Rec.2100.
    ///
    /// Also known as Wide Color Gamut. See Bt.2020.
    Bt2100,
}

/// The differencing scheme used in a Yuv construction.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Differencing {
    /// Rec BT.470 M/PAL differencing scheme for E_U and E_V, the naming origin for 'YUV'.
    /// FIXME: add YIQ proper, to add BT.470 M/NTSC?
    ///
    /// Note this same differencing scheme is used with different color primaries and whitepoints.
    /// With those shared with BT601_625 and D65 in more modern systems and a different one under
    /// illuminant C.
    Bt407MPal,
    /// The BT.470 M/PAL has a typo and, based on its parameters, we can derive a more accurate
    /// version than as what was published..
    Bt407MPalPrecise,
    /// Rec BT.601 luminance differencing.
    Bt601,
    /// Rec BT.601 luminance differencing, quantized with headroom.
    /// This is intended for analog use, not for digital images.
    Bt601Quantized,
    /// Rec BT.601 luminance differencing, quantized without headroom.
    ///
    /// Please tell the crate author where it's used but this makes it easy to quantize to 8-bit
    /// unsigned integers.
    Bt601FullSwing,
    /// Rec BT.709 luminance differencing.
    Bt709,
    /// Analog form
    Bt709Quantized,
    /// Rec BT.709 luminance differencing, quantized without headroom.
    /// Not technically an ITU BT recommendation, but introduced in h.264.
    Bt709FullSwing,

    // TODO: Rec. ITU-R BT.1361 = BT709 with a dash of questionable 'extended gamut quantization'.
    // Suppressed at (suppressed on 12/02/15) in favor of BT2020 (published xx/10/15).
    // But then again, it's referenced by EBU: https://tech.ebu.ch/docs/tech/tech3299.pdf
    // Turtles all the way down.
    /// Factors from analog SECAM standard.
    YDbDr,
    /// Rec BT.2020 luminance differencing.
    Bt2020,
    /// Rec BT.2100 luminance differencing.
    /// Same coefficients as the BT2020 scheme.
    Bt2100,
    /// Differencing scheme from YCoCb/ITU-T H.273.
    YCoCg,
}

#[non_exhaustive]
pub enum DifferencingYiq {
    /// Differencing scheme from NTSC in 1953, a rotated version of Yuv.
    Ntsc1953,
    /// Differencing scheme from NTSC SMPTE, a rotated version of Yuv.
    /// Also known as FCC NTSC.
    SmpteC,
}

/// The whitepoint/standard illuminant.
///
/// | Illuminant | X       | Y       | Z       |
/// |------------|---------|---------|---------|
/// | A          | 1.09850 | 1.00000 | 0.35585 |
/// | B          | 0.99072 | 1.00000 | 0.85223 |
/// | C          | 0.98074 | 1.00000 | 1.18232 |
/// | D50        | 0.96422 | 1.00000 | 0.82521 |
/// | D55        | 0.95682 | 1.00000 | 0.92149 |
/// | D65        | 0.95047 | 1.00000 | 1.08883 |
/// | D75        | 0.94972 | 1.00000 | 1.22638 |
/// | E          | 1.00000 | 1.00000 | 1.00000 |
/// | F2         | 0.99186 | 1.00000 | 0.67393 |
/// | F7         | 0.95041 | 1.00000 | 1.08747 |
/// | F11        | 1.00962 | 1.00000 | 0.64350 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Whitepoint {
    A,
    B,
    C,
    D50,
    D55,
    D65,
    D75,
    E,
    F2,
    F7,
    F11,
}

impl Color {
    pub const SRGB: Color = Color::Rgb {
        luminance: Luminance::Sdr,
        primary: Primaries::Bt709,
        transfer: Transfer::Srgb,
        whitepoint: Whitepoint::D65,
    };

    pub const SRGB_LUMA: Color = Color::Luma {
        luminance: Luminance::Sdr,
        transfer: Transfer::Srgb,
        whitepoint: Whitepoint::D65,
    };

    pub const RGB_ITU_BT2020: Color = Color::Rgb {
        luminance: Luminance::Sdr,
        primary: Primaries::Bt2020,
        transfer: Transfer::Bt2020_10bit,
        whitepoint: Whitepoint::D65,
    };

    pub const RGB_ITU_BT470_525: Color = Color::Rgb {
        luminance: Luminance::Sdr,
        primary: Primaries::Bt601_525,
        transfer: Transfer::Bt470,
        whitepoint: Whitepoint::D65,
    };

    pub const RGB_ITU_BT470_625: Color = Color::Rgb {
        luminance: Luminance::Sdr,
        primary: Primaries::Bt601_625,
        transfer: Transfer::Bt470,
        whitepoint: Whitepoint::D65,
    };

    pub const BT709_RGB: Color = Color::Rgb {
        luminance: Luminance::Sdr,
        primary: Primaries::Bt709,
        transfer: Transfer::Bt709,
        whitepoint: Whitepoint::D65,
    };

    #[allow(deprecated)]
    pub const BT709: Color = Color::Yuv {
        luminance: Luminance::Sdr,
        primary: Primaries::Bt709,
        transfer: Transfer::Bt709,
        whitepoint: Whitepoint::D65,
        differencing: Differencing::Bt709,
    };

    pub(crate) fn to_xyz_slice(&self, pixel: &[[f32; 4]], xyz: &mut [[f32; 4]]) {
        // We can do shared pre-processing.
        if let Color::Rgb {
            primary,
            transfer,
            whitepoint,
            luminance: _,
        } = self
        {
            let to_xyz = primary.to_xyz(*whitepoint);

            if let Some(eo_transfer) = transfer.to_optical_display_slice() {
                eo_transfer(pixel, xyz);

                for target_xyz in xyz {
                    let [r, g, b, a] = *target_xyz;
                    let [x, y, z] = to_xyz.mul_vec([r, g, b]);
                    *target_xyz = [x, y, z, a];
                }
            } else {
                for (target_xyz, src_pix) in xyz.iter_mut().zip(pixel) {
                    *target_xyz = transfer.to_optical_display(*src_pix);
                }

                for target_xyz in xyz {
                    let [r, g, b, a] = *target_xyz;
                    let [x, y, z] = to_xyz.mul_vec([r, g, b]);
                    *target_xyz = [x, y, z, a];
                }
            }

            return;
        } else if let Color::Luma {
            transfer,
            whitepoint,
            luminance: _,
        } = self
        {
            let [x, y, z] = whitepoint.to_xyz();

            // FIXME: really we'd like to only transfer idx 0 and 4.
            if let Some(eo_transfer) = transfer.to_optical_display_slice() {
                eo_transfer(pixel, xyz);

                for target_xyz in xyz {
                    let [l, _, _, a] = *target_xyz;
                    *target_xyz = [l * x, l * y, l * z, a];
                }
            } else {
                for (target_xyz, src_pix) in xyz.iter_mut().zip(pixel) {
                    let [l, _, _, a] = transfer.to_optical_display(*src_pix);
                    *target_xyz = [l * x, l * y, l * z, a];
                }
            }

            return;
        } else if let Color::Oklab {} = self {
            return oklab::to_xyz_slice(pixel, xyz);
        } else if let Color::SrLab2 { whitepoint } = self {
            return srlab2::to_xyz_slice(pixel, xyz, *whitepoint);
        }

        // Fallback path in all cases.
        for (src_pix, target_xyz) in pixel.iter().zip(xyz) {
            *target_xyz = self.to_xyz_once(*src_pix)
        }
    }

    pub(crate) fn from_xyz_slice(&self, xyz: &[[f32; 4]], pixel: &mut [[f32; 4]]) {
        if let Color::Rgb {
            primary,
            transfer,
            whitepoint,
            luminance: _,
        } = self
        {
            let from_xyz = primary.to_xyz(*whitepoint).inv();

            if let Some(oe_transfer) = transfer.from_optical_display_slice() {
                for (target_pix, src_xyz) in pixel.iter_mut().zip(xyz) {
                    let [x, y, z, a] = *src_xyz;
                    let [r, g, b] = from_xyz.mul_vec([x, y, z]);
                    *target_pix = [r, g, b, a];
                }

                oe_transfer(pixel);
            } else {
                for (target_pix, src_xyz) in pixel.iter_mut().zip(xyz) {
                    let [x, y, z, a] = *src_xyz;
                    let [r, g, b] = from_xyz.mul_vec([x, y, z]);
                    *target_pix = [r, g, b, a];
                }

                for target_pix in pixel {
                    *target_pix = transfer.from_optical_display(*target_pix);
                }
            }

            return;
        } else if let Color::Luma {
            transfer,
            whitepoint: _,
            luminance: _,
        } = self
        {
            if let Some(oe_transfer) = transfer.from_optical_display_slice() {
                for (target_pix, src_xyz) in pixel.iter_mut().zip(xyz) {
                    let [_, y, _, a] = *src_xyz;
                    *target_pix = [y, 0.0, 0.0, a];
                }

                oe_transfer(pixel);
            } else {
                for (target_pix, src_xyz) in pixel.iter_mut().zip(xyz) {
                    let [_, y, _, a] = *src_xyz;
                    *target_pix = transfer.from_optical_display([y, 0.0, 0.0, a]);
                }
            }
        } else if let Color::Oklab {} = self {
            return oklab::from_xyz_slice(xyz, pixel);
        } else if let Color::SrLab2 { whitepoint } = self {
            return srlab2::from_xyz_slice(xyz, pixel, *whitepoint);
        }

        for (target_pix, src_xyz) in pixel.iter_mut().zip(xyz) {
            *target_pix = self.from_xyz_once(*src_xyz)
        }
    }

    pub(crate) fn to_xyz_once(&self, value: [f32; 4]) -> [f32; 4] {
        match self {
            Color::Oklab => oklab::oklab_to_xyz(value),
            Color::Rgb {
                primary,
                transfer,
                whitepoint,
                luminance: _,
            } => {
                let [r, g, b, a] = transfer.to_optical_display(value);
                let to_xyz = primary.to_xyz(*whitepoint);
                let [x, y, z] = to_xyz.mul_vec([r, g, b]);
                [x, y, z, a]
            }
            Color::Luma {
                transfer,
                whitepoint,
                luminance: _,
            } => {
                let [l, _, _, a] = transfer.to_optical_display(value);
                let [x, y, z] = whitepoint.to_xyz();
                [l * x, l * y, l * z, a]
            }
            #[cfg(any())]
            Color::LumaDigital {
                primary,
                transfer,
                whitepoint,
                luminance: _,
            } => {
                let [l, _, _, a] = value;
                let to_xyz = primary.to_xyz(*whitepoint);
                let [r, g, b] = to_xyz.mul_vec([l, l, l]);
                let [r, g, b, a] = transfer.to_optical_display([r, g, b, a]);
                let [x, y, z] = to_xyz.mul_vec([r, g, b]);
                [x, y, z, a]
            }
            Color::Yuv {
                primary,
                transfer,
                whitepoint,
                luminance: _,
                differencing,
            } => {
                let mut yuv = value;

                yuv::to_rgb_slice(core::slice::from_mut(&mut yuv), *transfer, *differencing);

                let [r, g, b, a] = yuv;
                let from_xyz = primary.to_xyz(*whitepoint);
                let [x, y, z] = from_xyz.mul_vec([r, g, b]);
                [x, y, z, a]
            }
            Color::Scalars { transfer } => transfer.to_optical_display(value),
            Color::SrLab2 { whitepoint } => {
                let [x, y, z, a] = value;
                let [x, y, z] = srlab2::srlab_to_xyz([x, y, z], *whitepoint);
                [x, y, z, a]
            }
        }
    }

    pub(crate) fn from_xyz_once(&self, value: [f32; 4]) -> [f32; 4] {
        match self {
            Color::Oklab => oklab::oklab_from_xyz(value),
            Color::Rgb {
                primary,
                transfer,
                whitepoint,
                luminance: _,
            } => {
                let [x, y, z, a] = value;
                let from_xyz = primary.to_xyz(*whitepoint).inv();
                let [r, g, b] = from_xyz.mul_vec([x, y, z]);
                transfer.from_optical_display([r, g, b, a])
            }
            Color::Luma {
                transfer,
                whitepoint: _,
                luminance: _,
            } => {
                let [_, y, _, a] = value;
                transfer.from_optical_display([y, 0.0, 0.0, a])
            }
            #[cfg(any())]
            Color::LumaDigital {
                primary,
                transfer,
                whitepoint,
                luminance: _,
            } => {
                let [x, y, z, a] = value;
                let from_xyz = primary.to_xyz(*whitepoint).inv();
                let [r, g, b] = from_xyz.mul_vec([x, y, z]);
                let [r, g, b, a] = transfer.from_optical_display([r, g, b, a]);
                let [_, l, _] = primary.to_xyz(*whitepoint).mul_vec([r, g, b]);
                [l, 0.0, 0.0, a]
            }
            Color::Yuv {
                primary,
                transfer,
                whitepoint,
                luminance: _,
                differencing,
            } => {
                let [x, y, z, a] = value;
                let from_xyz = primary.to_xyz(*whitepoint).inv();
                let [r, g, b] = from_xyz.mul_vec([x, y, z]);
                let mut rgb = [r, g, b, a];

                yuv::from_rgb_slice(core::slice::from_mut(&mut rgb), *transfer, *differencing);

                rgb
            }
            Color::Scalars { transfer } => transfer.from_optical_display(value),
            Color::SrLab2 { whitepoint } => {
                let [x, y, z, a] = value;
                let [x, y, z] = srlab2::srlab_from_xyz([x, y, z], *whitepoint);
                [x, y, z, a]
            }
        }
    }

    pub fn model(&self) -> Option<ColorChannelModel> {
        Some(match self {
            Color::Rgb { .. } => ColorChannelModel::Rgb,
            Color::Luma { .. } => ColorChannelModel::Luma,
            #[cfg(any())]
            Color::LumaDigital { .. } => ColorChannelModel::Luma,
            Color::Oklab | Color::SrLab2 { .. } => ColorChannelModel::Lab,
            Color::Yuv { .. } => ColorChannelModel::Yuv,
            Color::Scalars { .. } => return None,
        })
    }
}

impl ColorChannelModel {
    pub const fn contains(self, channel: ColorChannel) -> bool {
        channel.in_model(self)
    }

    const _STATIC_ASSERTIONS: () = {
        fn _all_models(model: ColorChannelModel) {
            use ColorChannelModel::*;

            // Makes it obvious we have statically tested all models. At least assuming we didn't
            // fail to copy and paste.
            match model {
                Rgb => {
                    const _: () = {
                        assert!(Rgb.contains(ColorChannel::R));
                        assert!(Rgb.contains(ColorChannel::G));
                        assert!(Rgb.contains(ColorChannel::B));
                        assert!(Rgb.contains(ColorChannel::Alpha));
                    };
                }
                Luma => {
                    const _: () = {
                        assert!(Luma.contains(ColorChannel::Luma));
                        assert!(Luma.contains(ColorChannel::Alpha));
                    };
                }
                Yuv => {
                    const _: () = {
                        assert!(Yuv.contains(ColorChannel::Luma));
                        assert!(Yuv.contains(ColorChannel::Cb));
                        assert!(Yuv.contains(ColorChannel::Cr));
                        assert!(Yuv.contains(ColorChannel::Alpha));
                    };
                }
                Lab => {
                    const _: () = {
                        assert!(Lab.contains(ColorChannel::L));
                        assert!(Lab.contains(ColorChannel::LABa));
                        assert!(Lab.contains(ColorChannel::LABb));
                        assert!(Lab.contains(ColorChannel::Alpha));
                    };
                }
                ColorChannelModel::Lch => {
                    const _: () = {
                        // No other channels handled yet.
                        assert!(Lch.contains(ColorChannel::Alpha));
                    };
                }
                ColorChannelModel::Cmyk => {
                    const _: () = {
                        // No other channels handled yet.
                        assert!(!Cmyk.contains(ColorChannel::Alpha));
                    };
                }
                ColorChannelModel::Hsv => {
                    const _: () = {
                        // No other channels handled yet.
                        assert!(Lch.contains(ColorChannel::Alpha));
                    };
                }
                ColorChannelModel::Hsl => {
                    const _: () = {
                        // No other channels handled yet.
                        assert!(Lch.contains(ColorChannel::Alpha));
                    };
                }
            }
        }
    };
}

impl Transfer {
    /// Convert to optical (=linear) display intensity.
    ///
    /// The difference between display and scene light only matters for very recent HDR content,
    /// just regard it as electro-optical transfer application.
    pub(crate) fn to_optical_display(self, value: [f32; 4]) -> [f32; 4] {
        use self::transfer::*;

        let [r, g, b, a] = value;
        let rgb = [r, g, b];

        let [r, g, b] = match self {
            Transfer::Bt709 => rgb.map(transfer_eo_bt709),
            Transfer::Bt470M => rgb.map(transfer_eo_bt470m),
            Transfer::Bt470 => rgb.map(transfer_eo_bt470),
            Transfer::Bt601 => rgb.map(transfer_eo_bt601),
            Transfer::Smpte240 => rgb.map(transfer_eo_smpte240),
            Transfer::Linear => rgb,
            Transfer::Srgb => rgb.map(transfer_eo_srgb),
            Transfer::Bt2020_10bit => rgb.map(transfer_eo_bt2020_10b),
            Transfer::Bt2020_12bit => {
                // FIXME(color): implement.
                todo!()
            }
            Transfer::Smpte2084 => rgb.map(transfer_eo_smpte2084),
            Transfer::Bt2100Pq => {
                // FIXME(color): implement.
                todo!()
            }
            Transfer::Bt2100Hlg => {
                // FIXME(color): implement.
                todo!()
            }
            Transfer::Bt2100Scene => {
                // FIXME(color): implement.
                todo!()
            }
        };

        [r, g, b, a]
    }

    pub(crate) fn from_optical_display(self, value: [f32; 4]) -> [f32; 4] {
        use self::transfer::*;

        let [r, g, b, a] = value;
        let rgb = [r, g, b];

        let [r, g, b] = match self {
            Transfer::Bt709 => rgb.map(transfer_oe_bt709),
            Transfer::Bt470M => rgb.map(transfer_oe_bt470m),
            Transfer::Bt470 => rgb.map(transfer_oe_bt470),
            Transfer::Bt601 => rgb.map(transfer_oe_bt601),
            Transfer::Smpte240 => rgb.map(transfer_oe_smpte240),
            Transfer::Linear => rgb,
            Transfer::Srgb => rgb.map(transfer_oe_srgb),
            Transfer::Bt2020_10bit => rgb.map(transfer_oe_bt2020_10b),
            Transfer::Bt2020_12bit => {
                // FIXME(color): implement.
                todo!()
            }
            Transfer::Smpte2084 => rgb.map(transfer_oe_smpte2084),
            Transfer::Bt2100Pq => {
                // FIXME(color): implement.
                todo!()
            }
            Transfer::Bt2100Hlg => {
                // FIXME(color): implement.
                todo!()
            }
            Transfer::Bt2100Scene => {
                // FIXME(color): implement.
                todo!()
            }
        };

        [r, g, b, a]
    }

    pub(crate) fn to_optical_display_slice(self) -> Option<fn(&[[f32; 4]], &mut [[f32; 4]])> {
        macro_rules! optical_by_display {
            ($what:ident: $($pattern:pat => $transfer:path,)*) => {
                match $what {
                    $($pattern => return optical_by_display! {@ $transfer },)*
                    _ => return None,
                }
            };
            (@ $transfer:path) => {
                Some(|texels: &[[f32; 4]], pixels: &mut [[f32; 4]]| {
                    for (texel, target_pix) in texels.iter().zip(pixels) {
                        let [r, g, b, a] = *texel;
                        let [r, g, b] = [r, g, b].map($transfer);
                        *target_pix = [r, g, b, a];
                    }
                })
            };
        }

        if let Transfer::Linear = self {
            return Some(|x, y| y.copy_from_slice(x));
        }

        use self::transfer::*;
        optical_by_display!(self:
            Transfer::Bt709 => transfer_eo_bt709,
            Transfer::Bt470M => transfer_eo_bt470m,
            Transfer::Bt470 => transfer_eo_bt470,
            Transfer::Bt601 => transfer_eo_bt601,
            Transfer::Smpte240 => transfer_eo_smpte240,
            Transfer::Srgb => transfer_eo_srgb,
            Transfer::Bt2020_10bit => transfer_eo_bt2020_10b,
        );
    }

    pub(crate) fn to_optical_display_slice_inplace(self) -> Option<fn(&mut [[f32; 4]])> {
        macro_rules! optical_by_display {
            ($what:ident: $($pattern:pat => $transfer:path,)*) => {
                match $what {
                    $($pattern => return optical_by_display! {@ $transfer },)*
                    _ => return None,
                }
            };
            (@ $transfer:path) => {
                Some(|pixels: &mut [[f32; 4]]| {
                    for pix in pixels {
                        let [r, g, b, a] = *pix;
                        let [r, g, b] = [r, g, b].map($transfer);
                        *pix = [r, g, b, a];
                    }
                })
            };
        }

        if let Transfer::Linear = self {
            return Some(|_| {});
        }

        use self::transfer::*;
        optical_by_display!(self:
            Transfer::Bt709 => transfer_eo_bt709,
            Transfer::Bt470M => transfer_eo_bt470m,
            Transfer::Bt470 => transfer_eo_bt470,
            Transfer::Bt601 => transfer_eo_bt601,
            Transfer::Smpte240 => transfer_eo_smpte240,
            Transfer::Srgb => transfer_eo_srgb,
            Transfer::Bt2020_10bit => transfer_eo_bt2020_10b,
        );
    }

    pub(crate) fn from_optical_display_slice(self) -> Option<fn(&mut [[f32; 4]])> {
        macro_rules! optical_by_display {
            ($what:ident: $($pattern:pat => $transfer:path,)*) => {
                match $what {
                    $($pattern => return optical_by_display! {@ $transfer },)*
                    _ => return None,
                }
            };
            (@ $transfer:path) => {
                Some(|pixels: &mut [[f32; 4]]| {
                    for target_pix in pixels.iter_mut() {
                        let [r, g, b, a] = *target_pix;
                        let [r, g, b] = [r, g, b].map($transfer);
                        *target_pix = [r, g, b, a];
                    }
                })
            };
        }

        if let Transfer::Linear = self {
            return Some(|_| {});
        }

        use self::transfer::*;
        optical_by_display!(self:
            Transfer::Bt709 => transfer_oe_bt709,
            Transfer::Bt470M => transfer_oe_bt470m,
            Transfer::Bt601 => transfer_oe_bt601,
            Transfer::Smpte240 => transfer_oe_smpte240,
            Transfer::Srgb => transfer_oe_srgb,
            Transfer::Bt2020_10bit => transfer_oe_bt2020_10b,
        );
    }
}

impl Whitepoint {
    pub fn to_xyz(self) -> [f32; 3] {
        use Whitepoint::*;
        match self {
            A => [1.09850, 1.00000, 0.35585],
            B => [0.99072, 1.00000, 0.85223],
            C => [0.98074, 1.00000, 1.18232],
            D50 => [0.96422, 1.00000, 0.82521],
            D55 => [0.95682, 1.00000, 0.92149],
            D65 => [0.95047, 1.00000, 1.08883],
            D75 => [0.94972, 1.00000, 1.22638],
            E => [1.00000, 1.00000, 1.00000],
            F2 => [0.99186, 1.00000, 0.67393],
            F7 => [0.95041, 1.00000, 1.08747],
            F11 => [1.00962, 1.00000, 0.64350],
        }
    }
}

#[rustfmt::skip]
impl Primaries {
    /// Convert to XYZ, or back if you invert the matrix.
    ///
    /// This is done with the 'wrong' van Kries transform, under given illuminant, where the CIE
    /// XYZ are scaled to match the whitepoint individually. This is in accordance to the
    /// specification for sRGB et.al even though it isn't very correct in a perceptual sense.
    ///
    /// See: Mark D. Fairchild, Color Appearance Models, 2nd Edition,
    /// Or: SRLAB2 <https://www.magnetkern.de/srlab2.html> for a color model that is perceptually
    /// more correct with regards to illuminants, or the complex CIECAM02.
    pub(crate) fn to_xyz(self, white: Whitepoint) -> RowMatrix {
        use Primaries::*;
        // Rec.BT.601
        // https://en.wikipedia.org/wiki/Color_spaces_with_RGB_primaries#Specifications_with_RGB_primaries
        let xy: [[f32; 2]; 3] = match self {
            Bt601_525 | Smpte240 => [[0.63, 0.34], [0.31, 0.595], [0.155, 0.07]],
            Bt601_625 => [[0.64, 0.33], [0.29, 0.6], [0.15, 0.06]],
            Bt709 => [[0.64, 0.33], [0.30, 0.60], [0.15, 0.06]],
            Bt2020 | Bt2100 => [[0.708, 0.292], [0.170, 0.797], [0.131, 0.046]],
            Xyz => todo!(),
        };

        // A column of CIE XYZ intensities for that primary.
        let xyz = |[x, y]: [f32; 2]| {
            [x / y, 1.0, (1.0 - x - y)/y]
        };

        let xyz_r = xyz(xy[0]);
        let xyz_g = xyz(xy[1]);
        let xyz_b = xyz(xy[2]);

        // Virtually, N = [xyz_r | xyz_g | xyz_b]
        // As the unweighted conversion matrix for:
        //  XYZ = N · RGB
        let n1 = ColMatrix([xyz_r, xyz_g, xyz_b]).inv();

        // http://www.brucelindbloom.com/index.html
        let w = white.to_xyz();

        // s is the weights that give the whitepoint when converted to xyz.
        // That is we're solving:
        //  W = N · S
        let s = n1.mul_vec(w);

        RowMatrix([
            s[0]*xyz_r[0], s[1]*xyz_g[0], s[2]*xyz_b[0],
            s[0]*xyz_r[1], s[1]*xyz_g[1], s[2]*xyz_b[1],
            s[0]*xyz_r[2], s[1]*xyz_g[2], s[2]*xyz_b[2],
        ])
    }

    pub fn to_xyz_row_matrix(self, white: Whitepoint) -> [f32; 9] {
        self.to_xyz(white).into_inner()
    }

    pub fn from_xyz_row_matrix(self, white: Whitepoint) -> [f32; 9] {
        self.to_xyz(white).inv().into_inner()
    }
}

#[test]
fn inverse() {
    const RGBA: [f32; 4] = [1.0, 1.0, 0.0, 1.0];
    let color = Color::SRGB;
    let _rgba = color.from_xyz_once(color.to_xyz_once(RGBA));
}

#[test]
fn xyz_once_equal_to_multiple() {
    const POINTS: &[[f32; 4]] = &[
        [0.0, 0.0, 0.0, 1.0],
        [1.0, 0.0, 0.0, 1.0],
        [0.0, 1.0, 0.0, 1.0],
        [0.0, 0.0, 1.0, 1.0],
        [0.5, 0.5, 0.5, 1.0],
        [0.8, 0.5, 0.5, 1.0],
        [0.5, 0.8, 0.5, 1.0],
        [0.5, 0.5, 0.8, 1.0],
        [0.8, 0.5, 0.8, 1.0],
        [0.8, 0.8, 0.5, 1.0],
        [0.5, 0.8, 0.8, 1.0],
        [0.8, 0.5, 0.8, 0.4],
        [0.8, 0.8, 0.5, 0.4],
        [0.5, 0.8, 0.8, 0.4],
    ];

    const COLORS: &[Color] = &[
        Color::SRGB,
        Color::RGB_ITU_BT2020,
        Color::RGB_ITU_BT470_525,
        Color::RGB_ITU_BT470_625,
        Color::BT709_RGB,
        Color::BT709,
    ];

    let mut xyz_buffer = [[0.0; 4]; 1];
    let mut pbuf = [0.0; 4];

    for color in COLORS {
        for &point in POINTS {
            let xyz = color.to_xyz_once(point);
            color.to_xyz_slice(core::slice::from_ref(&point), &mut xyz_buffer);

            assert_eq!(
                xyz_buffer[0], xyz,
                "Color {:?} failed for point {:?}",
                color, point
            );

            color.from_xyz_slice(&xyz_buffer, core::slice::from_mut(&mut pbuf));
            let backpoint = color.from_xyz_once(xyz);

            assert_eq!(
                backpoint, pbuf,
                "Color {:?} failed for point {:?} XYZ {xyz:?}",
                color, point
            );
        }
    }
}

#[test]
fn xyz_test_vectors() {
    struct TestCase {
        color: Color,
        points: &'static [([f32; 3], [f32; 3])],
        name: &'static str,
    }

    fn somewhat_eq(a: [f32; 3], b: [f32; 3]) -> bool {
        // FIXME: would be great if we could qualify exactly where we are allowed to be how
        // imprecise but this involves matrix multiplications. Should we switch to some compensated
        // summation or an exact sum?
        a.iter().zip(b).all(|(x, y)| (x - y).abs() < 0.001)
    }

    fn test(case: TestCase) {
        let check_color_pair = |rgb: [f32; 3], result: [f32; 3]| {
            let [r, g, b] = rgb;
            let [x, y, z, _] = case.color.to_xyz_once([r, g, b, 1.0]);

            assert!(
                somewhat_eq([x, y, z], result),
                "{:?} - {:?} at ({})",
                [x, y, z],
                result,
                case.name
            );
        };

        for (lhs, rhs) in case.points {
            check_color_pair(*lhs, *rhs);
        }
    }

    let cases = [
        // As generated by:
        // ```python
        // import colour
        // colour.RGB_to_XYZ(
        //  colour.RGB_COLOURSPACES['sRGB'],
        //  [1.0, 1.0, 0.0],
        //  apply_cctf_decoding=True,
        // ) * 255
        // ```
        TestCase {
            color: Color::SRGB,
            points: &[
                // array([ 0.74843538,  0.78741229,  0.85749198])
                ([0.9, 0.9, 0.9], [0.74843538, 0.78741229, 0.85749198]),
            ],
            name: "sRGB",
        },
    ];

    cases.into_iter().for_each(test);
}