azul-css 0.0.10

Common datatypes used for styling applications using the Azul desktop GUI framework
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
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
//! CSS color types and parser.
//!
//! Core types: [`ColorU`] (u8 RGBA), [`ColorF`] (f32 RGBA), [`ColorOrSystem`]
//! (concrete color or runtime system-theme reference). The parser supports hex,
//! `rgb()`/`rgba()`, `hsl()`/`hsla()`, CSS named colors, and `system:*` syntax.

use alloc::string::{String, ToString};
use core::fmt;
use crate::corety::AzString;
use crate::props::basic::error::{ParseFloatError, ParseIntError};

use crate::{
    impl_option,
    props::basic::{
        direction::{
            parse_direction, CssDirectionParseError, CssDirectionParseErrorOwned, Direction,
        },
        length::{PercentageParseError, PercentageValue},
    },
};

/// Round-saturating `f32` → `u8` for colour channels. Rust's `as u8` already
/// saturates a float (NaN→0, negatives→0, >255→255, otherwise truncates toward
/// zero), so this is behaviour-preserving; it just names the intent and isolates
/// the one unavoidable float→int cast (there is no infallible `f32`→`u8` in std).
#[inline]
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
const fn channel_to_u8(v: f32) -> u8 {
    v as u8
}

/// u8-based color, range 0 to 255 (similar to webrenders `ColorU`)
#[derive(Debug, Copy, Clone, PartialEq, Ord, PartialOrd, Eq, Hash)]
#[repr(C)]
pub struct ColorU {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

impl_option!(
    ColorU,
    OptionColorU,
    [Debug, Copy, Clone, PartialEq, Ord, PartialOrd, Eq, Hash]
);

impl Default for ColorU {
    fn default() -> Self {
        Self::BLACK
    }
}

impl fmt::Display for ColorU {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "rgba({}, {}, {}, {})",
            self.r,
            self.g,
            self.b,
            f32::from(self.a) / 255.0
        )
    }
}

// Colour math keeps explicit `a*b + c` rather than `mul_add`: the latter is a
// software `fmaf` (slower) without target `+fma` and changes results bit-for-bit.
#[allow(clippy::suboptimal_flops)]
impl ColorU {
    pub const ALPHA_TRANSPARENT: u8 = 0;
    pub const ALPHA_OPAQUE: u8 = 255;
    pub const RED: Self = Self {
        r: 255,
        g: 0,
        b: 0,
        a: Self::ALPHA_OPAQUE,
    };
    pub const GREEN: Self = Self {
        r: 0,
        g: 255,
        b: 0,
        a: Self::ALPHA_OPAQUE,
    };
    pub const BLUE: Self = Self {
        r: 0,
        g: 0,
        b: 255,
        a: Self::ALPHA_OPAQUE,
    };
    pub const WHITE: Self = Self {
        r: 255,
        g: 255,
        b: 255,
        a: Self::ALPHA_OPAQUE,
    };
    pub const BLACK: Self = Self {
        r: 0,
        g: 0,
        b: 0,
        a: Self::ALPHA_OPAQUE,
    };
    pub const TRANSPARENT: Self = Self {
        r: 0,
        g: 0,
        b: 0,
        a: Self::ALPHA_TRANSPARENT,
    };

    // Additional common colors
    pub const YELLOW: Self = Self { r: 255, g: 255, b: 0, a: Self::ALPHA_OPAQUE };
    pub const CYAN: Self = Self { r: 0, g: 255, b: 255, a: Self::ALPHA_OPAQUE };
    pub const MAGENTA: Self = Self { r: 255, g: 0, b: 255, a: Self::ALPHA_OPAQUE };
    pub const ORANGE: Self = Self { r: 255, g: 165, b: 0, a: Self::ALPHA_OPAQUE };
    pub const PINK: Self = Self { r: 255, g: 192, b: 203, a: Self::ALPHA_OPAQUE };
    pub const PURPLE: Self = Self { r: 128, g: 0, b: 128, a: Self::ALPHA_OPAQUE };
    pub const BROWN: Self = Self { r: 139, g: 69, b: 19, a: Self::ALPHA_OPAQUE };
    pub const GRAY: Self = Self { r: 128, g: 128, b: 128, a: Self::ALPHA_OPAQUE };
    pub const LIGHT_GRAY: Self = Self { r: 211, g: 211, b: 211, a: Self::ALPHA_OPAQUE };
    pub const DARK_GRAY: Self = Self { r: 64, g: 64, b: 64, a: Self::ALPHA_OPAQUE };
    pub const NAVY: Self = Self { r: 0, g: 0, b: 128, a: Self::ALPHA_OPAQUE };
    pub const TEAL: Self = Self { r: 0, g: 128, b: 128, a: Self::ALPHA_OPAQUE };
    pub const OLIVE: Self = Self { r: 128, g: 128, b: 0, a: Self::ALPHA_OPAQUE };
    pub const MAROON: Self = Self { r: 128, g: 0, b: 0, a: Self::ALPHA_OPAQUE };
    pub const LIME: Self = Self { r: 0, g: 255, b: 0, a: Self::ALPHA_OPAQUE };
    pub const AQUA: Self = Self { r: 0, g: 255, b: 255, a: Self::ALPHA_OPAQUE };
    pub const SILVER: Self = Self { r: 192, g: 192, b: 192, a: Self::ALPHA_OPAQUE };
    pub const FUCHSIA: Self = Self { r: 255, g: 0, b: 255, a: Self::ALPHA_OPAQUE };
    pub const INDIGO: Self = Self { r: 75, g: 0, b: 130, a: Self::ALPHA_OPAQUE };
    pub const GOLD: Self = Self { r: 255, g: 215, b: 0, a: Self::ALPHA_OPAQUE };
    pub const CORAL: Self = Self { r: 255, g: 127, b: 80, a: Self::ALPHA_OPAQUE };
    pub const SALMON: Self = Self { r: 250, g: 128, b: 114, a: Self::ALPHA_OPAQUE };
    pub const TURQUOISE: Self = Self { r: 64, g: 224, b: 208, a: Self::ALPHA_OPAQUE };
    pub const VIOLET: Self = Self { r: 238, g: 130, b: 238, a: Self::ALPHA_OPAQUE };
    pub const CRIMSON: Self = Self { r: 220, g: 20, b: 60, a: Self::ALPHA_OPAQUE };
    pub const CHOCOLATE: Self = Self { r: 210, g: 105, b: 30, a: Self::ALPHA_OPAQUE };
    pub const SKY_BLUE: Self = Self { r: 135, g: 206, b: 235, a: Self::ALPHA_OPAQUE };
    pub const FOREST_GREEN: Self = Self { r: 34, g: 139, b: 34, a: Self::ALPHA_OPAQUE };
    pub const SEA_GREEN: Self = Self { r: 46, g: 139, b: 87, a: Self::ALPHA_OPAQUE };
    pub const SLATE_GRAY: Self = Self { r: 112, g: 128, b: 144, a: Self::ALPHA_OPAQUE };
    pub const MIDNIGHT_BLUE: Self = Self { r: 25, g: 25, b: 112, a: Self::ALPHA_OPAQUE };
    pub const DARK_RED: Self = Self { r: 139, g: 0, b: 0, a: Self::ALPHA_OPAQUE };
    pub const DARK_GREEN: Self = Self { r: 0, g: 100, b: 0, a: Self::ALPHA_OPAQUE };
    pub const DARK_BLUE: Self = Self { r: 0, g: 0, b: 139, a: Self::ALPHA_OPAQUE };
    pub const LIGHT_BLUE: Self = Self { r: 173, g: 216, b: 230, a: Self::ALPHA_OPAQUE };
    pub const LIGHT_GREEN: Self = Self { r: 144, g: 238, b: 144, a: Self::ALPHA_OPAQUE };
    pub const LIGHT_YELLOW: Self = Self { r: 255, g: 255, b: 224, a: Self::ALPHA_OPAQUE };
    pub const LIGHT_PINK: Self = Self { r: 255, g: 182, b: 193, a: Self::ALPHA_OPAQUE };

    // Constructor functions for C API (become AzColorU_red(), AzColorU_cyan(), etc.)
    #[must_use] pub const fn red() -> Self { Self::RED }
    #[must_use] pub const fn green() -> Self { Self::GREEN }
    #[must_use] pub const fn blue() -> Self { Self::BLUE }
    #[must_use] pub const fn white() -> Self { Self::WHITE }
    #[must_use] pub const fn black() -> Self { Self::BLACK }
    #[must_use] pub const fn transparent() -> Self { Self::TRANSPARENT }
    #[must_use] pub const fn yellow() -> Self { Self::YELLOW }
    #[must_use] pub const fn cyan() -> Self { Self::CYAN }
    #[must_use] pub const fn magenta() -> Self { Self::MAGENTA }
    #[must_use] pub const fn orange() -> Self { Self::ORANGE }
    #[must_use] pub const fn pink() -> Self { Self::PINK }
    #[must_use] pub const fn purple() -> Self { Self::PURPLE }
    #[must_use] pub const fn brown() -> Self { Self::BROWN }
    #[must_use] pub const fn gray() -> Self { Self::GRAY }
    #[must_use] pub const fn light_gray() -> Self { Self::LIGHT_GRAY }
    #[must_use] pub const fn dark_gray() -> Self { Self::DARK_GRAY }
    #[must_use] pub const fn navy() -> Self { Self::NAVY }
    #[must_use] pub const fn teal() -> Self { Self::TEAL }
    #[must_use] pub const fn olive() -> Self { Self::OLIVE }
    #[must_use] pub const fn maroon() -> Self { Self::MAROON }
    #[must_use] pub const fn lime() -> Self { Self::LIME }
    #[must_use] pub const fn aqua() -> Self { Self::AQUA }
    #[must_use] pub const fn silver() -> Self { Self::SILVER }
    #[must_use] pub const fn fuchsia() -> Self { Self::FUCHSIA }
    #[must_use] pub const fn indigo() -> Self { Self::INDIGO }
    #[must_use] pub const fn gold() -> Self { Self::GOLD }
    #[must_use] pub const fn coral() -> Self { Self::CORAL }
    #[must_use] pub const fn salmon() -> Self { Self::SALMON }
    #[must_use] pub const fn turquoise() -> Self { Self::TURQUOISE }
    #[must_use] pub const fn violet() -> Self { Self::VIOLET }
    #[must_use] pub const fn crimson() -> Self { Self::CRIMSON }
    #[must_use] pub const fn chocolate() -> Self { Self::CHOCOLATE }
    #[must_use] pub const fn sky_blue() -> Self { Self::SKY_BLUE }
    #[must_use] pub const fn forest_green() -> Self { Self::FOREST_GREEN }
    #[must_use] pub const fn sea_green() -> Self { Self::SEA_GREEN }
    #[must_use] pub const fn slate_gray() -> Self { Self::SLATE_GRAY }
    #[must_use] pub const fn midnight_blue() -> Self { Self::MIDNIGHT_BLUE }
    #[must_use] pub const fn dark_red() -> Self { Self::DARK_RED }
    #[must_use] pub const fn dark_green() -> Self { Self::DARK_GREEN }
    #[must_use] pub const fn dark_blue() -> Self { Self::DARK_BLUE }
    #[must_use] pub const fn light_blue() -> Self { Self::LIGHT_BLUE }
    #[must_use] pub const fn light_green() -> Self { Self::LIGHT_GREEN }
    #[must_use] pub const fn light_yellow() -> Self { Self::LIGHT_YELLOW }
    #[must_use] pub const fn light_pink() -> Self { Self::LIGHT_PINK }

    /// Creates a new color with RGBA values.
    #[must_use] pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self { r, g, b, a }
    }
    /// Creates a new color with RGB values (alpha = 255).
    #[must_use] pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b, a: 255 }
    }
    /// Alias for `rgba` - kept for internal compatibility, not exposed in FFI.
    #[inline]
    #[must_use] pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self::rgba(r, g, b, a)
    }
    /// Alias for `rgb` - kept for internal compatibility, not exposed in FFI.
    #[inline]
    #[must_use] pub const fn new_rgb(r: u8, g: u8, b: u8) -> Self {
        Self::rgb(r, g, b)
    }

    /// Linearly interpolate all four RGBA channels between `self` and `other`.
    /// `t = 0.0` returns `self`, `t = 1.0` returns `other`.
    #[must_use] pub fn interpolate(&self, other: &Self, t: f32) -> Self {
        Self {
            r: channel_to_u8(libm::roundf(f32::from(self.r) + (f32::from(other.r) - f32::from(self.r)) * t)),
            g: channel_to_u8(libm::roundf(f32::from(self.g) + (f32::from(other.g) - f32::from(self.g)) * t)),
            b: channel_to_u8(libm::roundf(f32::from(self.b) + (f32::from(other.b) - f32::from(self.b)) * t)),
            a: channel_to_u8(libm::roundf(f32::from(self.a) + (f32::from(other.a) - f32::from(self.a)) * t)),
        }
    }
    
    /// Lighten a color by a percentage (0.0 to 1.0).
    /// Returns a new color blended towards white, preserving the original alpha.
    #[must_use] pub fn lighten(&self, amount: f32) -> Self {
        let mut c = self.interpolate(&Self::WHITE, amount.clamp(0.0, 1.0));
        c.a = self.a;
        c
    }

    /// Darken a color by a percentage (0.0 to 1.0).
    /// Returns a new color blended towards black, preserving the original alpha.
    #[must_use] pub fn darken(&self, amount: f32) -> Self {
        let mut c = self.interpolate(&Self::BLACK, amount.clamp(0.0, 1.0));
        c.a = self.a;
        c
    }
    
    /// Mix two colors together with a given ratio (0.0 = self, 1.0 = other).
    #[must_use] pub fn mix(&self, other: &Self, ratio: f32) -> Self {
        self.interpolate(other, ratio.clamp(0.0, 1.0))
    }
    
    /// Create a hover variant (slightly lighter for dark colors, darker for light colors).
    /// This is useful for button hover states.
    #[must_use] pub fn hover_variant(&self) -> Self {
        let luminance = self.relative_luminance();
        if luminance > 0.5 {
            self.darken(0.08)
        } else {
            self.lighten(0.12)
        }
    }

    /// Create an active/pressed variant (darker than hover).
    /// This is useful for button active states.
    #[must_use] pub fn active_variant(&self) -> Self {
        let luminance = self.relative_luminance();
        if luminance > 0.5 {
            self.darken(0.15)
        } else {
            self.lighten(0.05)
        }
    }
    
    /// Calculate approximate luminance (0.0 = black, 1.0 = white).
    ///
    /// **Note:** This applies BT.709 coefficients directly to gamma-encoded sRGB
    /// values without linearizing first, so it is only an approximation.
    /// For accurate results (e.g. WCAG contrast checks), use [`relative_luminance()`].
    #[must_use] pub fn luminance(&self) -> f32 {
        let r = f32::from(self.r) / 255.0;
        let g = f32::from(self.g) / 255.0;
        let b = f32::from(self.b) / 255.0;
        0.2126 * r + 0.7152 * g + 0.0722 * b
    }

    /// Returns white or black text color for best contrast on this background.
    #[must_use] pub fn contrast_text(&self) -> Self {
        self.best_contrast_text()
    }
    
    // ============================================================
    // WCAG Accessibility and Contrast Helpers
    // Based on W3C WCAG 2.1 guidelines and Chromium research
    // ============================================================
    
    /// Converts a single sRGB channel to linear RGB.
    /// Used for accurate luminance and contrast calculations.
    fn srgb_to_linear(c: f32) -> f32 {
        if c <= 0.03928 {
            c / 12.92
        } else {
            libm::powf((c + 0.055) / 1.055, 2.4)
        }
    }
    
    /// Calculate relative luminance per WCAG 2.1 specification.
    /// Returns a value between 0.0 (darkest) and 1.0 (lightest).
    /// Uses the sRGB to linear conversion for accurate results.
    #[must_use] pub fn relative_luminance(&self) -> f32 {
        let r = Self::srgb_to_linear(f32::from(self.r) / 255.0);
        let g = Self::srgb_to_linear(f32::from(self.g) / 255.0);
        let b = Self::srgb_to_linear(f32::from(self.b) / 255.0);
        0.2126 * r + 0.7152 * g + 0.0722 * b
    }
    
    /// Calculate the contrast ratio between this color and another.
    /// Returns a value between 1.0 (no contrast) and 21.0 (max contrast).
    /// 
    /// WCAG 2.1 requirements:
    /// - AA normal text: >= 4.5:1
    /// - AA large text: >= 3.0:1
    /// - AAA normal text: >= 7.0:1
    /// - AAA large text: >= 4.5:1
    #[must_use] pub fn contrast_ratio(&self, other: &Self) -> f32 {
        let l1 = self.relative_luminance();
        let l2 = other.relative_luminance();
        let lighter = if l1 > l2 { l1 } else { l2 };
        let darker = if l1 > l2 { l2 } else { l1 };
        (lighter + 0.05) / (darker + 0.05)
    }
    
    /// Check if the contrast ratio meets WCAG AA requirements for normal text (>= 4.5:1).
    #[must_use] pub fn meets_wcag_aa(&self, other: &Self) -> bool {
        self.contrast_ratio(other) >= 4.5
    }
    
    /// Check if the contrast ratio meets WCAG AA requirements for large text (>= 3.0:1).
    /// Large text is defined as 18pt+ or 14pt+ bold.
    #[must_use] pub fn meets_wcag_aa_large(&self, other: &Self) -> bool {
        self.contrast_ratio(other) >= 3.0
    }
    
    /// Check if the contrast ratio meets WCAG AAA requirements for normal text (>= 7.0:1).
    #[must_use] pub fn meets_wcag_aaa(&self, other: &Self) -> bool {
        self.contrast_ratio(other) >= 7.0
    }
    
    /// Check if the contrast ratio meets WCAG AAA requirements for large text (>= 4.5:1).
    #[must_use] pub fn meets_wcag_aaa_large(&self, other: &Self) -> bool {
        self.contrast_ratio(other) >= 4.5
    }
    
    /// Returns true if this color is considered "light" (relative luminance > 0.5).
    /// Useful for determining if dark or light text should be used.
    #[must_use] pub fn is_light(&self) -> bool {
        self.relative_luminance() > 0.5
    }

    /// Returns true if this color is considered "dark" (relative luminance <= 0.5).
    #[must_use] pub fn is_dark(&self) -> bool {
        self.relative_luminance() <= 0.5
    }
    
    /// Suggest the best text color (black or white) for this background,
    /// ensuring WCAG AA compliance for normal text.
    /// 
    /// If neither black nor white meets AA requirements (unlikely), 
    /// returns the one with higher contrast.
    #[must_use] pub fn best_contrast_text(&self) -> Self {
        let white_contrast = self.contrast_ratio(&Self::WHITE);
        let black_contrast = self.contrast_ratio(&Self::BLACK);
        
        if white_contrast >= black_contrast {
            Self::WHITE
        } else {
            Self::BLACK
        }
    }
    
    /// Adjust the color to ensure it meets the minimum contrast ratio against a background.
    /// Lightens or darkens the color as needed.
    /// 
    /// Returns the original color if it already meets the requirement,
    /// otherwise returns an adjusted color that meets the minimum contrast.
    #[must_use] pub fn ensure_contrast(&self, background: &Self, min_ratio: f32) -> Self {
        let current_ratio = self.contrast_ratio(background);
        if current_ratio >= min_ratio {
            return *self;
        }
        
        // Determine if we should lighten or darken
        let bg_luminance = background.relative_luminance();
        let should_lighten = bg_luminance < 0.5;
        
        // Binary search for the right amount
        let mut low = 0.0f32;
        let mut high = 1.0f32;
        let mut result = *self;
        
        for _ in 0..16 {
            let mid = f32::midpoint(low, high);
            let candidate = if should_lighten {
                self.lighten(mid)
            } else {
                self.darken(mid)
            };
            
            if candidate.contrast_ratio(background) >= min_ratio {
                result = candidate;
                high = mid;
            } else {
                low = mid;
            }
        }
        
        result
    }
    
    /// Calculate the APCA (Accessible Perceptual Contrast Algorithm) contrast.
    /// This is a newer algorithm that may replace WCAG contrast in future standards.
    /// Returns a value between -108 (white on black) and 106 (black on white).
    ///
    /// **Note:** This is an approximation — it reuses the WCAG piecewise sRGB
    /// linearization and BT.709 luminance coefficients rather than the APCA-specific
    /// TRC exponents and coefficients from the full 0.0.98G specification.
    ///
    /// The sign indicates polarity (negative = light text on dark bg).
    /// For most purposes, use the absolute value.
    #[must_use] pub fn apca_contrast(&self, background: &Self) -> f32 {
        // APCA 0.0.98G constants
        const NORMBLKTXT: f32 = 0.56;
        const NORMWHT: f32 = 0.57;
        const REVTXT: f32 = 0.62;
        const REVWHT: f32 = 0.65;
        const BLKTHRS: f32 = 0.022;
        const SCALEBLKT: f32 = 1.414;
        const SCALEWHT: f32 = 1.14;

        // Convert to Y (luminance) using sRGB TRC
        let text_y = self.relative_luminance();
        let bg_y = background.relative_luminance();
        
        // Soft clamp
        let text_y = if text_y < 0.0 { 0.0 } else { text_y };
        let bg_y = if bg_y < 0.0 { 0.0 } else { bg_y };
        
        
        // Clamp black levels
        let txt_clamp = if text_y < BLKTHRS { 
            text_y + libm::powf(BLKTHRS - text_y, SCALEBLKT)
        } else { 
            text_y 
        };
        let bg_clamp = if bg_y < BLKTHRS { 
            bg_y + libm::powf(BLKTHRS - bg_y, SCALEBLKT)
        } else { 
            bg_y 
        };
        
        // Calculate contrast
        if bg_clamp > txt_clamp {
            // Dark text on light bg
            let s = (libm::powf(bg_clamp, NORMWHT) - libm::powf(txt_clamp, NORMBLKTXT)) * SCALEWHT;
            if s < 0.1 { 0.0 } else { s * 100.0 }
        } else {
            // Light text on dark bg
            let s = (libm::powf(bg_clamp, REVWHT) - libm::powf(txt_clamp, REVTXT)) * SCALEWHT;
            if s > -0.1 { 0.0 } else { s * 100.0 }
        }
    }
    
    /// Check if the APCA contrast meets the recommended minimum for body text (|Lc| >= 60).
    #[must_use] pub fn meets_apca_body(&self, background: &Self) -> bool {
        libm::fabsf(self.apca_contrast(background)) >= 60.0
    }
    
    /// Check if the APCA contrast meets the minimum for large text (|Lc| >= 45).
    #[must_use] pub fn meets_apca_large(&self, background: &Self) -> bool {
        libm::fabsf(self.apca_contrast(background)) >= 45.0
    }
    
    /// Set the alpha channel while keeping RGB values.
    #[must_use] pub const fn with_alpha(&self, a: u8) -> Self {
        Self { r: self.r, g: self.g, b: self.b, a }
    }
    
    /// Set the alpha as a float (0.0 to 1.0).
    #[must_use] pub fn with_alpha_f32(&self, a: f32) -> Self {
        self.with_alpha(channel_to_u8(a.clamp(0.0, 1.0) * 255.0))
    }
    
    /// Invert the color (keeping alpha).
    #[must_use] pub const fn invert(&self) -> Self {
        Self {
            r: 255 - self.r,
            g: 255 - self.g,
            b: 255 - self.b,
            a: self.a,
        }
    }
    
    /// Convert to grayscale using luminance weights.
    #[must_use] pub fn to_grayscale(&self) -> Self {
        let gray = channel_to_u8(0.299 * f32::from(self.r) + 0.587 * f32::from(self.g) + 0.114 * f32::from(self.b));
        Self { r: gray, g: gray, b: gray, a: self.a }
    }

    /// Returns `true` if the alpha channel is not fully opaque (i.e. `a != 255`).
    #[must_use] pub const fn has_alpha(&self) -> bool {
        self.a != Self::ALPHA_OPAQUE
    }

    /// Format the color as an 8-digit lowercase hex string (e.g. `#ff0000ff`).
    #[must_use] pub fn to_hash(&self) -> String {
        format!("#{:02x}{:02x}{:02x}{:02x}", self.r, self.g, self.b, self.a)
    }

    // ============================================================
    // Elementary OS color palette (with shade parameter 100-900)
    // ============================================================

    /// Strawberry color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn strawberry(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0xff, 0x8c, 0x82),   // 100: #ff8c82
            201..=400 => Self::rgb(0xed, 0x53, 0x53), // 300: #ed5353
            401..=600 => Self::rgb(0xc6, 0x26, 0x2e), // 500: #c6262e
            601..=800 => Self::rgb(0xa1, 0x07, 0x05), // 700: #a10705
            _ => Self::rgb(0x7a, 0x00, 0x00),         // 900: #7a0000
        }
    }

    /// Orange color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn palette_orange(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0xff, 0xc2, 0x7d),   // 100: #ffc27d
            201..=400 => Self::rgb(0xff, 0xa1, 0x54), // 300: #ffa154
            401..=600 => Self::rgb(0xf3, 0x73, 0x29), // 500: #f37329
            601..=800 => Self::rgb(0xcc, 0x3b, 0x02), // 700: #cc3b02
            _ => Self::rgb(0xa6, 0x21, 0x00),         // 900: #a62100
        }
    }

    /// Banana color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn banana(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0xff, 0xf3, 0x94),   // 100: #fff394
            201..=400 => Self::rgb(0xff, 0xe1, 0x6b), // 300: #ffe16b
            401..=600 => Self::rgb(0xf9, 0xc4, 0x40), // 500: #f9c440
            601..=800 => Self::rgb(0xd4, 0x8e, 0x15), // 700: #d48e15
            _ => Self::rgb(0xad, 0x5f, 0x00),         // 900: #ad5f00
        }
    }

    /// Lime color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn palette_lime(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0xd1, 0xff, 0x82),   // 100: #d1ff82
            201..=400 => Self::rgb(0x9b, 0xdb, 0x4d), // 300: #9bdb4d
            401..=600 => Self::rgb(0x68, 0xb7, 0x23), // 500: #68b723
            601..=800 => Self::rgb(0x3a, 0x91, 0x04), // 700: #3a9104
            _ => Self::rgb(0x20, 0x6b, 0x00),         // 900: #206b00
        }
    }

    /// Mint color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn mint(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0x89, 0xff, 0xdd),   // 100: #89ffdd
            201..=400 => Self::rgb(0x43, 0xd6, 0xb5), // 300: #43d6b5
            401..=600 => Self::rgb(0x28, 0xbc, 0xa3), // 500: #28bca3
            601..=800 => Self::rgb(0x0e, 0x9a, 0x83), // 700: #0e9a83
            _ => Self::rgb(0x00, 0x73, 0x67),         // 900: #007367
        }
    }

    /// Blueberry color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn blueberry(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0x8c, 0xd5, 0xff),   // 100: #8cd5ff
            201..=400 => Self::rgb(0x64, 0xba, 0xff), // 300: #64baff
            401..=600 => Self::rgb(0x36, 0x89, 0xe6), // 500: #3689e6
            601..=800 => Self::rgb(0x0d, 0x52, 0xbf), // 700: #0d52bf
            _ => Self::rgb(0x00, 0x2e, 0x99),         // 900: #002e99
        }
    }

    /// Grape color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn grape(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0xe4, 0xc6, 0xfa),   // 100: #e4c6fa
            201..=400 => Self::rgb(0xcd, 0x9e, 0xf7), // 300: #cd9ef7
            401..=600 => Self::rgb(0xa5, 0x6d, 0xe2), // 500: #a56de2
            601..=800 => Self::rgb(0x72, 0x39, 0xb3), // 700: #7239b3
            _ => Self::rgb(0x45, 0x29, 0x81),         // 900: #452981
        }
    }

    /// Bubblegum color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn bubblegum(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0xfe, 0x9a, 0xb8),   // 100: #fe9ab8
            201..=400 => Self::rgb(0xf4, 0x67, 0x9d), // 300: #f4679d
            401..=600 => Self::rgb(0xde, 0x3e, 0x80), // 500: #de3e80
            601..=800 => Self::rgb(0xbc, 0x24, 0x5d), // 700: #bc245d
            _ => Self::rgb(0x91, 0x0e, 0x38),         // 900: #910e38
        }
    }

    /// Cocoa color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn cocoa(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0xa3, 0x90, 0x7c),   // 100: #a3907c
            201..=400 => Self::rgb(0x8a, 0x71, 0x5e), // 300: #8a715e
            401..=600 => Self::rgb(0x71, 0x53, 0x44), // 500: #715344
            601..=800 => Self::rgb(0x57, 0x39, 0x2d), // 700: #57392d
            _ => Self::rgb(0x3d, 0x21, 0x1b),         // 900: #3d211b
        }
    }

    /// Silver color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn palette_silver(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0xfa, 0xfa, 0xfa),   // 100: #fafafa
            201..=400 => Self::rgb(0xd4, 0xd4, 0xd4), // 300: #d4d4d4
            401..=600 => Self::rgb(0xab, 0xac, 0xae), // 500: #abacae
            601..=800 => Self::rgb(0x7e, 0x80, 0x87), // 700: #7e8087
            _ => Self::rgb(0x55, 0x57, 0x61),         // 900: #555761
        }
    }

    /// Slate color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn slate(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0x95, 0xa3, 0xab),   // 100: #95a3ab
            201..=400 => Self::rgb(0x66, 0x78, 0x85), // 300: #667885
            401..=600 => Self::rgb(0x48, 0x5a, 0x6c), // 500: #485a6c
            601..=800 => Self::rgb(0x27, 0x34, 0x45), // 700: #273445
            _ => Self::rgb(0x0e, 0x14, 0x1f),         // 900: #0e141f
        }
    }

    /// Dark color palette (shade: 100, 300, 500, 700, 900)
    #[must_use] pub const fn dark(shade: usize) -> Self {
        match shade {
            0..=200 => Self::rgb(0x66, 0x66, 0x66),   // 100: #666
            201..=400 => Self::rgb(0x4d, 0x4d, 0x4d), // 300: #4d4d4d
            401..=600 => Self::rgb(0x33, 0x33, 0x33), // 500: #333
            601..=800 => Self::rgb(0x1a, 0x1a, 0x1a), // 700: #1a1a1a
            _ => Self::rgb(0x00, 0x00, 0x00),         // 900: #000
        }
    }

    // ============================================================
    // Apple System Colors (light and dark variants)
    // ============================================================

    /// Apple Red (light mode)
    #[must_use] pub const fn apple_red() -> Self { Self::rgb(255, 59, 48) }
    /// Apple Red (dark mode)
    #[must_use] pub const fn apple_red_dark() -> Self { Self::rgb(255, 69, 58) }
    /// Apple Orange (light mode)
    #[must_use] pub const fn apple_orange() -> Self { Self::rgb(255, 149, 0) }
    /// Apple Orange (dark mode)
    #[must_use] pub const fn apple_orange_dark() -> Self { Self::rgb(255, 159, 10) }
    /// Apple Yellow (light mode)
    #[must_use] pub const fn apple_yellow() -> Self { Self::rgb(255, 204, 0) }
    /// Apple Yellow (dark mode)
    #[must_use] pub const fn apple_yellow_dark() -> Self { Self::rgb(255, 214, 10) }
    /// Apple Green (light mode)
    #[must_use] pub const fn apple_green() -> Self { Self::rgb(40, 205, 65) }
    /// Apple Green (dark mode)
    #[must_use] pub const fn apple_green_dark() -> Self { Self::rgb(40, 215, 75) }
    /// Apple Mint (light mode)
    #[must_use] pub const fn apple_mint() -> Self { Self::rgb(0, 199, 190) }
    /// Apple Mint (dark mode)
    #[must_use] pub const fn apple_mint_dark() -> Self { Self::rgb(102, 212, 207) }
    /// Apple Teal (light mode)
    #[must_use] pub const fn apple_teal() -> Self { Self::rgb(89, 173, 196) }
    /// Apple Teal (dark mode)
    #[must_use] pub const fn apple_teal_dark() -> Self { Self::rgb(106, 196, 220) }
    /// Apple Cyan (light mode)
    #[must_use] pub const fn apple_cyan() -> Self { Self::rgb(85, 190, 240) }
    /// Apple Cyan (dark mode)
    #[must_use] pub const fn apple_cyan_dark() -> Self { Self::rgb(90, 200, 245) }
    /// Apple Blue (light mode)
    #[must_use] pub const fn apple_blue() -> Self { Self::rgb(0, 122, 255) }
    /// Apple Blue (dark mode)
    #[must_use] pub const fn apple_blue_dark() -> Self { Self::rgb(10, 132, 255) }
    /// Apple Indigo (light mode)
    #[must_use] pub const fn apple_indigo() -> Self { Self::rgb(88, 86, 214) }
    /// Apple Indigo (dark mode)
    #[must_use] pub const fn apple_indigo_dark() -> Self { Self::rgb(94, 92, 230) }
    /// Apple Purple (light mode)
    #[must_use] pub const fn apple_purple() -> Self { Self::rgb(175, 82, 222) }
    /// Apple Purple (dark mode)
    #[must_use] pub const fn apple_purple_dark() -> Self { Self::rgb(191, 90, 242) }
    /// Apple Pink (light mode)
    #[must_use] pub const fn apple_pink() -> Self { Self::rgb(255, 45, 85) }
    /// Apple Pink (dark mode)
    #[must_use] pub const fn apple_pink_dark() -> Self { Self::rgb(255, 55, 95) }
    /// Apple Brown (light mode)
    #[must_use] pub const fn apple_brown() -> Self { Self::rgb(162, 132, 94) }
    /// Apple Brown (dark mode)
    #[must_use] pub const fn apple_brown_dark() -> Self { Self::rgb(172, 142, 104) }
    /// Apple Gray (light mode)
    #[must_use] pub const fn apple_gray() -> Self { Self::rgb(142, 142, 147) }
    /// Apple Gray (dark mode)
    #[must_use] pub const fn apple_gray_dark() -> Self { Self::rgb(152, 152, 157) }

    // ============================================================
    // Bootstrap-style semantic button colors
    // These provide consistent button styling across platforms
    // ============================================================

    /// Primary button color (blue) - used for main actions
    #[must_use] pub const fn bootstrap_primary() -> Self { Self::rgb(13, 110, 253) }
    #[must_use] pub const fn bootstrap_primary_hover() -> Self { Self::rgb(11, 94, 215) }
    #[must_use] pub const fn bootstrap_primary_active() -> Self { Self::rgb(10, 88, 202) }
    
    /// Secondary button color (gray) - used for secondary actions
    #[must_use] pub const fn bootstrap_secondary() -> Self { Self::rgb(108, 117, 125) }
    #[must_use] pub const fn bootstrap_secondary_hover() -> Self { Self::rgb(92, 99, 106) }
    #[must_use] pub const fn bootstrap_secondary_active() -> Self { Self::rgb(86, 94, 100) }
    
    /// Success button color (green) - used for confirmations
    #[must_use] pub const fn bootstrap_success() -> Self { Self::rgb(25, 135, 84) }
    #[must_use] pub const fn bootstrap_success_hover() -> Self { Self::rgb(21, 115, 71) }
    #[must_use] pub const fn bootstrap_success_active() -> Self { Self::rgb(20, 108, 67) }
    
    /// Danger button color (red) - used for destructive actions
    #[must_use] pub const fn bootstrap_danger() -> Self { Self::rgb(220, 53, 69) }
    #[must_use] pub const fn bootstrap_danger_hover() -> Self { Self::rgb(187, 45, 59) }
    #[must_use] pub const fn bootstrap_danger_active() -> Self { Self::rgb(176, 42, 55) }
    
    /// Warning button color (yellow) - used for warnings, uses BLACK text
    #[must_use] pub const fn bootstrap_warning() -> Self { Self::rgb(255, 193, 7) }
    #[must_use] pub const fn bootstrap_warning_hover() -> Self { Self::rgb(255, 202, 44) }
    #[must_use] pub const fn bootstrap_warning_active() -> Self { Self::rgb(255, 205, 57) }
    
    /// Info button color (teal/cyan) - used for informational actions
    #[must_use] pub const fn bootstrap_info() -> Self { Self::rgb(13, 202, 240) }
    #[must_use] pub const fn bootstrap_info_hover() -> Self { Self::rgb(49, 210, 242) }
    #[must_use] pub const fn bootstrap_info_active() -> Self { Self::rgb(61, 213, 243) }
    
    /// Light button color - used for light-themed buttons
    #[must_use] pub const fn bootstrap_light() -> Self { Self::rgb(248, 249, 250) }
    #[must_use] pub const fn bootstrap_light_hover() -> Self { Self::rgb(233, 236, 239) }
    #[must_use] pub const fn bootstrap_light_active() -> Self { Self::rgb(218, 222, 226) }
    
    /// Dark button color - used for dark-themed buttons
    #[must_use] pub const fn bootstrap_dark() -> Self { Self::rgb(33, 37, 41) }
    #[must_use] pub const fn bootstrap_dark_hover() -> Self { Self::rgb(66, 70, 73) }
    #[must_use] pub const fn bootstrap_dark_active() -> Self { Self::rgb(78, 81, 84) }
    
    /// Link button text color
    #[must_use] pub const fn bootstrap_link() -> Self { Self::rgb(13, 110, 253) }
    #[must_use] pub const fn bootstrap_link_hover() -> Self { Self::rgb(10, 88, 202) }
}

/// f32-based color, range 0.0 to 1.0 (similar to webrenders `ColorF`)
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct ColorF {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Default for ColorF {
    fn default() -> Self {
        Self::BLACK
    }
}

impl fmt::Display for ColorF {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "rgba({}, {}, {}, {})",
            self.r * 255.0,
            self.g * 255.0,
            self.b * 255.0,
            self.a
        )
    }
}

impl ColorF {
    pub const ALPHA_TRANSPARENT: f32 = 0.0;
    pub const ALPHA_OPAQUE: f32 = 1.0;
    pub const WHITE: Self = Self {
        r: 1.0,
        g: 1.0,
        b: 1.0,
        a: Self::ALPHA_OPAQUE,
    };
    pub const BLACK: Self = Self {
        r: 0.0,
        g: 0.0,
        b: 0.0,
        a: Self::ALPHA_OPAQUE,
    };
    pub const TRANSPARENT: Self = Self {
        r: 0.0,
        g: 0.0,
        b: 0.0,
        a: Self::ALPHA_TRANSPARENT,
    };
}

impl From<ColorU> for ColorF {
    fn from(input: ColorU) -> Self {
        Self {
            r: f32::from(input.r) / 255.0,
            g: f32::from(input.g) / 255.0,
            b: f32::from(input.b) / 255.0,
            a: f32::from(input.a) / 255.0,
        }
    }
}

impl From<ColorF> for ColorU {
    fn from(input: ColorF) -> Self {
        Self {
            r: channel_to_u8(input.r.min(1.0) * 255.0),
            g: channel_to_u8(input.g.min(1.0) * 255.0),
            b: channel_to_u8(input.b.min(1.0) * 255.0),
            a: channel_to_u8(input.a.min(1.0) * 255.0),
        }
    }
}

/// A color reference that can be either a concrete color or a system color.
/// System colors are lazily evaluated at runtime based on the user's system theme.
/// 
/// CSS syntax: `system:accent`, `system:text`, `system:background`, etc.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C, u8)]
pub enum ColorOrSystem {
    /// A concrete RGBA color value.
    Color(ColorU),
    /// A reference to a system color, resolved at runtime.
    System(SystemColorRef),
}

impl Default for ColorOrSystem {
    fn default() -> Self {
        Self::Color(ColorU::BLACK)
    }
}

impl From<ColorU> for ColorOrSystem {
    fn from(color: ColorU) -> Self {
        Self::Color(color)
    }
}

impl ColorOrSystem {
    /// Create a new `ColorOrSystem` from a concrete color.
    #[must_use] pub const fn color(c: ColorU) -> Self {
        Self::Color(c)
    }
    
    /// Create a new `ColorOrSystem` from a system color reference.
    #[must_use] pub const fn system(s: SystemColorRef) -> Self {
        Self::System(s)
    }
    
    /// Resolve the color against a `SystemColors` struct.
    /// Returns the system color if available, or falls back to the provided default.
    #[must_use] pub fn resolve(&self, system_colors: &crate::system::SystemColors, fallback: ColorU) -> ColorU {
        match self {
            Self::Color(c) => *c,
            Self::System(ref_type) => ref_type.resolve(system_colors, fallback),
        }
    }
    
    /// Returns the concrete color if available, or a default fallback for system colors.
    /// Use this when `SystemColors` is not available (e.g., during rendering setup).
    #[must_use] pub const fn to_color_u_with_fallback(&self, fallback: ColorU) -> ColorU {
        match self {
            Self::Color(c) => *c,
            Self::System(_) => fallback,
        }
    }
    
    /// Returns the concrete color if available, or a gray fallback for system colors.
    #[must_use] pub const fn to_color_u_default(&self) -> ColorU {
        self.to_color_u_with_fallback(ColorU { r: 128, g: 128, b: 128, a: 255 })
    }
}

/// Reference to a specific system color.
/// These are resolved at runtime based on the user's system preferences.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum SystemColorRef {
    /// System text color (e.g., black on light theme, white on dark)
    Text,
    /// System background color
    Background,
    /// System accent color (user-selected highlight color)
    Accent,
    /// Text color when on accent background
    AccentText,
    /// Button face background color
    ButtonFace,
    /// Button text color
    ButtonText,
    /// Window/panel background color
    WindowBackground,
    /// Selection/highlight background color
    SelectionBackground,
    /// Text color when selected
    SelectionText,
}

impl SystemColorRef {
    /// Resolve this system color reference against actual system colors.
    #[must_use] pub fn resolve(&self, colors: &crate::system::SystemColors, fallback: ColorU) -> ColorU {
        match self {
            Self::Text => colors.text.as_option().copied().unwrap_or(fallback),
            Self::Background => colors.background.as_option().copied().unwrap_or(fallback),
            Self::Accent => colors.accent.as_option().copied().unwrap_or(fallback),
            Self::AccentText => colors.accent_text.as_option().copied().unwrap_or(fallback),
            Self::ButtonFace => colors.button_face.as_option().copied().unwrap_or(fallback),
            Self::ButtonText => colors.button_text.as_option().copied().unwrap_or(fallback),
            Self::WindowBackground => colors.window_background.as_option().copied().unwrap_or(fallback),
            Self::SelectionBackground => colors.selection_background.as_option().copied().unwrap_or(fallback),
            Self::SelectionText => colors.selection_text.as_option().copied().unwrap_or(fallback),
        }
    }
    
    /// Get the CSS syntax for this system color reference.
    #[must_use] pub const fn as_css_str(&self) -> &'static str {
        match self {
            Self::Text => "system:text",
            Self::Background => "system:background",
            Self::Accent => "system:accent",
            Self::AccentText => "system:accent-text",
            Self::ButtonFace => "system:button-face",
            Self::ButtonText => "system:button-text",
            Self::WindowBackground => "system:window-background",
            Self::SelectionBackground => "system:selection-background",
            Self::SelectionText => "system:selection-text",
        }
    }
}

// --- PARSER ---

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(C)]
pub enum CssColorComponent {
    Red,
    Green,
    Blue,
    Hue,
    Saturation,
    Lightness,
    Alpha,
}

#[derive(Clone, PartialEq)]
pub enum CssColorParseError<'a> {
    InvalidColor(&'a str),
    InvalidFunctionName(&'a str),
    InvalidColorComponent(u8),
    IntValueParseErr(ParseIntError),
    FloatValueParseErr(ParseFloatError),
    FloatValueOutOfRange(f32),
    MissingColorComponent(CssColorComponent),
    ExtraArguments(&'a str),
    UnclosedColor(&'a str),
    EmptyInput,
    DirectionParseError(CssDirectionParseError<'a>),
    UnsupportedDirection(&'a str),
    InvalidPercentage(PercentageParseError),
}

impl_debug_as_display!(CssColorParseError<'a>);
impl_display! {CssColorParseError<'a>, {
    InvalidColor(i) => format!("Invalid CSS color: \"{}\"", i),
    InvalidFunctionName(i) => format!("Invalid function name, expected one of: \"rgb\", \"rgba\", \"hsl\", \"hsla\" got: \"{}\"", i),
    InvalidColorComponent(i) => format!("Invalid color component when parsing CSS color: \"{}\"", i),
    IntValueParseErr(e) => format!("CSS color component: Value not in range between 00 - FF: \"{}\"", e),
    FloatValueParseErr(e) => format!("CSS color component: Value cannot be parsed as floating point number: \"{}\"", e),
    FloatValueOutOfRange(v) => format!("CSS color component: Value not in range between 0.0 - 1.0: \"{}\"", v),
    MissingColorComponent(c) => format!("CSS color is missing {:?} component", c),
    ExtraArguments(a) => format!("Extra argument to CSS color: \"{}\"", a),
    EmptyInput => format!("Empty color string."),
    UnclosedColor(i) => format!("Unclosed color: \"{}\"", i),
    DirectionParseError(e) => format!("Could not parse direction argument for CSS color: \"{}\"", e),
    UnsupportedDirection(d) => format!("Unsupported direction type for CSS color: \"{}\"", d),
    InvalidPercentage(p) => format!("Invalid percentage when parsing CSS color: \"{}\"", p),
}}

impl From<ParseIntError> for CssColorParseError<'_> {
    fn from(e: ParseIntError) -> Self {
        CssColorParseError::IntValueParseErr(e)
    }
}
impl From<ParseFloatError> for CssColorParseError<'_> {
    fn from(e: ParseFloatError) -> Self {
        CssColorParseError::FloatValueParseErr(e)
    }
}
impl From<core::num::ParseIntError> for CssColorParseError<'_> {
    fn from(e: core::num::ParseIntError) -> Self {
        CssColorParseError::IntValueParseErr(ParseIntError::from(e))
    }
}
impl From<core::num::ParseFloatError> for CssColorParseError<'_> {
    fn from(e: core::num::ParseFloatError) -> Self {
        CssColorParseError::FloatValueParseErr(ParseFloatError::from(e))
    }
}
impl_from!(
    CssDirectionParseError<'a>,
    CssColorParseError::DirectionParseError
);

#[derive(Debug, Clone, PartialEq)]
#[repr(C, u8)]
pub enum CssColorParseErrorOwned {
    InvalidColor(AzString),
    InvalidFunctionName(AzString),
    InvalidColorComponent(u8),
    IntValueParseErr(ParseIntError),
    FloatValueParseErr(ParseFloatError),
    FloatValueOutOfRange(f32),
    MissingColorComponent(CssColorComponent),
    ExtraArguments(AzString),
    UnclosedColor(AzString),
    EmptyInput,
    DirectionParseError(CssDirectionParseErrorOwned),
    UnsupportedDirection(AzString),
    InvalidPercentage(PercentageParseError),
}

impl CssColorParseError<'_> {
    #[must_use] pub fn to_contained(&self) -> CssColorParseErrorOwned {
        match self {
            CssColorParseError::InvalidColor(s) => {
                CssColorParseErrorOwned::InvalidColor((*s).to_string().into())
            }
            CssColorParseError::InvalidFunctionName(s) => {
                CssColorParseErrorOwned::InvalidFunctionName((*s).to_string().into())
            }
            CssColorParseError::InvalidColorComponent(n) => {
                CssColorParseErrorOwned::InvalidColorComponent(*n)
            }
            CssColorParseError::IntValueParseErr(e) => {
                CssColorParseErrorOwned::IntValueParseErr(*e)
            }
            CssColorParseError::FloatValueParseErr(e) => {
                CssColorParseErrorOwned::FloatValueParseErr(*e)
            }
            CssColorParseError::FloatValueOutOfRange(n) => {
                CssColorParseErrorOwned::FloatValueOutOfRange(*n)
            }
            CssColorParseError::MissingColorComponent(c) => {
                CssColorParseErrorOwned::MissingColorComponent(*c)
            }
            CssColorParseError::ExtraArguments(s) => {
                CssColorParseErrorOwned::ExtraArguments((*s).to_string().into())
            }
            CssColorParseError::UnclosedColor(s) => {
                CssColorParseErrorOwned::UnclosedColor((*s).to_string().into())
            }
            CssColorParseError::EmptyInput => CssColorParseErrorOwned::EmptyInput,
            CssColorParseError::DirectionParseError(e) => {
                CssColorParseErrorOwned::DirectionParseError(e.to_contained())
            }
            CssColorParseError::UnsupportedDirection(s) => {
                CssColorParseErrorOwned::UnsupportedDirection((*s).to_string().into())
            }
            CssColorParseError::InvalidPercentage(e) => {
                CssColorParseErrorOwned::InvalidPercentage(e.clone())
            }
        }
    }
}

impl CssColorParseErrorOwned {
    #[must_use] pub fn to_shared(&self) -> CssColorParseError<'_> {
        match self {
            Self::InvalidColor(s) => CssColorParseError::InvalidColor(s),
            Self::InvalidFunctionName(s) => {
                CssColorParseError::InvalidFunctionName(s)
            }
            Self::InvalidColorComponent(n) => {
                CssColorParseError::InvalidColorComponent(*n)
            }
            Self::IntValueParseErr(e) => {
                CssColorParseError::IntValueParseErr(*e)
            }
            Self::FloatValueParseErr(e) => {
                CssColorParseError::FloatValueParseErr(*e)
            }
            Self::FloatValueOutOfRange(n) => {
                CssColorParseError::FloatValueOutOfRange(*n)
            }
            Self::MissingColorComponent(c) => {
                CssColorParseError::MissingColorComponent(*c)
            }
            Self::ExtraArguments(s) => CssColorParseError::ExtraArguments(s),
            Self::UnclosedColor(s) => CssColorParseError::UnclosedColor(s),
            Self::EmptyInput => CssColorParseError::EmptyInput,
            Self::DirectionParseError(e) => {
                CssColorParseError::DirectionParseError(e.to_shared())
            }
            Self::UnsupportedDirection(s) => {
                CssColorParseError::UnsupportedDirection(s)
            }
            Self::InvalidPercentage(e) => {
                CssColorParseError::InvalidPercentage(e.clone())
            }
        }
    }
}

#[cfg(feature = "parser")]
/// # Errors
///
/// Returns an error if `input` is not a valid CSS `css-color` value.
pub fn parse_css_color(input: &str) -> Result<ColorU, CssColorParseError<'_>> {
    use crate::props::basic::parse::{parse_parentheses, ParenthesisParseError};

    let input = input.trim();
    if let Some(rest) = input.strip_prefix('#') {
        return parse_color_no_hash(rest);
    }

    match parse_parentheses(input, &["rgba", "rgb", "hsla", "hsl"]) {
        Ok((stopword, inner_value)) => match stopword {
            "rgba" => parse_color_rgb(inner_value, true),
            "rgb" => parse_color_rgb(inner_value, false),
            "hsla" => parse_color_hsl(inner_value, true),
            "hsl" => parse_color_hsl(inner_value, false),
            _ => unreachable!(),
        },
        Err(e) => match e {
            ParenthesisParseError::UnclosedBraces | ParenthesisParseError::NoClosingBraceFound => {
                Err(CssColorParseError::UnclosedColor(input))
            }
            ParenthesisParseError::EmptyInput => Err(CssColorParseError::EmptyInput),
            ParenthesisParseError::StopWordNotFound(stopword) => {
                Err(CssColorParseError::InvalidFunctionName(stopword))
            }
            ParenthesisParseError::NoOpeningBraceFound => parse_color_builtin(input),
        },
    }
}

/// Parse a color that can be either a concrete color or a system color reference.
/// 
/// Supports all standard CSS color formats plus:
/// - `system:accent` - System accent/highlight color
/// - `system:text` - System text color
/// - `system:background` - System background color
/// - `system:selection-background` - Selection/highlight background
/// - `system:selection-text` - Text color when selected
/// - `system:button-face` - Button background color
/// - `system:button-text` - Button text color
/// - `system:window-background` - Window background color
/// - `system:accent-text` - Text color on accent background
#[cfg(feature = "parser")]
/// # Errors
///
/// Returns an error if `input` is not a valid CSS `color-or-system` value.
pub fn parse_color_or_system(input: &str) -> Result<ColorOrSystem, CssColorParseError<'_>> {
    let input = input.trim();
    
    // Check for system color syntax: "system:name"
    if let Some(system_name) = input.strip_prefix("system:") {
        let system_ref = match system_name.trim() {
            "text" => SystemColorRef::Text,
            "background" => SystemColorRef::Background,
            "accent" => SystemColorRef::Accent,
            "accent-text" => SystemColorRef::AccentText,
            "button-face" => SystemColorRef::ButtonFace,
            "button-text" => SystemColorRef::ButtonText,
            "window-background" => SystemColorRef::WindowBackground,
            "selection-background" => SystemColorRef::SelectionBackground,
            "selection-text" => SystemColorRef::SelectionText,
            _ => return Err(CssColorParseError::InvalidColor(input)),
        };
        return Ok(ColorOrSystem::System(system_ref));
    }
    
    // Otherwise parse as regular color
    parse_css_color(input).map(ColorOrSystem::Color)
}

#[cfg(feature = "parser")]
fn parse_color_no_hash(input: &str) -> Result<ColorU, CssColorParseError<'_>> {
    #[inline]
    const fn from_hex<'a>(c: u8) -> Result<u8, CssColorParseError<'a>> {
        match c {
            b'0'..=b'9' => Ok(c - b'0'),
            b'a'..=b'f' => Ok(c - b'a' + 10),
            b'A'..=b'F' => Ok(c - b'A' + 10),
            _ => Err(CssColorParseError::InvalidColorComponent(c)),
        }
    }

    match input.len() {
        3 => {
            let mut bytes = input.bytes();
            let r = bytes.next().unwrap();
            let g = bytes.next().unwrap();
            let b = bytes.next().unwrap();
            Ok(ColorU::new_rgb(
                from_hex(r)? * 17,
                from_hex(g)? * 17,
                from_hex(b)? * 17,
            ))
        }
        4 => {
            let mut bytes = input.bytes();
            let r = bytes.next().unwrap();
            let g = bytes.next().unwrap();
            let b = bytes.next().unwrap();
            let a = bytes.next().unwrap();
            Ok(ColorU::new(
                from_hex(r)? * 17,
                from_hex(g)? * 17,
                from_hex(b)? * 17,
                from_hex(a)? * 17,
            ))
        }
        6 => {
            // u32::from_str_radix silently accepts a leading '+' ("+f0000"), which is
            // not a valid <hex-color> (CSS Color 4 §5.1: only hex digits). The 3/4-digit
            // branches decode per-byte and already reject it; guard the radix branches.
            if !input.bytes().all(|b| b.is_ascii_hexdigit()) {
                return Err(CssColorParseError::InvalidColor(input));
            }
            let val = u32::from_str_radix(input, 16)?;
            Ok(ColorU::new_rgb(
                ((val >> 16) & 0xFF) as u8,
                ((val >> 8) & 0xFF) as u8,
                (val & 0xFF) as u8,
            ))
        }
        8 => {
            if !input.bytes().all(|b| b.is_ascii_hexdigit()) {
                return Err(CssColorParseError::InvalidColor(input));
            }
            let val = u32::from_str_radix(input, 16)?;
            Ok(ColorU::new(
                ((val >> 24) & 0xFF) as u8,
                ((val >> 16) & 0xFF) as u8,
                ((val >> 8) & 0xFF) as u8,
                (val & 0xFF) as u8,
            ))
        }
        _ => Err(CssColorParseError::InvalidColor(input)),
    }
}

#[cfg(feature = "parser")]
fn parse_color_rgb(
    input: &str,
    parse_alpha: bool,
) -> Result<ColorU, CssColorParseError<'_>> {
    let mut components = input.split(',').map(str::trim);
    let rgb_color = parse_color_rgb_components(&mut components)?;
    let a = if parse_alpha {
        parse_alpha_component(&mut components)?
    } else {
        255
    };
    if let Some(arg) = components.next() {
        return Err(CssColorParseError::ExtraArguments(arg));
    }
    Ok(ColorU { a, ..rgb_color })
}

#[cfg(feature = "parser")]
fn parse_color_rgb_components<'a>(
    components: &mut dyn Iterator<Item = &'a str>,
) -> Result<ColorU, CssColorParseError<'a>> {
    #[inline]
    fn component_from_str<'a>(
        components: &mut dyn Iterator<Item = &'a str>,
        which: CssColorComponent,
    ) -> Result<u8, CssColorParseError<'a>> {
        let c = components
            .next()
            .ok_or(CssColorParseError::MissingColorComponent(which))?;
        if c.is_empty() {
            return Err(CssColorParseError::MissingColorComponent(which));
        }
        Ok(c.parse::<u8>()?)
    }
    Ok(ColorU {
        r: component_from_str(components, CssColorComponent::Red)?,
        g: component_from_str(components, CssColorComponent::Green)?,
        b: component_from_str(components, CssColorComponent::Blue)?,
        a: 255,
    })
}

#[cfg(feature = "parser")]
fn parse_color_hsl(
    input: &str,
    parse_alpha: bool,
) -> Result<ColorU, CssColorParseError<'_>> {
    let mut components = input.split(',').map(str::trim);
    let rgb_color = parse_color_hsl_components(&mut components)?;
    let a = if parse_alpha {
        parse_alpha_component(&mut components)?
    } else {
        255
    };
    if let Some(arg) = components.next() {
        return Err(CssColorParseError::ExtraArguments(arg));
    }
    Ok(ColorU { a, ..rgb_color })
}

#[cfg(feature = "parser")]
#[allow(clippy::many_single_char_names)] // domain-standard h/s/l/r/g/b colour component names
fn parse_color_hsl_components<'a>(
    components: &mut dyn Iterator<Item = &'a str>,
) -> Result<ColorU, CssColorParseError<'a>> {
    #[inline]
    fn angle_from_str<'a>(
        components: &mut dyn Iterator<Item = &'a str>,
        which: CssColorComponent,
    ) -> Result<f32, CssColorParseError<'a>> {
        let c = components
            .next()
            .ok_or(CssColorParseError::MissingColorComponent(which))?;
        if c.is_empty() {
            return Err(CssColorParseError::MissingColorComponent(which));
        }
        let dir = parse_direction(c)?;
        match dir {
            Direction::Angle(deg) => Ok(deg.to_degrees()),
            Direction::FromTo(_) => Err(CssColorParseError::UnsupportedDirection(c)),
        }
    }

    #[inline]
    fn percent_from_str<'a>(
        components: &mut dyn Iterator<Item = &'a str>,
        which: CssColorComponent,
    ) -> Result<f32, CssColorParseError<'a>> {
        use crate::props::basic::parse_percentage_value;

        let c = components
            .next()
            .ok_or(CssColorParseError::MissingColorComponent(which))?;
        if c.is_empty() {
            return Err(CssColorParseError::MissingColorComponent(which));
        }

        // Modern CSS allows both percentage and unitless values for HSL
        Ok(parse_percentage_value(c)
            .map_err(CssColorParseError::InvalidPercentage)?
            .normalized()
            * 100.0)
    }

    #[inline]
    #[allow(clippy::suboptimal_flops)] // explicit FP; mul_add slower without +fma
    #[allow(clippy::many_single_char_names)] // domain-standard colour/coordinate component names
    fn hsl_to_rgb(h: f32, s: f32, l: f32) -> (u8, u8, u8) {
        let s = s / 100.0;
        let l = l / 100.0;
        let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
        let h_prime = h / 60.0;
        let x = c * (1.0 - ((h_prime % 2.0) - 1.0).abs());
        let (r1, g1, b1) = if (0.0..1.0).contains(&h_prime) {
            (c, x, 0.0)
        } else if (1.0..2.0).contains(&h_prime) {
            (x, c, 0.0)
        } else if (2.0..3.0).contains(&h_prime) {
            (0.0, c, x)
        } else if (3.0..4.0).contains(&h_prime) {
            (0.0, x, c)
        } else if (4.0..5.0).contains(&h_prime) {
            (x, 0.0, c)
        } else {
            (c, 0.0, x)
        };
        let m = l - c / 2.0;
        (
            channel_to_u8((r1 + m) * 255.0),
            channel_to_u8((g1 + m) * 255.0),
            channel_to_u8((b1 + m) * 255.0),
        )
    }

    let (h, s, l) = (
        angle_from_str(components, CssColorComponent::Hue)?,
        percent_from_str(components, CssColorComponent::Saturation)?,
        percent_from_str(components, CssColorComponent::Lightness)?,
    );

    let (r, g, b) = hsl_to_rgb(h, s, l);
    Ok(ColorU { r, g, b, a: 255 })
}

#[cfg(feature = "parser")]
fn parse_alpha_component<'a>(
    components: &mut dyn Iterator<Item = &'a str>,
) -> Result<u8, CssColorParseError<'a>> {
    let a_str = components
        .next()
        .ok_or(CssColorParseError::MissingColorComponent(
            CssColorComponent::Alpha,
        ))?;
    if a_str.is_empty() {
        return Err(CssColorParseError::MissingColorComponent(
            CssColorComponent::Alpha,
        ));
    }
    let a = a_str.parse::<f32>()?;
    if !(0.0..=1.0).contains(&a) {
        return Err(CssColorParseError::FloatValueOutOfRange(a));
    }
    Ok(channel_to_u8((a * 255.0).round()))
}

#[cfg(feature = "parser")]
#[allow(clippy::too_many_lines)] // large but cohesive: single-purpose CSS parser/formatter/dispatch table (one branch per property/variant)
fn parse_color_builtin(input: &str) -> Result<ColorU, CssColorParseError<'_>> {
    let (r, g, b, a) = match input.to_lowercase().as_str() {
        "aliceblue" => (240, 248, 255, 255),
        "antiquewhite" => (250, 235, 215, 255),
        "aqua" | "cyan" => (0, 255, 255, 255),
        "aquamarine" => (127, 255, 212, 255),
        "azure" => (240, 255, 255, 255),
        "beige" => (245, 245, 220, 255),
        "bisque" => (255, 228, 196, 255),
        "black" => (0, 0, 0, 255),
        "blanchedalmond" => (255, 235, 205, 255),
        "blue" => (0, 0, 255, 255),
        "blueviolet" => (138, 43, 226, 255),
        "brown" => (165, 42, 42, 255),
        "burlywood" => (222, 184, 135, 255),
        "cadetblue" => (95, 158, 160, 255),
        "chartreuse" => (127, 255, 0, 255),
        "chocolate" => (210, 105, 30, 255),
        "coral" => (255, 127, 80, 255),
        "cornflowerblue" => (100, 149, 237, 255),
        "cornsilk" => (255, 248, 220, 255),
        "crimson" => (220, 20, 60, 255),
        "darkblue" => (0, 0, 139, 255),
        "darkcyan" => (0, 139, 139, 255),
        "darkgoldenrod" => (184, 134, 11, 255),
        "darkgray" | "darkgrey" => (169, 169, 169, 255),
        "darkgreen" => (0, 100, 0, 255),
        "darkkhaki" => (189, 183, 107, 255),
        "darkmagenta" => (139, 0, 139, 255),
        "darkolivegreen" => (85, 107, 47, 255),
        "darkorange" => (255, 140, 0, 255),
        "darkorchid" => (153, 50, 204, 255),
        "darkred" => (139, 0, 0, 255),
        "darksalmon" => (233, 150, 122, 255),
        "darkseagreen" => (143, 188, 143, 255),
        "darkslateblue" => (72, 61, 139, 255),
        "darkslategray" | "darkslategrey" => (47, 79, 79, 255),
        "darkturquoise" => (0, 206, 209, 255),
        "darkviolet" => (148, 0, 211, 255),
        "deeppink" => (255, 20, 147, 255),
        "deepskyblue" => (0, 191, 255, 255),
        "dimgray" | "dimgrey" => (105, 105, 105, 255),
        "dodgerblue" => (30, 144, 255, 255),
        "firebrick" => (178, 34, 34, 255),
        "floralwhite" => (255, 250, 240, 255),
        "forestgreen" => (34, 139, 34, 255),
        "fuchsia" | "magenta" => (255, 0, 255, 255),
        "gainsboro" => (220, 220, 220, 255),
        "ghostwhite" => (248, 248, 255, 255),
        "gold" => (255, 215, 0, 255),
        "goldenrod" => (218, 165, 32, 255),
        "gray" | "grey" => (128, 128, 128, 255),
        "green" => (0, 128, 0, 255),
        "greenyellow" => (173, 255, 47, 255),
        "honeydew" => (240, 255, 240, 255),
        "hotpink" => (255, 105, 180, 255),
        "indianred" => (205, 92, 92, 255),
        "indigo" => (75, 0, 130, 255),
        "ivory" => (255, 255, 240, 255),
        "khaki" => (240, 230, 140, 255),
        "lavender" => (230, 230, 250, 255),
        "lavenderblush" => (255, 240, 245, 255),
        "lawngreen" => (124, 252, 0, 255),
        "lemonchiffon" => (255, 250, 205, 255),
        "lightblue" => (173, 216, 230, 255),
        "lightcoral" => (240, 128, 128, 255),
        "lightcyan" => (224, 255, 255, 255),
        "lightgoldenrodyellow" => (250, 250, 210, 255),
        "lightgray" | "lightgrey" => (211, 211, 211, 255),
        "lightgreen" => (144, 238, 144, 255),
        "lightpink" => (255, 182, 193, 255),
        "lightsalmon" => (255, 160, 122, 255),
        "lightseagreen" => (32, 178, 170, 255),
        "lightskyblue" => (135, 206, 250, 255),
        "lightslategray" | "lightslategrey" => (119, 136, 153, 255),
        "lightsteelblue" => (176, 196, 222, 255),
        "lightyellow" => (255, 255, 224, 255),
        "lime" => (0, 255, 0, 255),
        "limegreen" => (50, 205, 50, 255),
        "linen" => (250, 240, 230, 255),
        "maroon" => (128, 0, 0, 255),
        "mediumaquamarine" => (102, 205, 170, 255),
        "mediumblue" => (0, 0, 205, 255),
        "mediumorchid" => (186, 85, 211, 255),
        "mediumpurple" => (147, 112, 219, 255),
        "mediumseagreen" => (60, 179, 113, 255),
        "mediumslateblue" => (123, 104, 238, 255),
        "mediumspringgreen" => (0, 250, 154, 255),
        "mediumturquoise" => (72, 209, 204, 255),
        "mediumvioletred" => (199, 21, 133, 255),
        "midnightblue" => (25, 25, 112, 255),
        "mintcream" => (245, 255, 250, 255),
        "mistyrose" => (255, 228, 225, 255),
        "moccasin" => (255, 228, 181, 255),
        "navajowhite" => (255, 222, 173, 255),
        "navy" => (0, 0, 128, 255),
        "oldlace" => (253, 245, 230, 255),
        "olive" => (128, 128, 0, 255),
        "olivedrab" => (107, 142, 35, 255),
        "orange" => (255, 165, 0, 255),
        "orangered" => (255, 69, 0, 255),
        "orchid" => (218, 112, 214, 255),
        "palegoldenrod" => (238, 232, 170, 255),
        "palegreen" => (152, 251, 152, 255),
        "paleturquoise" => (175, 238, 238, 255),
        "palevioletred" => (219, 112, 147, 255),
        "papayawhip" => (255, 239, 213, 255),
        "peachpuff" => (255, 218, 185, 255),
        "peru" => (205, 133, 63, 255),
        "pink" => (255, 192, 203, 255),
        "plum" => (221, 160, 221, 255),
        "powderblue" => (176, 224, 230, 255),
        "purple" => (128, 0, 128, 255),
        "rebeccapurple" => (102, 51, 153, 255),
        "red" => (255, 0, 0, 255),
        "rosybrown" => (188, 143, 143, 255),
        "royalblue" => (65, 105, 225, 255),
        "saddlebrown" => (139, 69, 19, 255),
        "salmon" => (250, 128, 114, 255),
        "sandybrown" => (244, 164, 96, 255),
        "seagreen" => (46, 139, 87, 255),
        "seashell" => (255, 245, 238, 255),
        "sienna" => (160, 82, 45, 255),
        "silver" => (192, 192, 192, 255),
        "skyblue" => (135, 206, 235, 255),
        "slateblue" => (106, 90, 205, 255),
        "slategray" | "slategrey" => (112, 128, 144, 255),
        "snow" => (255, 250, 250, 255),
        "springgreen" => (0, 255, 127, 255),
        "steelblue" => (70, 130, 180, 255),
        "tan" => (210, 180, 140, 255),
        "teal" => (0, 128, 128, 255),
        "thistle" => (216, 191, 216, 255),
        "tomato" => (255, 99, 71, 255),
        "transparent" => (0, 0, 0, 0),
        "turquoise" => (64, 224, 208, 255),
        "violet" => (238, 130, 238, 255),
        "wheat" => (245, 222, 179, 255),
        "white" => (255, 255, 255, 255),
        "whitesmoke" => (245, 245, 245, 255),
        "yellow" => (255, 255, 0, 255),
        "yellowgreen" => (154, 205, 50, 255),
        _ => return Err(CssColorParseError::InvalidColor(input)),
    };
    Ok(ColorU { r, g, b, a })
}

#[cfg(all(test, feature = "parser"))]
mod tests {
    use super::*;

    #[test]
    fn test_parse_color_keywords() {
        assert_eq!(parse_css_color("red").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("blue").unwrap(), ColorU::BLUE);
        assert_eq!(parse_css_color("transparent").unwrap(), ColorU::TRANSPARENT);
        assert_eq!(
            parse_css_color("rebeccapurple").unwrap(),
            ColorU::new_rgb(102, 51, 153)
        );
    }

    #[test]
    fn test_parse_color_hex() {
        // 3-digit
        assert_eq!(parse_css_color("#f00").unwrap(), ColorU::RED);
        // 4-digit
        assert_eq!(
            parse_css_color("#f008").unwrap(),
            ColorU::new(255, 0, 0, 136)
        );
        // 6-digit
        assert_eq!(parse_css_color("#00ff00").unwrap(), ColorU::GREEN);
        // 8-digit
        assert_eq!(
            parse_css_color("#0000ff80").unwrap(),
            ColorU::new(0, 0, 255, 128)
        );
        // Uppercase
        assert_eq!(
            parse_css_color("#FFC0CB").unwrap(),
            ColorU::new_rgb(255, 192, 203)
        ); // Pink
    }

    #[test]
    fn test_parse_color_rgb() {
        assert_eq!(parse_css_color("rgb(255, 0, 0)").unwrap(), ColorU::RED);
        assert_eq!(
            parse_css_color("rgba(0, 255, 0, 0.5)").unwrap(),
            ColorU::new(0, 255, 0, 128)
        );
        assert_eq!(
            parse_css_color("rgba(10, 20, 30, 1)").unwrap(),
            ColorU::new_rgb(10, 20, 30)
        );
        assert_eq!(parse_css_color("rgb( 0 , 0 , 0 )").unwrap(), ColorU::BLACK);
    }

    #[test]
    fn test_parse_color_hsl() {
        assert_eq!(parse_css_color("hsl(0, 100%, 50%)").unwrap(), ColorU::RED);
        assert_eq!(
            parse_css_color("hsl(120, 100%, 50%)").unwrap(),
            ColorU::GREEN
        );
        assert_eq!(
            parse_css_color("hsla(240, 100%, 50%, 0.5)").unwrap(),
            ColorU::new(0, 0, 255, 128)
        );
        assert_eq!(parse_css_color("hsl(0, 0%, 0%)").unwrap(), ColorU::BLACK);
    }

    #[test]
    fn test_parse_color_errors() {
        assert!(parse_css_color("redd").is_err());
        assert!(parse_css_color("#12345").is_err()); // Invalid length
        assert!(parse_css_color("#ggg").is_err()); // Invalid hex digit
        assert!(parse_css_color("rgb(255, 0)").is_err()); // Missing component
        assert!(parse_css_color("rgba(255, 0, 0, 2)").is_err()); // Alpha out of range
        assert!(parse_css_color("rgb(256, 0, 0)").is_err()); // Value out of range
                                                             // Modern CSS allows both hsl(0, 100%, 50%) and hsl(0 100 50)
        assert!(parse_css_color("hsl(0, 100, 50%)").is_ok()); // Valid in modern CSS
        assert!(parse_css_color("rgb(255 0 0)").is_err()); // Missing commas (this implementation
                                                           // requires commas)
    }

    #[test]
    fn test_parse_system_colors() {
        // Test parsing system color syntax
        assert_eq!(
            parse_color_or_system("system:accent").unwrap(),
            ColorOrSystem::System(SystemColorRef::Accent)
        );
        assert_eq!(
            parse_color_or_system("system:text").unwrap(),
            ColorOrSystem::System(SystemColorRef::Text)
        );
        assert_eq!(
            parse_color_or_system("system:background").unwrap(),
            ColorOrSystem::System(SystemColorRef::Background)
        );
        assert_eq!(
            parse_color_or_system("system:selection-background").unwrap(),
            ColorOrSystem::System(SystemColorRef::SelectionBackground)
        );
        assert_eq!(
            parse_color_or_system("system:selection-text").unwrap(),
            ColorOrSystem::System(SystemColorRef::SelectionText)
        );
        assert_eq!(
            parse_color_or_system("system:accent-text").unwrap(),
            ColorOrSystem::System(SystemColorRef::AccentText)
        );
        assert_eq!(
            parse_color_or_system("system:button-face").unwrap(),
            ColorOrSystem::System(SystemColorRef::ButtonFace)
        );
        assert_eq!(
            parse_color_or_system("system:button-text").unwrap(),
            ColorOrSystem::System(SystemColorRef::ButtonText)
        );
        assert_eq!(
            parse_color_or_system("system:window-background").unwrap(),
            ColorOrSystem::System(SystemColorRef::WindowBackground)
        );
        
        // Invalid system color should error
        assert!(parse_color_or_system("system:invalid").is_err());
        
        // Regular colors should still work
        assert_eq!(
            parse_color_or_system("red").unwrap(),
            ColorOrSystem::Color(ColorU::RED)
        );
        assert_eq!(
            parse_color_or_system("#ff0000").unwrap(),
            ColorOrSystem::Color(ColorU::RED)
        );
    }

    #[test]
    fn test_system_color_resolution() {
        use crate::system::SystemColors;
        
        let system_colors = SystemColors {
            text: OptionColorU::Some(ColorU::BLACK),
            secondary_text: OptionColorU::None,
            tertiary_text: OptionColorU::None,
            background: OptionColorU::Some(ColorU::WHITE),
            accent: OptionColorU::Some(ColorU::new_rgb(0, 122, 255)), // macOS blue
            accent_text: OptionColorU::Some(ColorU::WHITE),
            button_face: OptionColorU::Some(ColorU::new_rgb(240, 240, 240)),
            button_text: OptionColorU::Some(ColorU::BLACK),
            disabled_text: OptionColorU::None,
            window_background: OptionColorU::Some(ColorU::WHITE),
            under_page_background: OptionColorU::None,
            selection_background: OptionColorU::Some(ColorU::new_rgb(0, 120, 215)),
            selection_text: OptionColorU::Some(ColorU::WHITE),
            selection_background_inactive: OptionColorU::None,
            selection_text_inactive: OptionColorU::None,
            link: OptionColorU::None,
            separator: OptionColorU::None,
            grid: OptionColorU::None,
            find_highlight: OptionColorU::None,
            sidebar_background: OptionColorU::None,
            sidebar_selection: OptionColorU::None,
        };
        
        // Test resolution of system colors
        let accent_ref = ColorOrSystem::System(SystemColorRef::Accent);
        let resolved = accent_ref.resolve(&system_colors, ColorU::GRAY);
        assert_eq!(resolved, ColorU::new_rgb(0, 122, 255));
        
        // Test resolution with fallback when color is not set
        let empty_colors = SystemColors::default();
        let resolved_fallback = accent_ref.resolve(&empty_colors, ColorU::GRAY);
        assert_eq!(resolved_fallback, ColorU::GRAY);
        
        // Test that concrete colors just return themselves
        let concrete = ColorOrSystem::Color(ColorU::RED);
        let resolved_concrete = concrete.resolve(&system_colors, ColorU::GRAY);
        assert_eq!(resolved_concrete, ColorU::RED);
    }

    #[test]
    fn test_system_color_css_str() {
        assert_eq!(SystemColorRef::Accent.as_css_str(), "system:accent");
        assert_eq!(SystemColorRef::Text.as_css_str(), "system:text");
        assert_eq!(SystemColorRef::Background.as_css_str(), "system:background");
        assert_eq!(SystemColorRef::SelectionBackground.as_css_str(), "system:selection-background");
    }
}

#[cfg(test)]
#[allow(clippy::float_cmp, clippy::unreadable_literal)]
mod autotest_generated {
    use super::*;

    /// Every `ColorU` this module sweeps over. Chosen to hit the interesting
    /// channel boundaries (0 / 1 / 127 / 128 / 254 / 255) plus a few real colors.
    const SAMPLES: [ColorU; 10] = [
        ColorU { r: 0, g: 0, b: 0, a: 0 },
        ColorU { r: 0, g: 0, b: 0, a: 255 },
        ColorU { r: 255, g: 255, b: 255, a: 255 },
        ColorU { r: 255, g: 255, b: 255, a: 0 },
        ColorU { r: 1, g: 2, b: 3, a: 4 },
        ColorU { r: 127, g: 128, b: 129, a: 254 },
        ColorU { r: 254, g: 1, b: 128, a: 1 },
        ColorU { r: 128, g: 128, b: 128, a: 255 },
        ColorU { r: 255, g: 0, b: 0, a: 255 },
        ColorU { r: 13, g: 110, b: 253, a: 200 },
    ];

    // =====================================================================
    // numeric: channel_to_u8 (private) — the one float→int cast in the file
    // =====================================================================

    #[test]
    fn channel_to_u8_zero_and_negative_zero() {
        assert_eq!(channel_to_u8(0.0), 0);
        assert_eq!(channel_to_u8(-0.0), 0);
    }

    #[test]
    fn channel_to_u8_truncates_toward_zero_and_does_not_round() {
        assert_eq!(channel_to_u8(0.9), 0);
        assert_eq!(channel_to_u8(127.5), 127);
        assert_eq!(channel_to_u8(254.999), 254);
        assert_eq!(channel_to_u8(255.0), 255);
        assert_eq!(channel_to_u8(255.9), 255);
    }

    #[test]
    fn channel_to_u8_saturates_on_overflow_instead_of_wrapping() {
        assert_eq!(channel_to_u8(256.0), 255);
        assert_eq!(channel_to_u8(1e30), 255);
        assert_eq!(channel_to_u8(f32::MAX), 255);
    }

    #[test]
    fn channel_to_u8_negative_saturates_to_zero() {
        assert_eq!(channel_to_u8(-0.5), 0);
        assert_eq!(channel_to_u8(-1.0), 0);
        assert_eq!(channel_to_u8(-1e30), 0);
        assert_eq!(channel_to_u8(f32::MIN), 0);
    }

    #[test]
    fn channel_to_u8_nan_and_inf_are_defined_and_do_not_panic() {
        assert_eq!(channel_to_u8(f32::NAN), 0);
        assert_eq!(channel_to_u8(-f32::NAN), 0);
        assert_eq!(channel_to_u8(f32::INFINITY), 255);
        assert_eq!(channel_to_u8(f32::NEG_INFINITY), 0);
    }

    #[test]
    fn channel_to_u8_subnormal_inputs_do_not_panic() {
        assert_eq!(channel_to_u8(f32::MIN_POSITIVE), 0);
        assert_eq!(channel_to_u8(1e-45), 0);
        assert_eq!(channel_to_u8(-1e-45), 0);
    }

    // =====================================================================
    // constructors: rgba / rgb / new / new_rgb / with_alpha / with_alpha_f32
    // =====================================================================

    #[test]
    fn rgba_fields_match_args_at_min_and_max() {
        let min = ColorU::rgba(0, 0, 0, 0);
        assert_eq!((min.r, min.g, min.b, min.a), (0, 0, 0, 0));
        let max = ColorU::rgba(u8::MAX, u8::MAX, u8::MAX, u8::MAX);
        assert_eq!((max.r, max.g, max.b, max.a), (255, 255, 255, 255));
        let mixed = ColorU::rgba(1, 2, 3, 4);
        assert_eq!((mixed.r, mixed.g, mixed.b, mixed.a), (1, 2, 3, 4));
    }

    #[test]
    fn rgb_defaults_alpha_to_opaque() {
        assert_eq!(ColorU::rgb(0, 0, 0), ColorU::BLACK);
        assert_eq!(ColorU::rgb(1, 2, 3).a, ColorU::ALPHA_OPAQUE);
        assert_eq!(ColorU::rgb(u8::MAX, u8::MAX, u8::MAX), ColorU::WHITE);
    }

    #[test]
    fn new_and_new_rgb_are_exact_aliases() {
        for c in SAMPLES {
            assert_eq!(ColorU::new(c.r, c.g, c.b, c.a), ColorU::rgba(c.r, c.g, c.b, c.a));
            assert_eq!(ColorU::new_rgb(c.r, c.g, c.b), ColorU::rgb(c.r, c.g, c.b));
        }
    }

    #[test]
    fn with_alpha_keeps_rgb_for_every_alpha() {
        let base = ColorU::rgba(13, 110, 253, 7);
        for a in 0..=u8::MAX {
            let c = base.with_alpha(a);
            assert_eq!((c.r, c.g, c.b), (base.r, base.g, base.b));
            assert_eq!(c.a, a);
        }
    }

    #[test]
    fn with_alpha_f32_clamps_out_of_range_and_nan() {
        let base = ColorU::rgb(1, 2, 3);
        assert_eq!(base.with_alpha_f32(0.0).a, 0);
        assert_eq!(base.with_alpha_f32(1.0).a, 255);
        // Out of range clamps rather than wrapping.
        assert_eq!(base.with_alpha_f32(-1.0).a, 0);
        assert_eq!(base.with_alpha_f32(-1e30).a, 0);
        assert_eq!(base.with_alpha_f32(2.0).a, 255);
        assert_eq!(base.with_alpha_f32(1e30).a, 255);
        assert_eq!(base.with_alpha_f32(f32::INFINITY).a, 255);
        assert_eq!(base.with_alpha_f32(f32::NEG_INFINITY).a, 0);
        // `clamp` propagates NaN, and `NaN as u8` is 0 — fully transparent, not a panic.
        assert_eq!(base.with_alpha_f32(f32::NAN).a, 0);
        // RGB is never touched, whatever the alpha input.
        for a in [-1.0, 0.0, 0.5, 1.0, 2.0, f32::NAN, f32::INFINITY] {
            let c = base.with_alpha_f32(a);
            assert_eq!((c.r, c.g, c.b), (1, 2, 3));
        }
    }

    #[test]
    fn with_alpha_f32_truncates_rather_than_rounds() {
        // 0.5 * 255.0 == 127.5, and `as u8` truncates => 127.
        // NOTE: the `rgba(..., 0.5)` parser rounds the same value to 128
        // (`parse_alpha_component` calls `.round()` first). See report.
        assert_eq!(ColorU::rgb(0, 0, 0).with_alpha_f32(0.5).a, 127);
    }

    // =====================================================================
    // numeric: interpolate / lighten / darken / mix
    // =====================================================================

    #[test]
    fn interpolate_endpoints_are_exact() {
        for a in SAMPLES {
            for b in SAMPLES {
                assert_eq!(a.interpolate(&b, 0.0), a, "t=0 must return self");
                assert_eq!(a.interpolate(&b, 1.0), b, "t=1 must return other");
            }
        }
    }

    #[test]
    fn interpolate_midpoint_rounds_half_away_from_zero() {
        // 0 + 255 * 0.5 = 127.5, roundf => 128.
        assert_eq!(
            ColorU::BLACK.interpolate(&ColorU::WHITE, 0.5),
            ColorU::rgba(128, 128, 128, 255)
        );
    }

    #[test]
    fn interpolate_is_symmetric_under_swapped_endpoints() {
        for a in SAMPLES {
            for b in SAMPLES {
                assert_eq!(a.interpolate(&b, 0.25), b.interpolate(&a, 0.75));
            }
        }
    }

    #[test]
    fn interpolate_nan_t_is_defined_and_does_not_panic() {
        // t = NaN makes every channel NaN, and `NaN as u8` == 0.
        for a in SAMPLES {
            for b in SAMPLES {
                assert_eq!(a.interpolate(&b, f32::NAN), ColorU::rgba(0, 0, 0, 0));
            }
        }
    }

    #[test]
    fn interpolate_infinite_t_saturates_differing_channels() {
        // Channels that differ run off to +/-inf and saturate at the u8 bounds.
        let c = ColorU::rgba(0, 0, 0, 0).interpolate(&ColorU::WHITE, f32::INFINITY);
        assert_eq!(c, ColorU::rgba(255, 255, 255, 255));
        let c = ColorU::WHITE.interpolate(&ColorU::rgba(0, 0, 0, 0), f32::INFINITY);
        assert_eq!(c, ColorU::rgba(0, 0, 0, 0));
    }

    #[test]
    fn interpolate_infinite_t_zeroes_equal_channels() {
        // Where a channel is EQUAL in both colors the delta is 0.0, and
        // `0.0 * inf == NaN` => that channel collapses to 0 instead of
        // staying put. Both endpoints here are alpha=255, so alpha => 0.
        let c = ColorU::BLACK.interpolate(&ColorU::WHITE, f32::INFINITY);
        assert_eq!(c, ColorU::rgba(255, 255, 255, 0));
        // Interpolating a color with ITSELF at t=inf wipes it out entirely.
        assert_eq!(
            ColorU::RED.interpolate(&ColorU::RED, f32::INFINITY),
            ColorU::rgba(0, 0, 0, 0)
        );
    }

    #[test]
    fn interpolate_out_of_range_t_saturates_instead_of_wrapping() {
        // Extrapolating past the endpoints overshoots the u8 range; the cast must
        // saturate, not wrap (0 + 255*2 == 510 -> 255, not 254).
        assert_eq!(
            ColorU::BLACK.interpolate(&ColorU::WHITE, 2.0),
            ColorU::rgba(255, 255, 255, 255)
        );
        assert_eq!(
            ColorU::WHITE.interpolate(&ColorU::BLACK, -1.0),
            ColorU::rgba(255, 255, 255, 255)
        );
        assert_eq!(
            ColorU::WHITE.interpolate(&ColorU::BLACK, 2.0),
            ColorU::rgba(0, 0, 0, 255)
        );
        assert_eq!(
            ColorU::BLACK.interpolate(&ColorU::WHITE, -1.0),
            ColorU::rgba(0, 0, 0, 255)
        );
        // And the whole sample matrix must stay panic-free and deterministic.
        for t in [-1e30, -1.0, -0.5, 1.5, 2.0, 1e30] {
            for a in SAMPLES {
                for b in SAMPLES {
                    assert_eq!(a.interpolate(&b, t), a.interpolate(&b, t));
                }
            }
        }
    }

    #[test]
    fn lighten_and_darken_clamp_the_amount() {
        let base = ColorU::rgba(128, 128, 128, 77);
        // Below 0 clamps to 0 => unchanged.
        assert_eq!(base.lighten(0.0), base);
        assert_eq!(base.darken(0.0), base);
        assert_eq!(base.lighten(-1.0), base);
        assert_eq!(base.darken(-1e30), base);
        assert_eq!(base.lighten(f32::NEG_INFINITY), base);
        // Above 1 clamps to 1 => full white / full black, alpha preserved.
        assert_eq!(base.lighten(1.0), ColorU::rgba(255, 255, 255, 77));
        assert_eq!(base.lighten(2.0), ColorU::rgba(255, 255, 255, 77));
        assert_eq!(base.lighten(f32::INFINITY), ColorU::rgba(255, 255, 255, 77));
        assert_eq!(base.darken(1.0), ColorU::rgba(0, 0, 0, 77));
        assert_eq!(base.darken(1e30), ColorU::rgba(0, 0, 0, 77));
        assert_eq!(base.darken(f32::INFINITY), ColorU::rgba(0, 0, 0, 77));
    }

    #[test]
    fn lighten_and_darken_always_preserve_alpha() {
        for c in SAMPLES {
            for amount in [-1.0, 0.0, 0.3, 1.0, 2.0, f32::NAN, f32::INFINITY] {
                assert_eq!(c.lighten(amount).a, c.a);
                assert_eq!(c.darken(amount).a, c.a);
            }
        }
    }

    #[test]
    fn lighten_nan_amount_is_defined_and_does_not_panic() {
        // `f32::clamp` propagates NaN, so the RGB channels collapse to 0 while
        // alpha is explicitly restored afterwards.
        let c = ColorU::rgba(255, 0, 0, 200);
        assert_eq!(c.lighten(f32::NAN), ColorU::rgba(0, 0, 0, 200));
        assert_eq!(c.darken(f32::NAN), ColorU::rgba(0, 0, 0, 200));
    }

    #[test]
    fn mix_clamps_ratio_to_the_endpoints() {
        let a = ColorU::rgba(10, 20, 30, 40);
        let b = ColorU::rgba(200, 210, 220, 230);
        assert_eq!(a.mix(&b, 0.0), a);
        assert_eq!(a.mix(&b, 1.0), b);
        assert_eq!(a.mix(&b, -1.0), a);
        assert_eq!(a.mix(&b, f32::NEG_INFINITY), a);
        assert_eq!(a.mix(&b, 2.0), b);
        assert_eq!(a.mix(&b, 1e30), b);
        assert_eq!(a.mix(&b, f32::INFINITY), b);
    }

    #[test]
    fn mix_nan_ratio_is_defined_and_does_not_panic() {
        // Unlike lighten/darken, mix does NOT restore alpha => fully transparent.
        assert_eq!(
            ColorU::RED.mix(&ColorU::BLUE, f32::NAN),
            ColorU::rgba(0, 0, 0, 0)
        );
    }

    // =====================================================================
    // numeric: srgb_to_linear (private)
    // =====================================================================

    #[test]
    fn srgb_to_linear_endpoints_and_monotonicity() {
        assert_eq!(ColorU::srgb_to_linear(0.0), 0.0);
        assert!((ColorU::srgb_to_linear(1.0) - 1.0).abs() < 1e-5);
        // Monotonically non-decreasing over the whole 8-bit ramp.
        let mut prev = f32::NEG_INFINITY;
        for i in 0..=255u16 {
            let v = ColorU::srgb_to_linear(f32::from(i) / 255.0);
            assert!(v >= prev, "srgb_to_linear not monotonic at {i}");
            assert!((0.0..=1.0).contains(&v), "out of range at {i}: {v}");
            prev = v;
        }
    }

    #[test]
    fn srgb_to_linear_handles_the_piecewise_boundary() {
        // The branch flips at c == 0.03928 (linear below, gamma above).
        let below = ColorU::srgb_to_linear(0.03928);
        assert!((below - 0.03928 / 12.92).abs() < 1e-9);
        let above = ColorU::srgb_to_linear(0.03929);
        assert!(above > below, "must not go backwards across the boundary");
    }

    #[test]
    fn srgb_to_linear_nan_inf_and_negative_do_not_panic() {
        assert!(ColorU::srgb_to_linear(f32::NAN).is_nan());
        assert_eq!(ColorU::srgb_to_linear(f32::INFINITY), f32::INFINITY);
        assert_eq!(ColorU::srgb_to_linear(f32::NEG_INFINITY), f32::NEG_INFINITY);
        // Negative inputs take the linear branch and stay negative (deterministic).
        assert!(ColorU::srgb_to_linear(-1.0) < 0.0);
        assert_eq!(ColorU::srgb_to_linear(-0.0), -0.0);
    }

    // =====================================================================
    // getters: luminance / relative_luminance / is_light / is_dark
    // =====================================================================

    #[test]
    fn luminance_endpoints_and_range() {
        assert!((ColorU::BLACK.luminance() - 0.0).abs() < 1e-6);
        assert!((ColorU::WHITE.luminance() - 1.0).abs() < 1e-6);
        for r in (0..=255u16).step_by(17) {
            for g in (0..=255u16).step_by(51) {
                for b in (0..=255u16).step_by(85) {
                    #[allow(clippy::cast_possible_truncation)]
                    let l = ColorU::rgb(r as u8, g as u8, b as u8).luminance();
                    assert!(l.is_finite() && (-1e-6..=1.000_001).contains(&l), "luminance {l}");
                }
            }
        }
    }

    #[test]
    fn luminance_ignores_alpha() {
        for a in [0u8, 1, 128, 254, 255] {
            assert!((ColorU::rgba(10, 20, 30, a).luminance()
                - ColorU::rgba(10, 20, 30, 255).luminance())
                .abs()
                < 1e-9);
        }
    }

    #[test]
    fn relative_luminance_endpoints_and_range() {
        assert!((ColorU::BLACK.relative_luminance() - 0.0).abs() < 1e-6);
        assert!((ColorU::WHITE.relative_luminance() - 1.0).abs() < 1e-6);
        for i in 0..=255u16 {
            #[allow(clippy::cast_possible_truncation)]
            let l = ColorU::rgb(i as u8, i as u8, i as u8).relative_luminance();
            assert!(l.is_finite(), "non-finite relative_luminance at {i}");
            assert!((-1e-6..=1.000_001).contains(&l), "out of range at {i}: {l}");
        }
    }

    #[test]
    fn relative_luminance_is_monotonic_along_the_gray_ramp() {
        let mut prev = f32::NEG_INFINITY;
        for i in 0..=255u16 {
            #[allow(clippy::cast_possible_truncation)]
            let l = ColorU::rgb(i as u8, i as u8, i as u8).relative_luminance();
            assert!(l >= prev, "gray ramp not monotonic at {i}");
            prev = l;
        }
    }

    #[test]
    fn is_light_and_is_dark_are_exact_complements() {
        // The two predicates split at exactly 0.5 with no overlap and no gap,
        // for every single 8-bit color on the gray ramp plus the samples.
        for i in 0..=255u16 {
            #[allow(clippy::cast_possible_truncation)]
            let c = ColorU::rgb(i as u8, i as u8, i as u8);
            assert_ne!(c.is_light(), c.is_dark(), "not complementary at {i}");
        }
        for c in SAMPLES {
            assert_ne!(c.is_light(), c.is_dark());
        }
    }

    #[test]
    fn is_light_and_is_dark_known_values() {
        assert!(ColorU::WHITE.is_light());
        assert!(!ColorU::WHITE.is_dark());
        assert!(ColorU::BLACK.is_dark());
        assert!(!ColorU::BLACK.is_light());
        // Default (BLACK) is dark.
        assert!(ColorU::default().is_dark());
        // Mid gray is "dark" under WCAG relative luminance (~0.216, not 0.5).
        assert!(ColorU::rgb(128, 128, 128).is_dark());
    }

    // =====================================================================
    // contrast: contrast_ratio / meets_wcag_* / best_contrast_text
    // =====================================================================

    #[test]
    fn contrast_ratio_is_symmetric() {
        for a in SAMPLES {
            for b in SAMPLES {
                let ab = a.contrast_ratio(&b);
                let ba = b.contrast_ratio(&a);
                assert!((ab - ba).abs() < 1e-6, "asymmetric: {ab} vs {ba}");
            }
        }
    }

    #[test]
    fn contrast_ratio_stays_within_1_and_21() {
        for a in SAMPLES {
            for b in SAMPLES {
                let r = a.contrast_ratio(&b);
                assert!(r.is_finite(), "non-finite contrast ratio");
                assert!((0.999..=21.001).contains(&r), "contrast ratio out of range: {r}");
            }
            // Self-contrast is exactly 1.
            assert!((a.contrast_ratio(&a) - 1.0).abs() < 1e-6);
        }
        // Max contrast (fp gives 20.999998, not a clean 21.0).
        let max = ColorU::BLACK.contrast_ratio(&ColorU::WHITE);
        assert!((max - 21.0).abs() < 0.01, "black/white contrast was {max}");
    }

    #[test]
    fn meets_wcag_thresholds_agree_with_contrast_ratio() {
        for a in SAMPLES {
            for b in SAMPLES {
                let r = a.contrast_ratio(&b);
                assert_eq!(a.meets_wcag_aa(&b), r >= 4.5);
                assert_eq!(a.meets_wcag_aa_large(&b), r >= 3.0);
                assert_eq!(a.meets_wcag_aaa(&b), r >= 7.0);
                assert_eq!(a.meets_wcag_aaa_large(&b), r >= 4.5);
            }
        }
    }

    #[test]
    fn meets_wcag_known_true_and_false() {
        assert!(ColorU::BLACK.meets_wcag_aa(&ColorU::WHITE));
        assert!(ColorU::BLACK.meets_wcag_aaa(&ColorU::WHITE));
        assert!(ColorU::WHITE.meets_wcag_aa_large(&ColorU::BLACK));
        // A color has no contrast against itself.
        assert!(!ColorU::RED.meets_wcag_aa(&ColorU::RED));
        assert!(!ColorU::RED.meets_wcag_aa_large(&ColorU::RED));
        assert!(!ColorU::WHITE.meets_wcag_aaa(&ColorU::WHITE));
    }

    #[test]
    fn best_contrast_text_only_ever_returns_black_or_white() {
        for c in SAMPLES {
            let t = c.best_contrast_text();
            assert!(t == ColorU::WHITE || t == ColorU::BLACK, "got {t:?}");
            // contrast_text is documented as an alias.
            assert_eq!(c.contrast_text(), t);
        }
        for i in 0..=255u16 {
            #[allow(clippy::cast_possible_truncation)]
            let c = ColorU::rgb(i as u8, i as u8, i as u8);
            let t = c.best_contrast_text();
            assert!(t == ColorU::WHITE || t == ColorU::BLACK);
        }
    }

    #[test]
    fn best_contrast_text_picks_the_higher_contrast_option() {
        assert_eq!(ColorU::WHITE.best_contrast_text(), ColorU::BLACK);
        assert_eq!(ColorU::BLACK.best_contrast_text(), ColorU::WHITE);
        for c in SAMPLES {
            let t = c.best_contrast_text();
            let other = if t == ColorU::WHITE { ColorU::BLACK } else { ColorU::WHITE };
            assert!(
                c.contrast_ratio(&t) >= c.contrast_ratio(&other),
                "{c:?} picked the lower-contrast text color"
            );
        }
    }

    // =====================================================================
    // numeric: ensure_contrast (binary search — must terminate + saturate)
    // =====================================================================

    #[test]
    fn ensure_contrast_returns_self_when_already_compliant() {
        // 21:1 already, nothing to do.
        assert_eq!(
            ColorU::BLACK.ensure_contrast(&ColorU::WHITE, 4.5),
            ColorU::BLACK
        );
        let gray = ColorU::rgb(128, 128, 128);
        // 5.3:1 against black already clears 4.5.
        assert_eq!(gray.ensure_contrast(&ColorU::BLACK, 4.5), gray);
    }

    #[test]
    fn ensure_contrast_actually_reaches_the_requested_ratio() {
        let gray = ColorU::rgb(128, 128, 128);
        let fixed = gray.ensure_contrast(&ColorU::WHITE, 4.5);
        assert!(
            fixed.contrast_ratio(&ColorU::WHITE) >= 4.5,
            "adjusted color {fixed:?} still fails 4.5:1"
        );
        // Darkening against a light background must not make it lighter.
        assert!(fixed.r <= gray.r && fixed.g <= gray.g && fixed.b <= gray.b);
    }

    #[test]
    fn ensure_contrast_degenerate_min_ratios_return_self() {
        let gray = ColorU::rgb(128, 128, 128);
        // <= current ratio: early return.
        assert_eq!(gray.ensure_contrast(&ColorU::WHITE, 0.0), gray);
        assert_eq!(gray.ensure_contrast(&ColorU::WHITE, -1.0), gray);
        assert_eq!(gray.ensure_contrast(&ColorU::WHITE, f32::NEG_INFINITY), gray);
        // Unsatisfiable / NaN: every comparison is false, so `result` never
        // moves off `*self`. Terminates (fixed 16 iterations), never hangs.
        assert_eq!(gray.ensure_contrast(&ColorU::WHITE, f32::INFINITY), gray);
        assert_eq!(gray.ensure_contrast(&ColorU::WHITE, f32::NAN), gray);
        assert_eq!(gray.ensure_contrast(&ColorU::WHITE, 1e30), gray);
    }

    #[test]
    fn ensure_contrast_terminates_for_every_sample_pair() {
        for c in SAMPLES {
            for bg in SAMPLES {
                for min in [1.0, 3.0, 4.5, 7.0, 21.0, 25.0] {
                    let out = c.ensure_contrast(&bg, min);
                    // Alpha is carried through lighten/darken untouched.
                    assert_eq!(out.a, c.a);
                }
            }
        }
    }

    // =====================================================================
    // APCA
    // =====================================================================

    #[test]
    fn apca_contrast_sign_encodes_polarity() {
        let dark_on_light = ColorU::BLACK.apca_contrast(&ColorU::WHITE);
        let light_on_dark = ColorU::WHITE.apca_contrast(&ColorU::BLACK);
        assert!(dark_on_light > 0.0, "black-on-white should be positive");
        assert!(light_on_dark < 0.0, "white-on-black should be negative");
        assert!(dark_on_light.is_finite() && light_on_dark.is_finite());
    }

    #[test]
    fn apca_contrast_of_a_color_against_itself_is_zero() {
        for c in SAMPLES {
            assert_eq!(c.apca_contrast(&c), 0.0, "{c:?} vs itself");
        }
    }

    #[test]
    fn apca_contrast_is_finite_for_every_sample_pair() {
        for a in SAMPLES {
            for b in SAMPLES {
                assert!(a.apca_contrast(&b).is_finite(), "{a:?} on {b:?}");
            }
        }
    }

    #[test]
    fn meets_apca_thresholds_agree_with_apca_contrast() {
        for a in SAMPLES {
            for b in SAMPLES {
                let lc = libm::fabsf(a.apca_contrast(&b));
                assert_eq!(a.meets_apca_body(&b), lc >= 60.0);
                assert_eq!(a.meets_apca_large(&b), lc >= 45.0);
            }
        }
        assert!(ColorU::BLACK.meets_apca_body(&ColorU::WHITE));
        assert!(ColorU::BLACK.meets_apca_large(&ColorU::WHITE));
        assert!(!ColorU::RED.meets_apca_body(&ColorU::RED));
        assert!(!ColorU::RED.meets_apca_large(&ColorU::RED));
    }

    // =====================================================================
    // getters / predicates: hover_variant, active_variant, invert,
    //                       to_grayscale, has_alpha, to_hash
    // =====================================================================

    #[test]
    fn hover_and_active_variants_preserve_alpha_and_never_panic() {
        for c in SAMPLES {
            assert_eq!(c.hover_variant().a, c.a);
            assert_eq!(c.active_variant().a, c.a);
        }
        // Light colors get darker, dark colors get lighter.
        assert!(ColorU::WHITE.hover_variant().r < 255);
        assert!(ColorU::BLACK.hover_variant().r > 0);
        assert!(ColorU::WHITE.active_variant().r < ColorU::WHITE.hover_variant().r);
    }

    #[test]
    fn invert_is_its_own_inverse() {
        for c in SAMPLES {
            assert_eq!(c.invert().invert(), c);
            assert_eq!(c.invert().a, c.a, "invert must keep alpha");
        }
        assert_eq!(ColorU::BLACK.invert(), ColorU::WHITE);
        assert_eq!(ColorU::WHITE.invert(), ColorU::BLACK);
    }

    #[test]
    fn invert_does_not_underflow_at_the_channel_bounds() {
        // `255 - self.r` on u8 would panic in debug on underflow; it cannot,
        // but pin the boundary values anyway.
        assert_eq!(ColorU::rgba(0, 0, 0, 0).invert(), ColorU::rgba(255, 255, 255, 0));
        assert_eq!(
            ColorU::rgba(255, 255, 255, 255).invert(),
            ColorU::rgba(0, 0, 0, 255)
        );
    }

    #[test]
    fn to_grayscale_produces_equal_channels_and_keeps_alpha() {
        for c in SAMPLES {
            let g = c.to_grayscale();
            assert_eq!(g.r, g.g);
            assert_eq!(g.g, g.b);
            assert_eq!(g.a, c.a);
        }
    }

    #[test]
    fn to_grayscale_boundary_values() {
        assert_eq!(ColorU::BLACK.to_grayscale(), ColorU::BLACK);
        assert_eq!(ColorU::WHITE.to_grayscale(), ColorU::WHITE);
        assert_eq!(
            ColorU::rgb(128, 128, 128).to_grayscale(),
            ColorU::rgb(128, 128, 128)
        );
        // An already-gray color is (near enough) a fixed point of to_grayscale:
        // the BT.601 weights sum to 1.0, so only the truncating cast can shave
        // off at most one level.
        for i in 0..=255u16 {
            #[allow(clippy::cast_possible_truncation)]
            let c = ColorU::rgb(i as u8, i as u8, i as u8);
            let drift = i32::from(c.r) - i32::from(c.to_grayscale().r);
            assert!((0..=1).contains(&drift), "gray {i} drifted by {drift}");
        }
    }

    #[test]
    fn has_alpha_is_true_for_everything_but_255() {
        assert!(!ColorU::rgba(0, 0, 0, 255).has_alpha());
        assert!(!ColorU::WHITE.has_alpha());
        assert!(ColorU::rgba(0, 0, 0, 254).has_alpha());
        assert!(ColorU::TRANSPARENT.has_alpha());
        for a in 0..=u8::MAX {
            assert_eq!(ColorU::rgba(1, 2, 3, a).has_alpha(), a != 255);
        }
    }

    #[test]
    fn to_hash_is_always_nine_lowercase_chars() {
        assert_eq!(ColorU::RED.to_hash(), "#ff0000ff");
        assert_eq!(ColorU::TRANSPARENT.to_hash(), "#00000000");
        assert_eq!(ColorU::rgba(1, 2, 3, 4).to_hash(), "#01020304");
        assert_eq!(ColorU::WHITE.to_hash(), "#ffffffff");
        for c in SAMPLES {
            let h = c.to_hash();
            assert_eq!(h.len(), 9, "{h} is not 9 bytes");
            assert!(h.starts_with('#'));
            assert!(
                h[1..].chars().all(|ch| ch.is_ascii_hexdigit() && !ch.is_ascii_uppercase()),
                "{h} is not lowercase hex"
            );
        }
    }

    // =====================================================================
    // serializer: Display for ColorU / ColorF
    // =====================================================================

    #[test]
    fn coloru_display_is_well_formed() {
        assert_eq!(format!("{}", ColorU::RED), "rgba(255, 0, 0, 1)");
        assert_eq!(format!("{}", ColorU::TRANSPARENT), "rgba(0, 0, 0, 0)");
        assert_eq!(format!("{}", ColorU::default()), "rgba(0, 0, 0, 1)");
        // Alpha is normalized to 0.0..=1.0.
        assert_eq!(format!("{}", ColorU::rgba(1, 2, 3, 128)), "rgba(1, 2, 3, 0.5019608)");
        for c in SAMPLES {
            let s = format!("{c}");
            assert!(s.starts_with("rgba(") && s.ends_with(')') && s.len() > 6);
        }
    }

    #[test]
    fn colorf_display_survives_nan_and_inf() {
        assert_eq!(format!("{}", ColorF::BLACK), "rgba(0, 0, 0, 1)");
        assert_eq!(format!("{}", ColorF::WHITE), "rgba(255, 255, 255, 1)");
        assert_eq!(format!("{}", ColorF::TRANSPARENT), "rgba(0, 0, 0, 0)");
        assert_eq!(format!("{}", ColorF::default()), format!("{}", ColorF::BLACK));

        let nan = ColorF { r: f32::NAN, g: f32::NAN, b: f32::NAN, a: f32::NAN };
        assert_eq!(format!("{nan}"), "rgba(NaN, NaN, NaN, NaN)");

        let inf = ColorF {
            r: f32::INFINITY,
            g: f32::NEG_INFINITY,
            b: f32::MAX,
            a: f32::INFINITY,
        };
        let s = format!("{inf}");
        assert!(s.starts_with("rgba(inf, -inf, ") && s.ends_with(", inf)"), "{s}");
    }

    // =====================================================================
    // round-trip: ColorU <-> ColorF, to_hash -> parse, Display -> parse
    // =====================================================================

    #[test]
    fn coloru_to_colorf_and_back_is_lossless_for_all_256_channel_values() {
        for i in 0..=255u16 {
            #[allow(clippy::cast_possible_truncation)]
            let c = ColorU::rgba(i as u8, (255 - i) as u8, i as u8, (255 - i) as u8);
            let f: ColorF = c.into();
            let back: ColorU = f.into();
            assert_eq!(back, c, "round-trip lost information at {i}");
        }
    }

    #[test]
    fn colorf_to_coloru_clamps_out_of_range_channels() {
        // > 1.0 is clamped by `.min(1.0)`.
        let over = ColorF { r: 2.0, g: 1e30, b: f32::INFINITY, a: 1.5 };
        assert_eq!(ColorU::from(over), ColorU::rgba(255, 255, 255, 255));
        // < 0.0 is NOT clamped by `.min`, but `as u8` saturates it to 0 anyway.
        let under = ColorF { r: -1.0, g: -1e30, b: f32::NEG_INFINITY, a: -0.5 };
        assert_eq!(ColorU::from(under), ColorU::rgba(0, 0, 0, 0));
    }

    #[test]
    fn colorf_to_coloru_maps_nan_channels_to_255() {
        // `f32::min` returns the NON-NaN operand, so `NaN.min(1.0) == 1.0`,
        // and a NaN channel comes out fully saturated rather than 0.
        let nan = ColorF { r: f32::NAN, g: 0.0, b: 0.0, a: f32::NAN };
        assert_eq!(ColorU::from(nan), ColorU::rgba(255, 0, 0, 255));
    }

    #[cfg(feature = "parser")]
    #[test]
    fn to_hash_round_trips_through_the_parser() {
        assert_eq!(parse_css_color(&ColorU::RED.to_hash()).unwrap(), ColorU::RED);
        for r in (0..=255u16).step_by(51) {
            for g in (0..=255u16).step_by(51) {
                for b in (0..=255u16).step_by(85) {
                    for a in (0..=255u16).step_by(85) {
                        #[allow(clippy::cast_possible_truncation)]
                        let c = ColorU::rgba(r as u8, g as u8, b as u8, a as u8);
                        let encoded = c.to_hash();
                        let decoded = parse_css_color(&encoded)
                            .unwrap_or_else(|e| panic!("{encoded} failed to parse: {e}"));
                        assert_eq!(decoded, c, "{encoded} decoded to the wrong color");
                    }
                }
            }
        }
    }

    #[cfg(feature = "parser")]
    #[test]
    fn coloru_display_round_trips_through_the_parser() {
        // Display emits `rgba(r, g, b, a/255)`, which parse_css_color accepts.
        for a in 0..=255u16 {
            #[allow(clippy::cast_possible_truncation)]
            let c = ColorU::rgba(13, 110, 253, a as u8);
            let encoded = format!("{c}");
            let decoded = parse_css_color(&encoded)
                .unwrap_or_else(|e| panic!("{encoded} failed to parse: {e}"));
            assert_eq!(decoded, c, "{encoded} decoded to the wrong color");
        }
        for c in SAMPLES {
            assert_eq!(parse_css_color(&format!("{c}")).unwrap(), c);
        }
    }

    #[cfg(feature = "parser")]
    #[test]
    fn system_color_ref_css_str_round_trips_for_every_variant() {
        let all = [
            SystemColorRef::Text,
            SystemColorRef::Background,
            SystemColorRef::Accent,
            SystemColorRef::AccentText,
            SystemColorRef::ButtonFace,
            SystemColorRef::ButtonText,
            SystemColorRef::WindowBackground,
            SystemColorRef::SelectionBackground,
            SystemColorRef::SelectionText,
        ];
        for variant in all {
            let encoded = variant.as_css_str();
            assert!(encoded.starts_with("system:"), "{encoded}");
            assert_eq!(
                parse_color_or_system(encoded).unwrap(),
                ColorOrSystem::System(variant),
                "{encoded} did not round-trip"
            );
        }
    }

    // =====================================================================
    // ColorOrSystem / SystemColorRef
    // =====================================================================

    #[test]
    fn color_or_system_constructors_and_fallbacks() {
        let c = ColorOrSystem::color(ColorU::RED);
        assert_eq!(c, ColorOrSystem::Color(ColorU::RED));
        assert_eq!(c.to_color_u_with_fallback(ColorU::BLUE), ColorU::RED);
        assert_eq!(c.to_color_u_default(), ColorU::RED);

        let s = ColorOrSystem::system(SystemColorRef::Accent);
        assert_eq!(s, ColorOrSystem::System(SystemColorRef::Accent));
        // A system ref has no concrete value, so the fallback wins.
        assert_eq!(s.to_color_u_with_fallback(ColorU::BLUE), ColorU::BLUE);
        assert_eq!(s.to_color_u_default(), ColorU::rgba(128, 128, 128, 255));

        // Default is opaque black, and From<ColorU> agrees with ::color().
        assert_eq!(ColorOrSystem::default(), ColorOrSystem::Color(ColorU::BLACK));
        assert_eq!(ColorOrSystem::from(ColorU::RED), ColorOrSystem::color(ColorU::RED));
    }

    #[test]
    fn system_color_ref_resolve_falls_back_when_unset() {
        use crate::system::SystemColors;

        let empty = SystemColors::default();
        let all = [
            SystemColorRef::Text,
            SystemColorRef::Background,
            SystemColorRef::Accent,
            SystemColorRef::AccentText,
            SystemColorRef::ButtonFace,
            SystemColorRef::ButtonText,
            SystemColorRef::WindowBackground,
            SystemColorRef::SelectionBackground,
            SystemColorRef::SelectionText,
        ];
        for variant in all {
            assert_eq!(variant.resolve(&empty, ColorU::RED), ColorU::RED, "{variant:?}");
            assert_eq!(
                ColorOrSystem::System(variant).resolve(&empty, ColorU::RED),
                ColorU::RED
            );
        }
        // A concrete color ignores both the SystemColors and the fallback.
        assert_eq!(
            ColorOrSystem::Color(ColorU::BLUE).resolve(&empty, ColorU::RED),
            ColorU::BLUE
        );
    }

    // =====================================================================
    // palettes: shade is a `usize`, so every value must land somewhere
    // =====================================================================

    #[test]
    fn palette_shades_are_total_over_usize_and_always_opaque() {
        type Palette = fn(usize) -> ColorU;
        const PALETTES: [Palette; 12] = [
            ColorU::strawberry,
            ColorU::palette_orange,
            ColorU::banana,
            ColorU::palette_lime,
            ColorU::mint,
            ColorU::blueberry,
            ColorU::grape,
            ColorU::bubblegum,
            ColorU::cocoa,
            ColorU::palette_silver,
            ColorU::slate,
            ColorU::dark,
        ];
        for p in PALETTES {
            for shade in [0, 1, 100, 200, 201, 300, 400, 401, 500, 600, 601, 700, 800, 801, 900, 1000, usize::MAX] {
                assert_eq!(p(shade).a, 255, "shade {shade} was not opaque");
            }
            // Every out-of-band shade collapses into the 900 bucket.
            assert_eq!(p(usize::MAX), p(900));
            assert_eq!(p(801), p(900));
            // The documented buckets are distinct at their boundaries.
            assert_eq!(p(0), p(200));
            assert_ne!(p(200), p(201));
            assert_ne!(p(400), p(401));
            assert_ne!(p(600), p(601));
            assert_ne!(p(800), p(801));
        }
    }

    #[test]
    fn palette_known_values() {
        assert_eq!(ColorU::strawberry(100), ColorU::rgb(0xff, 0x8c, 0x82));
        assert_eq!(ColorU::strawberry(900), ColorU::rgb(0x7a, 0x00, 0x00));
        assert_eq!(ColorU::dark(900), ColorU::BLACK);
        assert_eq!(ColorU::dark(usize::MAX), ColorU::BLACK);
    }

    // =====================================================================
    // named / themed constructors: every one must be a valid opaque color
    // =====================================================================

    #[test]
    fn named_constructors_match_their_constants() {
        assert_eq!(ColorU::red(), ColorU::RED);
        assert_eq!(ColorU::green(), ColorU::GREEN);
        assert_eq!(ColorU::blue(), ColorU::BLUE);
        assert_eq!(ColorU::white(), ColorU::WHITE);
        assert_eq!(ColorU::black(), ColorU::BLACK);
        assert_eq!(ColorU::transparent(), ColorU::TRANSPARENT);
        assert_eq!(ColorU::yellow(), ColorU::YELLOW);
        assert_eq!(ColorU::cyan(), ColorU::CYAN);
        assert_eq!(ColorU::magenta(), ColorU::MAGENTA);
        assert_eq!(ColorU::orange(), ColorU::ORANGE);
        assert_eq!(ColorU::pink(), ColorU::PINK);
        assert_eq!(ColorU::purple(), ColorU::PURPLE);
        assert_eq!(ColorU::brown(), ColorU::BROWN);
        assert_eq!(ColorU::gray(), ColorU::GRAY);
        assert_eq!(ColorU::light_gray(), ColorU::LIGHT_GRAY);
        assert_eq!(ColorU::dark_gray(), ColorU::DARK_GRAY);
        assert_eq!(ColorU::navy(), ColorU::NAVY);
        assert_eq!(ColorU::teal(), ColorU::TEAL);
        assert_eq!(ColorU::olive(), ColorU::OLIVE);
        assert_eq!(ColorU::maroon(), ColorU::MAROON);
        assert_eq!(ColorU::lime(), ColorU::LIME);
        assert_eq!(ColorU::aqua(), ColorU::AQUA);
        assert_eq!(ColorU::silver(), ColorU::SILVER);
        assert_eq!(ColorU::fuchsia(), ColorU::FUCHSIA);
        assert_eq!(ColorU::indigo(), ColorU::INDIGO);
        assert_eq!(ColorU::gold(), ColorU::GOLD);
        assert_eq!(ColorU::coral(), ColorU::CORAL);
        assert_eq!(ColorU::salmon(), ColorU::SALMON);
        assert_eq!(ColorU::turquoise(), ColorU::TURQUOISE);
        assert_eq!(ColorU::violet(), ColorU::VIOLET);
        assert_eq!(ColorU::crimson(), ColorU::CRIMSON);
        assert_eq!(ColorU::chocolate(), ColorU::CHOCOLATE);
        assert_eq!(ColorU::sky_blue(), ColorU::SKY_BLUE);
        assert_eq!(ColorU::forest_green(), ColorU::FOREST_GREEN);
        assert_eq!(ColorU::sea_green(), ColorU::SEA_GREEN);
        assert_eq!(ColorU::slate_gray(), ColorU::SLATE_GRAY);
        assert_eq!(ColorU::midnight_blue(), ColorU::MIDNIGHT_BLUE);
        assert_eq!(ColorU::dark_red(), ColorU::DARK_RED);
        assert_eq!(ColorU::dark_green(), ColorU::DARK_GREEN);
        assert_eq!(ColorU::dark_blue(), ColorU::DARK_BLUE);
        assert_eq!(ColorU::light_blue(), ColorU::LIGHT_BLUE);
        assert_eq!(ColorU::light_green(), ColorU::LIGHT_GREEN);
        assert_eq!(ColorU::light_yellow(), ColorU::LIGHT_YELLOW);
        assert_eq!(ColorU::light_pink(), ColorU::LIGHT_PINK);
    }

    #[test]
    fn every_named_constructor_except_transparent_is_opaque() {
        type Ctor = fn() -> ColorU;
        const CTORS: [Ctor; 43] = [
            ColorU::red, ColorU::green, ColorU::blue, ColorU::white, ColorU::black,
            ColorU::yellow, ColorU::cyan, ColorU::magenta, ColorU::orange, ColorU::pink,
            ColorU::purple, ColorU::brown, ColorU::gray, ColorU::light_gray, ColorU::dark_gray,
            ColorU::navy, ColorU::teal, ColorU::olive, ColorU::maroon, ColorU::lime,
            ColorU::aqua, ColorU::silver, ColorU::fuchsia, ColorU::indigo, ColorU::gold,
            ColorU::coral, ColorU::salmon, ColorU::turquoise, ColorU::violet, ColorU::crimson,
            ColorU::chocolate, ColorU::sky_blue, ColorU::forest_green, ColorU::sea_green,
            ColorU::slate_gray, ColorU::midnight_blue, ColorU::dark_red, ColorU::dark_green,
            ColorU::dark_blue, ColorU::light_blue, ColorU::light_green, ColorU::light_yellow,
            ColorU::light_pink,
        ];
        for ctor in CTORS {
            let c = ctor();
            assert_eq!(c.a, ColorU::ALPHA_OPAQUE);
            assert!(!c.has_alpha());
        }
        // The one exception.
        assert_eq!(ColorU::transparent().a, ColorU::ALPHA_TRANSPARENT);
        assert!(ColorU::transparent().has_alpha());
    }

    #[test]
    fn apple_and_bootstrap_palettes_are_opaque_and_distinct() {
        type Ctor = fn() -> ColorU;
        const APPLE: [Ctor; 26] = [
            ColorU::apple_red, ColorU::apple_red_dark,
            ColorU::apple_orange, ColorU::apple_orange_dark,
            ColorU::apple_yellow, ColorU::apple_yellow_dark,
            ColorU::apple_green, ColorU::apple_green_dark,
            ColorU::apple_mint, ColorU::apple_mint_dark,
            ColorU::apple_teal, ColorU::apple_teal_dark,
            ColorU::apple_cyan, ColorU::apple_cyan_dark,
            ColorU::apple_blue, ColorU::apple_blue_dark,
            ColorU::apple_indigo, ColorU::apple_indigo_dark,
            ColorU::apple_purple, ColorU::apple_purple_dark,
            ColorU::apple_pink, ColorU::apple_pink_dark,
            ColorU::apple_brown, ColorU::apple_brown_dark,
            ColorU::apple_gray, ColorU::apple_gray_dark,
        ];
        const BOOTSTRAP: [Ctor; 23] = [
            ColorU::bootstrap_primary, ColorU::bootstrap_primary_hover, ColorU::bootstrap_primary_active,
            ColorU::bootstrap_secondary, ColorU::bootstrap_secondary_hover, ColorU::bootstrap_secondary_active,
            ColorU::bootstrap_success, ColorU::bootstrap_success_hover, ColorU::bootstrap_success_active,
            ColorU::bootstrap_danger, ColorU::bootstrap_danger_hover, ColorU::bootstrap_danger_active,
            ColorU::bootstrap_warning, ColorU::bootstrap_warning_hover, ColorU::bootstrap_warning_active,
            ColorU::bootstrap_info, ColorU::bootstrap_info_hover, ColorU::bootstrap_info_active,
            ColorU::bootstrap_light, ColorU::bootstrap_light_hover, ColorU::bootstrap_light_active,
            ColorU::bootstrap_dark, ColorU::bootstrap_dark_hover,
        ];
        for ctor in APPLE.iter().chain(BOOTSTRAP.iter()) {
            assert_eq!(ctor().a, 255);
        }
        // Each light/dark pair must actually differ.
        for pair in APPLE.chunks_exact(2) {
            assert_ne!(pair[0](), pair[1](), "an apple light/dark pair is identical");
        }
        // bootstrap_link duplicates bootstrap_primary by design; check the hover shifts.
        assert_eq!(ColorU::bootstrap_link(), ColorU::bootstrap_primary());
        assert_ne!(ColorU::bootstrap_link_hover(), ColorU::bootstrap_link());
        assert_ne!(ColorU::bootstrap_dark_active(), ColorU::bootstrap_dark());
    }

    // =====================================================================
    // parser: parse_css_color — malformed / huge / boundary / unicode
    // =====================================================================

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_valid_minimal_positive_controls() {
        assert_eq!(parse_css_color("red").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("#f00").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("#ff0000").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("rgb(255,0,0)").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("hsl(0,100%,50%)").unwrap(), ColorU::RED);
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_empty_and_whitespace_only_are_errors() {
        assert!(parse_css_color("").is_err());
        assert!(parse_css_color("   ").is_err());
        assert!(parse_css_color("\t\n\r ").is_err());
        assert!(parse_css_color("#").is_err());
        assert_eq!(parse_css_color(""), Err(CssColorParseError::EmptyInput));
        assert_eq!(parse_css_color("  \t "), Err(CssColorParseError::EmptyInput));
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_garbage_is_rejected_without_panicking() {
        for garbage in [
            "!@#$%^&*()", "\0\0\0", "rgb", "rgb(", "rgb)", ")(", "()", "#-1", "#+1",
            "notacolor", "0", "-0", "1e10", "NaN", "inf", "-inf", ";", ",,,", "\\",
            "rgb(,,)", "hsl(,,)", "rgba(,,,)", "#\u{0}\u{0}\u{0}",
        ] {
            assert!(
                parse_css_color(garbage).is_err(),
                "{garbage:?} was unexpectedly accepted"
            );
        }
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_extremely_long_input_does_not_hang_or_panic() {
        // Hex path: rejected on the length check alone.
        let long_hex = format!("#{}", "f".repeat(1_000_000));
        assert!(parse_css_color(&long_hex).is_err());
        // Named-color path: lowercases 100k bytes, then fails the match.
        let long_name = "a".repeat(100_000);
        assert!(parse_css_color(&long_name).is_err());
        // Function path with a huge component list.
        let long_rgb = format!("rgb({})", "1,".repeat(50_000));
        assert!(parse_css_color(&long_rgb).is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_deeply_nested_input_does_not_stack_overflow() {
        // The parser is iterative, not recursive — these must simply be errors.
        let nested_parens = "(".repeat(10_000);
        assert!(parse_css_color(&nested_parens).is_err());
        let unclosed = "rgb(".repeat(10_000);
        assert!(parse_css_color(&unclosed).is_err());
        let balanced = format!("{}{}", "rgb(".repeat(5_000), ")".repeat(5_000));
        assert!(parse_css_color(&balanced).is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_unicode_input_does_not_panic() {
        // The 3/4-byte hex branches read raw bytes, so a multi-byte char must
        // fail cleanly rather than slice through a char boundary.
        for input in [
            "\u{1F600}",        // emoji
            "#\u{1F600}",       // 4 bytes after '#' -> hits the len==4 branch
            "#\u{e9}1",         // 3 bytes after '#' -> hits the len==3 branch
            "#\u{e9}\u{e9}\u{e9}", // 6 bytes -> hits the from_str_radix branch
            "r\u{e9}d",
            "\u{0301}\u{0301}",  // bare combining marks
            "\u{4e2d}\u{6587}",  // CJK
            "rgb(\u{1F600},0,0)",
            "rgba(0,0,0,\u{1F600})",
            "hsl(\u{1F600},100%,50%)",
        ] {
            assert!(
                parse_css_color(input).is_err(),
                "{input:?} was unexpectedly accepted"
            );
        }
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_boundary_numbers() {
        // rgb components are u8: 0 and 255 in, 256 and -1 out.
        assert_eq!(parse_css_color("rgb(0,0,0)").unwrap(), ColorU::BLACK);
        assert_eq!(parse_css_color("rgb(255,255,255)").unwrap(), ColorU::WHITE);
        assert!(parse_css_color("rgb(256,0,0)").is_err());
        assert!(parse_css_color("rgb(-1,0,0)").is_err());
        assert!(parse_css_color("rgb(9223372036854775807,0,0)").is_err());
        assert!(parse_css_color("rgb(340282350000000000000000000000000000000,0,0)").is_err());
        // Alpha is a float clamped to 0.0..=1.0 (inclusive), out-of-range rejected.
        assert_eq!(parse_css_color("rgba(0,0,0,0)").unwrap().a, 0);
        assert_eq!(parse_css_color("rgba(0,0,0,1)").unwrap().a, 255);
        assert_eq!(parse_css_color("rgba(0,0,0,1.0)").unwrap().a, 255);
        assert_eq!(parse_css_color("rgba(0,0,0,-0)").unwrap().a, 0);
        assert!(parse_css_color("rgba(0,0,0,1.0001)").is_err());
        assert!(parse_css_color("rgba(0,0,0,-0.0001)").is_err());
        assert!(parse_css_color("rgba(0,0,0,2)").is_err());
        // NaN / inf are valid f32 literals to FromStr, but must fail the range check.
        assert!(parse_css_color("rgba(0,0,0,NaN)").is_err());
        assert!(parse_css_color("rgba(0,0,0,nan)").is_err());
        assert!(parse_css_color("rgba(0,0,0,inf)").is_err());
        assert!(parse_css_color("rgba(0,0,0,-inf)").is_err());
        assert!(parse_css_color("rgba(0,0,0,infinity)").is_err());
        // Subnormals round down to a fully transparent alpha rather than panicking.
        assert_eq!(parse_css_color("rgba(0,0,0,1e-45)").unwrap().a, 0);
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_alpha_rounds_to_nearest() {
        // `(a * 255.0).round()` — half rounds away from zero.
        assert_eq!(parse_css_color("rgba(0,0,0,0.5)").unwrap().a, 128);
        assert_eq!(parse_css_color("rgba(0,0,0,0.0)").unwrap().a, 0);
        assert_eq!(parse_css_color("rgba(0,0,0,0.999)").unwrap().a, 255);
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_arity_errors() {
        assert!(parse_css_color("rgb(255,0)").is_err());       // missing blue
        assert!(parse_css_color("rgb(255)").is_err());         // missing green
        assert!(parse_css_color("rgb()").is_err());            // missing everything
        assert!(parse_css_color("rgb(0,0,0,0)").is_err());     // extra arg to rgb()
        assert!(parse_css_color("rgba(0,0,0)").is_err());      // missing alpha
        assert!(parse_css_color("rgba(0,0,0,1,1)").is_err());  // extra arg to rgba()
        assert!(parse_css_color("hsl(0,100%)").is_err());      // missing lightness
        assert!(parse_css_color("hsla(0,100%,50%)").is_err()); // missing alpha
        // This implementation requires commas; space-separated CSS4 syntax is not supported.
        assert!(parse_css_color("rgb(255 0 0)").is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_leading_and_trailing_whitespace_is_trimmed() {
        assert_eq!(parse_css_color("  red  ").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("\t#f00\n").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("  rgb( 255 , 0 , 0 )  ").unwrap(), ColorU::RED);
        // Trailing junk after a bare keyword IS rejected.
        assert!(parse_css_color("red;garbage").is_err());
        assert!(parse_css_color("red red").is_err());
        assert!(parse_css_color("#f00;").is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_accepts_trailing_junk_after_a_function_call() {
        // KNOWN DEVIATION (pinned, not endorsed): parse_parentheses slices between
        // the FIRST '(' and the LAST ')', so anything after the closing paren is
        // silently dropped instead of being rejected as an error. See report.
        assert_eq!(parse_css_color("rgb(1,2,3)garbage").unwrap(), ColorU::rgb(1, 2, 3));
        assert_eq!(parse_css_color("rgb(1,2,3);").unwrap(), ColorU::rgb(1, 2, 3));
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_hex_is_case_insensitive_and_length_checked() {
        assert_eq!(parse_css_color("#ABCDEF").unwrap(), parse_css_color("#abcdef").unwrap());
        assert_eq!(parse_css_color("#FFF").unwrap(), ColorU::WHITE);
        // 3/4-digit shorthand expands by *17 (f -> 0xff).
        assert_eq!(parse_css_color("#f00f").unwrap(), ColorU::rgba(255, 0, 0, 255));
        assert_eq!(parse_css_color("#0008").unwrap(), ColorU::rgba(0, 0, 0, 136));
        // Only lengths 3, 4, 6 and 8 are legal.
        for bad_len in ["#", "#f", "#ff", "#fffff", "#fffffff", "#fffffffff"] {
            assert!(parse_css_color(bad_len).is_err(), "{bad_len} accepted");
        }
        // Non-hex digits.
        assert!(parse_css_color("#ggg").is_err());
        assert!(parse_css_color("#gggggg").is_err());
        assert!(parse_css_color("#-12345").is_err());
        assert!(parse_css_color("#+f0000").is_err());
        assert!(parse_css_color("#ff ff").is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_builtin_names_are_case_insensitive() {
        assert_eq!(parse_css_color("RED").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("ReD").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("TRANSPARENT").unwrap(), ColorU::TRANSPARENT);
        assert_eq!(parse_css_color("transparent").unwrap().a, 0);
        // Near-miss names are rejected, not fuzzy-matched.
        for near_miss in ["redd", "re", "r ed", "red1", "gray2", "greyish", "blackk"] {
            assert!(parse_css_color(near_miss).is_err(), "{near_miss} accepted");
        }
        // ...but surrounding whitespace really is just trimmed.
        assert_eq!(parse_css_color(" grey ").unwrap(), ColorU::GRAY);
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_hsl_boundaries_and_hue_wraparound() {
        assert_eq!(parse_css_color("hsl(0,100%,50%)").unwrap(), ColorU::RED);
        assert_eq!(parse_css_color("hsl(120,100%,50%)").unwrap(), ColorU::GREEN);
        assert_eq!(parse_css_color("hsl(240,100%,50%)").unwrap(), ColorU::BLUE);
        // A full extra turn lands back on red.
        assert_eq!(parse_css_color("hsl(720,100%,50%)").unwrap(), ColorU::RED);
        // Achromatic ends.
        assert_eq!(parse_css_color("hsl(0,0%,0%)").unwrap(), ColorU::BLACK);
        assert_eq!(parse_css_color("hsl(0,0%,100%)").unwrap(), ColorU::WHITE);
        // Huge but finite hues must not panic.
        for hue in ["1000000", "99999999", "-360"] {
            let s = format!("hsl({hue},100%,50%)");
            let _ = parse_css_color(&s).map(|c| assert_eq!(c.a, 255));
        }
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_css_color_unitless_hsl_components_are_scaled_wrong() {
        // KNOWN DEVIATION (pinned, not endorsed): `parse_percentage_value` turns a
        // unitless value into `value * 100` percent, and `percent_from_str` then
        // re-normalizes it, so a unitless component is a 0..1 FRACTION rather than
        // the CSS Color 4 "number of percent". `hsl(0 100 50)` — plain red in every
        // browser — comes out CYAN here. Pinned so a fix shows up as a diff.
        assert_eq!(parse_css_color("hsl(0,100,50)").unwrap(), ColorU::rgb(0, 255, 255));
        // The fraction spelling is what currently means "100% / 50%".
        assert_eq!(parse_css_color("hsl(0,1,0.5)").unwrap(), ColorU::RED);
        // The mixed spelling that the existing suite smoke-tests happens to land on
        // red by coincidence (the out-of-range saturation clips back into gamut).
        assert_eq!(parse_css_color("hsl(0,100,50%)").unwrap(), ColorU::RED);
    }

    // =====================================================================
    // parser: private helpers
    // =====================================================================

    #[cfg(feature = "parser")]
    #[test]
    fn parse_color_no_hash_only_accepts_3_4_6_and_8_bytes() {
        assert_eq!(parse_color_no_hash("fff").unwrap(), ColorU::WHITE);
        assert_eq!(parse_color_no_hash("000f").unwrap(), ColorU::BLACK);
        assert_eq!(parse_color_no_hash("ff0000").unwrap(), ColorU::RED);
        assert_eq!(parse_color_no_hash("ff000080").unwrap(), ColorU::rgba(255, 0, 0, 128));
        for bad in ["", "f", "ff", "fffff", "fffffff", "fffffffff", "   ", "zzz"] {
            assert!(parse_color_no_hash(bad).is_err(), "{bad:?} accepted");
        }
        // `input.len()` is a BYTE length, so a 3-byte multi-byte string reaches
        // the byte-reading branch and must error, not slice through a char.
        assert!(parse_color_no_hash("\u{e9}1").is_err());
        assert!(parse_color_no_hash("\u{1F600}").is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_color_rgb_alpha_flag_controls_arity() {
        assert_eq!(parse_color_rgb("1,2,3", false).unwrap(), ColorU::rgb(1, 2, 3));
        assert_eq!(parse_color_rgb("1,2,3,1", true).unwrap(), ColorU::rgba(1, 2, 3, 255));
        // parse_alpha=true but no alpha given.
        assert!(parse_color_rgb("1,2,3", true).is_err());
        // parse_alpha=false but an alpha given.
        assert!(parse_color_rgb("1,2,3,1", false).is_err());
        // Empty / whitespace components.
        assert!(parse_color_rgb("", false).is_err());
        assert!(parse_color_rgb("   ", false).is_err());
        assert!(parse_color_rgb(",,", false).is_err());
        assert!(parse_color_rgb("1,,3", false).is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_color_rgb_components_boundaries() {
        let mut ok = ["0", "128", "255"].into_iter();
        assert_eq!(
            parse_color_rgb_components(&mut ok).unwrap(),
            ColorU::rgb(0, 128, 255)
        );
        // An empty iterator is a missing-component error, not a panic.
        let mut empty = core::iter::empty::<&str>();
        assert!(parse_color_rgb_components(&mut empty).is_err());
        // Too few components.
        let mut short = ["1", "2"].into_iter();
        assert!(parse_color_rgb_components(&mut short).is_err());
        // Overflow / underflow / garbage.
        for bad in [
            ["256", "0", "0"],
            ["-1", "0", "0"],
            ["0", "0", "1e3"],
            ["0.5", "0", "0"],
            ["abc", "0", "0"],
            ["", "0", "0"],
            ["+0", "0", "999999999999999999999"],
        ] {
            let mut it = bad.into_iter();
            assert!(parse_color_rgb_components(&mut it).is_err(), "{bad:?} accepted");
        }
        // Extra components past the third are simply not consumed here.
        let mut extra = ["1", "2", "3", "4", "5"].into_iter();
        assert_eq!(
            parse_color_rgb_components(&mut extra).unwrap(),
            ColorU::rgb(1, 2, 3)
        );
        assert_eq!(extra.next(), Some("4"));
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_color_hsl_components_boundaries() {
        let mut red = ["0", "100%", "50%"].into_iter();
        assert_eq!(parse_color_hsl_components(&mut red).unwrap(), ColorU::RED);
        // KNOWN DEVIATION (pinned, not endorsed): a unitless component is read as
        // a 0.0..=1.0 FRACTION, not as a number of percent, so CSS Color 4's
        // `hsl(0 100 50)` saturation/lightness are scaled 100x too far. Only the
        // fraction spelling currently round-trips to red. See report.
        let mut fractions = ["0", "1", "0.5"].into_iter();
        assert_eq!(parse_color_hsl_components(&mut fractions).unwrap(), ColorU::RED);
        let mut unitless = ["0", "100", "50"].into_iter();
        assert_eq!(
            parse_color_hsl_components(&mut unitless).unwrap(),
            ColorU::rgb(0, 255, 255),
            "unitless hsl(0 100 50) should be red, not cyan"
        );
        // Missing components error rather than panic.
        let mut empty = core::iter::empty::<&str>();
        assert!(parse_color_hsl_components(&mut empty).is_err());
        let mut short = ["0", "100%"].into_iter();
        assert!(parse_color_hsl_components(&mut short).is_err());
        for bad in [
            ["", "100%", "50%"],
            ["notanangle", "100%", "50%"],
            ["to left", "100%", "50%"], // Direction::FromTo is unsupported for hue
            ["0", "", "50%"],
            ["0", "100%", ""],
        ] {
            let mut it = bad.into_iter();
            assert!(parse_color_hsl_components(&mut it).is_err(), "{bad:?} accepted");
        }
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_alpha_component_range_and_rounding() {
        let cases: [(&str, u8); 5] = [("0", 0), ("0.0", 0), ("0.5", 128), ("1", 255), ("1.0", 255)];
        for (input, expected) in cases {
            let mut it = [input].into_iter();
            assert_eq!(
                parse_alpha_component(&mut it).unwrap(),
                expected,
                "alpha {input}"
            );
        }
        // Out of range / unparseable / NaN / inf all produce Err, never a panic.
        for bad in ["", " ", "-0.0001", "1.0001", "2", "-1", "NaN", "inf", "-inf", "abc", "0,5", "50%"] {
            let mut it = [bad].into_iter();
            assert!(parse_alpha_component(&mut it).is_err(), "{bad:?} accepted");
        }
        // Missing entirely.
        let mut empty = core::iter::empty::<&str>();
        assert!(parse_alpha_component(&mut empty).is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_color_builtin_rejects_junk_without_panicking() {
        assert_eq!(parse_color_builtin("red").unwrap(), ColorU::RED);
        assert_eq!(parse_color_builtin("REBECCAPURPLE").unwrap(), ColorU::rgb(102, 51, 153));
        assert_eq!(parse_color_builtin("transparent").unwrap(), ColorU::TRANSPARENT);
        // Not trimmed at this level — the caller is responsible for that.
        assert!(parse_color_builtin(" red").is_err());
        assert!(parse_color_builtin("").is_err());
        // to_lowercase() on exotic input must not panic (dotted capital I expands).
        assert!(parse_color_builtin("\u{130}").is_err());
        assert!(parse_color_builtin("\u{1F600}").is_err());
        assert!(parse_color_builtin(&"z".repeat(100_000)).is_err());
    }

    // =====================================================================
    // parser: parse_color_or_system
    // =====================================================================

    #[cfg(feature = "parser")]
    #[test]
    fn parse_color_or_system_rejects_bad_system_names() {
        for bad in [
            "system:",
            "system:invalid",
            "system: ",
            "system::text",
            "system:text-",
            "system:TEXT",      // the variant table is case-SENSITIVE
            "SYSTEM:text",      // the prefix is case-SENSITIVE
            "system:text;junk",
            "system:\u{1F600}",
        ] {
            assert!(parse_color_or_system(bad).is_err(), "{bad:?} accepted");
        }
        // Empty / whitespace.
        assert!(parse_color_or_system("").is_err());
        assert!(parse_color_or_system("   ").is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_color_or_system_trims_and_falls_through_to_colors() {
        assert_eq!(
            parse_color_or_system("  system:accent  ").unwrap(),
            ColorOrSystem::System(SystemColorRef::Accent)
        );
        // The name after the prefix is trimmed too.
        assert_eq!(
            parse_color_or_system("system: accent ").unwrap(),
            ColorOrSystem::System(SystemColorRef::Accent)
        );
        // Non-system input is delegated to parse_css_color.
        assert_eq!(
            parse_color_or_system("  #f00 ").unwrap(),
            ColorOrSystem::Color(ColorU::RED)
        );
        assert_eq!(
            parse_color_or_system("rgba(0,0,0,0)").unwrap(),
            ColorOrSystem::Color(ColorU::TRANSPARENT)
        );
        assert!(parse_color_or_system("definitely-not-a-color").is_err());
    }

    #[cfg(feature = "parser")]
    #[test]
    fn parse_color_or_system_long_and_nested_input_does_not_hang() {
        let long = format!("system:{}", "a".repeat(100_000));
        assert!(parse_color_or_system(&long).is_err());
        let nested = "rgb(".repeat(10_000);
        assert!(parse_color_or_system(&nested).is_err());
    }

    // =====================================================================
    // error types: to_contained / to_shared round-trip
    // =====================================================================

    #[cfg(feature = "parser")]
    #[test]
    fn css_color_parse_error_round_trips_through_owned() {
        // One representative input per reachable error variant.
        let errors = [
            parse_css_color("notacolor").unwrap_err(),        // InvalidColor
            parse_css_color("foo(1,2)").unwrap_err(),          // InvalidFunctionName
            parse_css_color("#zzz").unwrap_err(),              // InvalidColorComponent
            parse_css_color("rgb(300,0,0)").unwrap_err(),      // IntValueParseErr
            parse_css_color("rgba(0,0,0,x)").unwrap_err(),     // FloatValueParseErr
            parse_css_color("rgba(0,0,0,2)").unwrap_err(),     // FloatValueOutOfRange
            parse_css_color("rgb(1,2)").unwrap_err(),          // MissingColorComponent
            parse_css_color("rgb(1,2,3,4)").unwrap_err(),      // ExtraArguments
            parse_css_color("rgb(1,2,3").unwrap_err(),         // UnclosedColor
            parse_css_color("").unwrap_err(),                  // EmptyInput
            parse_css_color("hsl(x,1%,1%)").unwrap_err(),      // DirectionParseError
            parse_css_color("hsl(0,x%,1%)").unwrap_err(),      // InvalidPercentage
        ];
        for e in &errors {
            let owned = e.to_contained();
            let shared = owned.to_shared();
            // Borrowed -> owned -> borrowed -> owned must be a fixed point.
            assert_eq!(shared.to_contained(), owned, "error did not round-trip: {e}");
            // Debug/Display must both produce something non-empty.
            assert!(!format!("{e}").is_empty());
            assert!(!format!("{e:?}").is_empty());
            assert!(!format!("{owned:?}").is_empty());
        }
    }

    #[cfg(feature = "parser")]
    #[test]
    fn css_color_parse_error_carries_the_offending_input() {
        assert_eq!(
            parse_css_color("notacolor"),
            Err(CssColorParseError::InvalidColor("notacolor"))
        );
        assert_eq!(
            parse_css_color("rgb(1,2,3,4)"),
            Err(CssColorParseError::ExtraArguments("4"))
        );
        assert_eq!(
            parse_css_color("rgb(1,2)"),
            Err(CssColorParseError::MissingColorComponent(
                CssColorComponent::Blue
            ))
        );
        assert_eq!(
            parse_css_color("rgba(1,2,3)"),
            Err(CssColorParseError::MissingColorComponent(
                CssColorComponent::Alpha
            ))
        );
        assert_eq!(
            parse_css_color("rgba(0,0,0,2)"),
            Err(CssColorParseError::FloatValueOutOfRange(2.0))
        );
        // The byte, not the char, is reported for a bad hex digit.
        assert_eq!(
            parse_css_color("#zzz"),
            Err(CssColorParseError::InvalidColorComponent(b'z'))
        );
    }
}