native-theme 0.7.1

Cross-platform native theme detection and loading for Rust GUI applications
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
// KDE color group parsing -> populate per-widget fields on ThemeMode
// Maps semantic color roles from KDE INI color groups directly to ThemeMode.

use super::shade::{KdeShadeRole, kde_shade};
use crate::Rgba;

/// Look up a color key from a KDE INI section and parse it as RGB.
fn get_color(ini: &configparser::ini::Ini, section: &str, key: &str) -> Option<Rgba> {
    let value = ini.get(section, key)?;
    super::parse_rgb(&value)
}

/// Breeze's `Metrics::Blend_Value` (breezemetrics.h:176 at breeze v6.7.5), the
/// alpha of the selection colour over a checked indicator's fill
/// (breezehelper.cpp:40), of the focus colour over a hovered menu item
/// (breezestyle.cpp:6021-6029 at breeze f0b1d75) and of the selection colour over
/// a hovered item-view row (:4871-4875); docs/platform-facts.md §2.5, §2.6,
/// §2.15.
const BLEND_VALUE: f32 = 0.3;

/// `kdeglobals` `[KDE] frameContrast` where it is unset, KColorScheme's own
/// default (kcolorscheme.cpp:529-538 at 27066d47; docs/platform-facts.md §2.11).
const FRAME_CONTRAST_DEFAULT: f64 = 0.2;

/// `kdeglobals` `[KDE] contrast` where it is unset or not an integer,
/// KColorScheme's own default: `contrastF` is `0.1 * readEntry("contrast", 7)`
/// (kcolorscheme.cpp:519-527 at kcolorscheme v6.31.0; docs/platform-facts.md
/// §2.15).
const CONTRAST_DEFAULT: i32 = 7;

/// `color` at `alpha`, held in 8 bits as `Rgba` holds every alpha.
fn translucent(color: Rgba, alpha: f32) -> Rgba {
    let [r, g, b, _] = color.to_f32_array();
    Rgba::from_f32(r, g, b, alpha)
}

/// `top` painted over the opaque `bottom` (source over).
fn over(top: Rgba, bottom: Rgba) -> Rgba {
    let [tr, tg, tb, alpha] = top.to_f32_array();
    let [br, bg, bb, _] = bottom.to_f32_array();
    let channel = |t: f32, b: f32| t * alpha + b * (1.0 - alpha);
    Rgba::from_f32(channel(tr, br), channel(tg, bg), channel(tb, bb), 1.0)
}

/// `qRound(x)` for a non-negative `x` as Qt 6.11.2 computes it on x86-64,
/// `int(x + 0.5f)` (qtbase `src/corelib/global/qnumeric.h:539-544`), held
/// in the 16 bits `QColor` stores a component in. Every caller passes a
/// value in 0.0..=65535.0; the clamp keeps any other in the 16 bits.
fn q_round(x: f32) -> u16 {
    (x.clamp(0.0, 65535.0) + 0.5) as u16
}

/// An RGB colour as Qt 6.11.2's `QColor` stores one: four 16-bit channels.
/// Qt's colour arithmetic reads and rounds them, not the 8-bit values, so
/// a port that rounds to 8 bits between steps gives other colours
/// (qtbase `src/gui/painting/qcolor.cpp` at v6.11.2).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct QtColor {
    r: u16,
    g: u16,
    b: u16,
    a: u16,
}

/// An HSV colour as `QColor` stores one: the hue in hundredths of a degree,
/// `u16::MAX` where it is undefined (a grey), the rest in 16 bits.
#[derive(Clone, Copy, Debug)]
struct QtHsv {
    hue: u16,
    saturation: u16,
    value: u16,
    alpha: u16,
}

impl QtColor {
    /// `QColor(r, g, b, a)`: each 8-bit channel times 0x101 (`setRgb`,
    /// qcolor.cpp:1518-1531).
    fn from_rgba(color: Rgba) -> Self {
        let wide = |c: u8| u16::from_be_bytes([c, c]);
        Self {
            r: wide(color.r),
            g: wide(color.g),
            b: wide(color.b),
            a: wide(color.a),
        }
    }

    /// `QColor::fromRgbF` (qcolor.cpp:2607-2633) for components in
    /// 0.0..=1.0, each `qRound(x * 65535)`. Qt keeps a component outside that
    /// range in half floats instead; the callers here cannot make one, and
    /// the clamp keeps it so.
    fn from_rgb_f(r: f32, g: f32, b: f32, a: f32) -> Self {
        let channel = |x: f32| q_round(x.clamp(0.0, 1.0) * 65535.0);
        Self {
            r: channel(r),
            g: channel(g),
            b: channel(b),
            a: channel(a),
        }
    }

    /// `redF()` and the others: the channel over 65535 as a float
    /// (qcolor.cpp:1665-1670, :1783-1791).
    fn f(channel: u16) -> f32 {
        f32::from(channel) / 65535.0
    }

    /// The colour at `alpha`, `QColor::setAlphaF` (qcolor.cpp:1679-1689).
    fn with_alpha_f(self, alpha: f32) -> Self {
        Self {
            a: q_round(alpha.clamp(0.0, 1.0) * 65535.0),
            ..self
        }
    }

    /// The 8-bit colour, `red()` and the others: `qt_div_257` of each
    /// channel (qcolor.cpp:1697-1702; `qdrawhelper_p.h:886-887`).
    fn to_rgba(self) -> Rgba {
        let narrow = |c: u16| u8::try_from(div_257(u64::from(c))).unwrap_or(u8::MAX);
        Rgba::new(
            narrow(self.r),
            narrow(self.g),
            narrow(self.b),
            narrow(self.a),
        )
    }

    /// `QColor::toHsv` (qcolor.cpp:2370-2415).
    fn to_hsv(self) -> QtHsv {
        let (r, g, b) = (self.r, self.g, self.b);
        let max = r.max(g).max(b);
        let min = r.min(g).min(b);
        if max == min {
            return QtHsv {
                hue: u16::MAX,
                saturation: 0,
                value: max,
                alpha: self.a,
            };
        }
        let chroma = f32::from(max.saturating_sub(min));
        let (r, g, b) = (f32::from(r), f32::from(g), f32::from(b));
        let hue = if max == self.r {
            let hue = (g - b) / chroma;
            if hue < 0.0 { hue + 6.0 } else { hue }
        } else if max == self.g {
            2.0 + (b - r) / chroma
        } else {
            4.0 + (r - g) / chroma
        };
        QtHsv {
            hue: q_round(hue * 6000.0),
            saturation: q_round(chroma / f32::from(max) * 65535.0),
            value: max,
            alpha: self.a,
        }
    }
}

impl QtHsv {
    /// `QColor::fromHsvF` (qcolor.cpp:2703-2716) for components in
    /// 0.0..=1.0, the range Kirigami clamps them to.
    fn from_hsv_f(hue: f32, saturation: f32, value: f32, alpha: f32) -> Self {
        let channel = |x: f32| q_round(x.clamp(0.0, 1.0) * 65535.0);
        Self {
            hue: q_round(hue.clamp(0.0, 1.0) * 36000.0),
            saturation: channel(saturation),
            value: channel(value),
            alpha: channel(alpha),
        }
    }

    /// `hueF()`: -1 where the hue is undefined (qcolor.cpp:1968-1973).
    fn hue_f(self) -> f32 {
        if self.hue == u16::MAX {
            -1.0
        } else {
            f32::from(self.hue) / 36000.0
        }
    }

    /// `QColor::toRgb` from HSV (qcolor.cpp:2222-2292). `from_hsv_f` and
    /// `to_hsv` give a hue of at most 36000, Qt's 0, so the sector is 0..=5.
    ///
    /// Qt 6.11.2 as PySide6 6.11.2 ships it rounds `1.0f - (s * f)` once, a
    /// fused multiply-add. That is a property of the build, not of Qt's
    /// source: the compiler may contract the expression where the target has
    /// FMA. A build that rounds the product first differs by one 16-bit unit
    /// on rare inputs, 15 of the generator's 20 000 random interpolations
    /// and 40 of 100 000 more (seed 4243), and gave the same 8-bit colour on
    /// every one of them (scripts/generate_qt_color_vectors.py).
    fn to_rgb(self) -> QtColor {
        let alpha = self.alpha;
        if self.saturation == 0 || self.hue == u16::MAX {
            let v = self.value;
            return QtColor {
                r: v,
                g: v,
                b: v,
                a: alpha,
            };
        }
        let h = if self.hue == 36000 {
            0.0
        } else {
            f32::from(self.hue) / 6000.0
        };
        let s = QtColor::f(self.saturation);
        let v = QtColor::f(self.value);
        // `h` is below 6: the hue is below 36000 here.
        let sector = h as u8;
        let f = h - f32::from(sector);
        let p = v * (1.0 - s);
        let (r, g, b) = if sector & 1 == 1 {
            let q = v * (-s).mul_add(f, 1.0);
            match sector {
                1 => (q, v, p),
                3 => (p, q, v),
                _ => (v, p, q),
            }
        } else {
            let t = v * (-s).mul_add(1.0 - f, 1.0);
            match sector {
                0 => (v, t, p),
                2 => (p, v, t),
                _ => (t, p, v),
            }
        };
        let channel = |x: f32| q_round(x * 65535.0);
        QtColor {
            r: channel(r),
            g: channel(g),
            b: channel(b),
            a: alpha,
        }
    }
}

/// `KColorUtils::mix(a, b, bias)` for opaque `a` and `b`, as kdeglobals
/// gives every colour, in 8 bits: [`mix_qt`].
fn mix(a: Rgba, b: Rgba, bias: f64) -> Rgba {
    mix_qt(a, b, bias).to_rgba()
}

/// `KColorUtils::mix(a, b, bias)` for opaque `a` and `b`: each channel moved
/// from `a` towards `b` by `bias`, in `QColor`'s channels and arithmetic
/// (kcolorutils.cpp:144-165 at kguiaddons 7c766f6f; `qreal` is f64,
/// `QColor`'s components f32).
fn mix_qt(a: Rgba, b: Rgba, bias: f64) -> QtColor {
    let (a, b) = (QtColor::from_rgba(a), QtColor::from_rgba(b));
    if bias.is_nan() || bias <= 0.0 {
        return a;
    }
    if bias >= 1.0 {
        return b;
    }
    // Both alphas are 1, so the mixed alpha is 1 and dividing by it is exact.
    let channel = |from: u16, to: u16| {
        let (from, to) = (f64::from(QtColor::f(from)), f64::from(QtColor::f(to)));
        (from + (to - from) * bias).clamp(0.0, 1.0) as f32
    };
    QtColor::from_rgb_f(channel(a.r, b.r), channel(a.g, b.g), channel(a.b, b.b), 1.0)
}

/// Kirigami's `ColorUtils::linearInterpolation(one, two, balance)` for
/// opaque colours, in 8 bits: [`linear_interpolation_qt`].
fn linear_interpolation(one: Rgba, two: Rgba, balance: f64) -> Rgba {
    linear_interpolation_qt(one, two, balance).to_rgba()
}

/// Kirigami's `ColorUtils::linearInterpolation(one, two, balance)` for
/// opaque colours: hue, saturation and value each moved from `one` towards
/// `two` by `balance`, in `QColor`'s HSV and arithmetic (kirigami
/// `src/platform/colorutils.cpp:61-80` at 8319acc7).
fn linear_interpolation_qt(one: Rgba, two: Rgba, balance: f64) -> QtColor {
    let one = QtColor::from_rgba(one).to_hsv();
    let two = QtColor::from_rgba(two).to_hsv();
    let lerp = |from: f32, to: f32| {
        let from = f64::from(from);
        from + (f64::from(to) - from) * balance
    };
    // A hue that is not above 0, a grey's undefined -1 or a red's 0, takes
    // the other colour's, and 0 where both are so.
    let hue_of = |own: QtHsv, other: QtHsv| {
        let hue = if own.hue_f() > 0.0 {
            own.hue_f()
        } else {
            other.hue_f()
        };
        hue.max(0.0)
    };
    // `std::fmod(hue, 1.0)`: the same as `rem_euclid` for the hue, which is
    // not negative.
    let hue = lerp(hue_of(one, two), hue_of(two, one)).rem_euclid(1.0);
    let saturation = lerp(QtColor::f(one.saturation), QtColor::f(two.saturation)).clamp(0.0, 1.0);
    let value = lerp(QtColor::f(one.value), QtColor::f(two.value)).clamp(0.0, 1.0);
    QtHsv::from_hsv_f(hue as f32, saturation as f32, value as f32, 1.0).to_rgb()
}

/// `Qt.tint(base, tint)`, `QQuickColorValueType::tint` (qtdeclarative
/// `src/quick/util/qquickvaluetypes.cpp:57-75` at v6.11.2, 4e3399c2): `tint`
/// laid over `base` at its own alpha, kept at `QColor`'s precision (this is
/// Qt Quick's, not `KColorUtils::tint`).
fn tint(base: QtColor, tint: QtColor) -> QtColor {
    match div_257(u64::from(tint.a)) {
        255 => return tint,
        0 => return base,
        _ => {}
    }
    let alpha = f64::from(QtColor::f(tint.a));
    let inverse = 1.0 - alpha;
    let channel = |t: u16, b: u16| {
        (f64::from(QtColor::f(t)) * alpha + f64::from(QtColor::f(b)) * inverse) as f32
    };
    QtColor::from_rgb_f(
        channel(tint.r, base.r),
        channel(tint.g, base.g),
        channel(tint.b, base.b),
        (alpha + inverse * f64::from(QtColor::f(base.a))) as f32,
    )
}

/// The alpha qqc2-desktop-style's switch tints its off track's text colour
/// by its background at, `blendBackgroundWithTextColorWithRatio(0.9)`
/// (`org.kde.desktop/private/SwitchIndicator.qml:49` at v6.31.0).
const SWITCH_OFF_TINT_ALPHA: f64 = 0.9;

/// The alpha of the highlight over the switch's on track, `Qt.alpha(
/// highlightColor, 0.5)` (SwitchIndicator.qml:62 at v6.31.0).
const SWITCH_ON_ALPHA: f64 = 0.5;

/// qqc2-desktop-style's switch off track, `Qt.tint(textColor,
/// Qt.alpha(backgroundColor, 0.9))` of the Button set
/// (SwitchIndicator.qml:21, :32-35, :49 at v6.31.0), at `QColor`'s
/// precision. `Qt.alpha` takes a double and `setAlphaF` a float.
fn switch_off_track(background: Rgba, text: Rgba) -> QtColor {
    tint(
        QtColor::from_rgba(text),
        QtColor::from_rgba(background).with_alpha_f(SWITCH_OFF_TINT_ALPHA as f32),
    )
}

/// qqc2-desktop-style's switch on track: `Qt.alpha(highlight, 0.5)` laid
/// over the off track (SwitchIndicator.qml:53-63 at v6.31.0), both at the
/// precision `QColor` stores them, in exact arithmetic, rounded once to 8
/// bits (docs/platform-facts.md §2.21).
fn switch_on_track(off: QtColor, highlight: Rgba) -> Rgba {
    let highlight = QtColor::from_rgba(highlight).with_alpha_f(SWITCH_ON_ALPHA as f32);
    let alpha = f64::from(highlight.a);
    let inverse = f64::from(u16::MAX.saturating_sub(highlight.a));
    // 255 × (h × alpha + o × inverse) is an integer below 2^41, exact in an
    // f64, so dividing by 65535² rounds only once.
    let channel = |h: u16, o: u16| {
        let numerator = (f64::from(h) * alpha + f64::from(o) * inverse) * 255.0;
        // At most 255: `h` and `o` are at most 65535 and the alphas sum to 65535.
        (numerator / (65535.0 * 65535.0)).round() as u8
    };
    Rgba::rgb(
        channel(highlight.r, off.r),
        channel(highlight.g, off.g),
        channel(highlight.b, off.b),
    )
}

/// How far a checked, non-flat Breeze button's fill moves from `Button`
/// towards `ButtonText`: `KColorUtils::mix(palette.button(),
/// palette.buttonText(), 0.125)` (breezehelper.cpp:695-699 at breeze f0b1d75;
/// docs/platform-facts.md §2.3).
const CHECKED_BUTTON_MIX: f64 = 0.125;

/// How far a pressed, enabled, non-flat Breeze button's fill moves from
/// `Button` towards `Highlight`: `KColorUtils::mix(palette.button().color(),
/// highlightColor, Metrics::Blend_Value)`, the `qreal` 0.3
/// (breezehelper.cpp:694, breezemetrics.h:176 at breeze f0b1d75;
/// docs/platform-facts.md §2.3).
const PRESSED_BUTTON_MIX: f64 = 0.3;

/// How far a Qt Widgets menu's panel moves from `Window` towards `Base`:
/// `frameBackgroundColor()`, `KColorUtils::mix(Window, Base,
/// Metrics::Blend_Value)`, the `qreal` 0.3 (`drawPanelMenuPrimitive`,
/// breezestyle.cpp:4708; breezehelper.cpp:200-203; breezemetrics.h:176 at breeze
/// f0b1d75; docs/platform-facts.md §2.6).
const MENU_PANEL_MIX: f64 = 0.3;

/// The alpha of the hover decoration a hovered, unselected Breeze tab is
/// filled with, `alphaColor(hoverColor(palette), 0.2)` (breezehelper.cpp:1497
/// at breeze f0b1d75; docs/platform-facts.md §2.11).
const TAB_HOVER_ALPHA: f32 = 0.2;

/// The alpha a Breeze scroll-bar handle's colour is filled with over the
/// window, `overlayColors(bg, alphaColor(fg, 0.5))` (breezehelper.cpp:1284
/// at breeze f0b1d75; docs/platform-facts.md §2.8).
const SCROLLBAR_HANDLE_ALPHA: f32 = 0.5;

/// The alpha of the window text over the window in a Breeze scroll-bar handle at
/// rest, the pointer off the bar: the handle colour `WindowText` at 0.5
/// (breezehelper.cpp:279), times 0.7 off the bar (breezestyle.cpp:6401-6403),
/// filled at half of that over the window, `overlayColors(Window, alphaColor(fg,
/// 0.5))` (breezehelper.cpp:1284, :349-355; breezestyle.cpp:6405) at breeze
/// f0b1d75, with animations on, their default (breeze.kcfg:45-47);
/// docs/platform-facts.md §2.8.
const SCROLLBAR_IDLE_HANDLE_ALPHA: f32 = 0.175;

/// The alpha of the accent over the window in a Breeze progress bar's
/// contents, `alphaColor(fg, 0.7)` (breezehelper.cpp:1216 at breeze f0b1d75;
/// docs/platform-facts.md §2.10).
const PROGRESS_FILL_ALPHA: f32 = 0.7;

/// `(x * a + 0x7f) / 0xff` as Qt's raster engine rounds it, `BYTE_MUL`
/// (qtbase `qdrawhelper_p.h`), for 8-bit `x` and `a`.
fn byte_mul(x: u64, a: u64) -> u64 {
    let t = x.saturating_mul(a);
    t.saturating_add(t.wrapping_shr(8))
        .saturating_add(0x80)
        .wrapping_shr(8)
}

/// `x * a / 0xffff` rounded, for 16-bit `x` and `a` (qtbase `qt_div_65535`).
fn mul_65535(x: u64, a: u64) -> u64 {
    let t = x.saturating_mul(a);
    t.saturating_add(t.wrapping_shr(16))
        .saturating_add(0x8000)
        .wrapping_shr(16)
}

/// A 16-bit value to 8 bits, `x / 257` rounded to nearest.
fn div_257(x: u64) -> u64 {
    let t = x.saturating_add(0x80);
    t.saturating_sub(t.wrapping_shr(8)).wrapping_shr(8)
}

/// `KColorUtils::overlayColors(base, paint)` for an opaque `paint` colour
/// given the alpha `alpha` by `QColor::setAlphaF`: `paint` filled
/// source-over onto the opaque `base` in a 1×1 `ARGB32_Premultiplied` image
/// (kcolorutils.cpp:168-181 at kguiaddons 7c766f6f). Qt premultiplies the
/// colour in 16 bits, stores it in 8, and blends the 8-bit destination by the
/// 8-bit inverse alpha; this arithmetic reproduces Qt 6.11.2's pixel on
/// 20 000 random inputs, so the result is Breeze's to the unit.
fn overlay_colors(base: Rgba, paint: Rgba, alpha: f32) -> Rgba {
    // QColor keeps alpha in 16 bits: qRound(alpha * 65535), in f32.
    let alpha16 = (alpha.clamp(0.0, 1.0) * 65535.0 + 0.5).floor() as u64;
    let alpha8 = div_257(alpha16);
    let inverse = 255_u64.saturating_sub(alpha8);
    let channel = |p: u8, b: u8| {
        let premultiplied = div_257(mul_65535(u64::from(p).saturating_mul(257), alpha16));
        let v = premultiplied.saturating_add(byte_mul(u64::from(b), inverse));
        u8::try_from(v).unwrap_or(u8::MAX)
    };
    Rgba::rgb(
        channel(paint.r, base.r),
        channel(paint.g, base.g),
        channel(paint.b, base.b),
    )
}

/// `KColorScheme::frameContrast()`: `[KDE] frameContrast`, clamped to
/// 0.0..=1.0, or its default where unset or not a number; a `qreal`, as
/// KConfig reads it.
fn frame_contrast(ini: &configparser::ini::Ini) -> f64 {
    ini.get("KDE", "frameContrast")
        .and_then(|v| v.trim().parse::<f64>().ok())
        .filter(|v| v.is_finite())
        .map_or(FRAME_CONTRAST_DEFAULT, |v| v.clamp(0.0, 1.0))
}

/// KColorScheme's contrast step: `[KDE] contrast` as an integer, or
/// [`CONTRAST_DEFAULT`] where unset or not an integer. [`kde_shade`] takes a
/// step beyond ±10 as ±10, so it is not bounded here.
fn contrast(ini: &configparser::ini::Ini) -> i32 {
    ini.get("KDE", "contrast")
        .and_then(|v| v.trim().parse::<i32>().ok())
        .unwrap_or(CONTRAST_DEFAULT)
}

/// KColorScheme's `MidShade` of `window` at `contrast`, which `QPalette::Mid`
/// is (kcolorscheme.cpp:685 at 27066d47; docs/platform-facts.md §2.15), read
/// back to 8 bits: [`kde_shade`] returns Qt's 16-bit channels over 65535, and
/// [`Rgba::from_f32`] rounds each times 255 to the nearest, which is
/// `qt_div_257` of the 16-bit value (`div_257` here): a channel `s / 65535`
/// times 255 is `s / 257`, never halfway between two integers.
fn mid_shade(window: Rgba, contrast: i32) -> Rgba {
    let [r, g, b, a] = kde_shade(window.to_f32_array(), KdeShadeRole::Mid, contrast as f32);
    Rgba::from_f32(r, g, b, a)
}

/// Populate a ThemeMode with colors from KDE INI color groups.
///
/// Maps all standard KDE color groups (View, Window, Button, Selection,
/// Tooltip, Header, WM) to per-widget fields on the variant.
/// Missing INI keys result in None fields (no hardcoded fallbacks), so a
/// colour the reader cannot derive keeps the preset's value.
///
/// Some colours Plasma computes from the scheme, and the reader computes
/// them as Breeze, Kirigami and qqc2-desktop-style do, in Qt's arithmetic:
/// the checked button, the checkbox and progress-bar blends, the switch
/// tracks and outlines, the toolbar's fill and edge, the popover's and the
/// card's edges, the menu's hover, separator and panel, the list's hover,
/// the table's grid and the scroll bar's resting handle. The blends on the
/// accent (the checked checkbox, the progress fill, the switch's on track and
/// its outline, and the list's hover) come from kdeglobals'
/// `[Colors:Selection] BackgroundNormal`, so an application's `with_overlay` accent or the
/// portal's (`gnome::apply_accent`, applied by `GnomePortalKdeReader`) does
/// not move them. The menu's hover comes from `[Colors:View] DecorationFocus`,
/// so the same holds for it.
///
/// The toolbar takes the Header set's fill as Breeze paints it where the
/// window decoration's border size is automatic, None or NoSides
/// (breezehelper.cpp:1904-1941 at breeze f0b1d75); the reader does not read
/// kwinrc and assumes Plasma's default, automatic.
pub(crate) fn populate_colors(ini: &configparser::ini::Ini, variant: &mut crate::ThemeMode) {
    let window_fg = get_color(ini, "Colors:Window", "ForegroundNormal");
    let view_focus = get_color(ini, "Colors:View", "DecorationFocus");

    // === defaults-level colors ===
    variant.defaults.accent_color = view_focus;
    variant.defaults.background_color = get_color(ini, "Colors:Window", "BackgroundNormal");
    variant.defaults.text_color = window_fg;
    variant.defaults.surface_color = get_color(ini, "Colors:View", "BackgroundNormal");
    // border: not set by reader -- KDE has no native border color API.
    // The preset provides the correct neutral gray (platform-facts: "(preset)").
    variant.defaults.muted_color = get_color(ini, "Colors:Window", "ForegroundInactive");
    // KDE does not expose shadow color in kdeglobals
    variant.defaults.link_color = get_color(ini, "Colors:View", "ForegroundLink");
    variant.defaults.focus_ring_color = view_focus;

    // Selection
    variant.defaults.selection_background = get_color(ini, "Colors:Selection", "BackgroundNormal");
    variant.defaults.selection_text_color = get_color(ini, "Colors:Selection", "ForegroundNormal");
    // accent_text_color: foreground on accent-colored backgrounds (platform-facts 2.1.3)
    variant.defaults.accent_text_color = get_color(ini, "Colors:Selection", "ForegroundNormal");

    // Status colors
    variant.defaults.danger_color = get_color(ini, "Colors:View", "ForegroundNegative");
    variant.defaults.danger_text_color = window_fg;
    variant.defaults.warning_color = get_color(ini, "Colors:View", "ForegroundNeutral");
    variant.defaults.warning_text_color = window_fg;
    variant.defaults.success_color = get_color(ini, "Colors:View", "ForegroundPositive");
    variant.defaults.success_text_color = window_fg;
    variant.defaults.info_color = get_color(ini, "Colors:View", "ForegroundActive");
    variant.defaults.info_text_color = window_fg;

    // Disabled
    variant.defaults.disabled_text_color = get_color(ini, "Colors:View", "ForegroundInactive");

    // === per-widget colors ===

    // Button
    variant.button.background_color = get_color(ini, "Colors:Button", "BackgroundNormal");
    if let Some(color) = get_color(ini, "Colors:Button", "ForegroundNormal") {
        variant.button.font.get_or_insert_default().color = Some(color);
    }
    // A checked button (docs/platform-facts.md §2.3): the button colour moved
    // an eighth towards the button text, lettered in the button text.
    let button_fg = get_color(ini, "Colors:Button", "ForegroundNormal");
    variant.button.checked_background = variant
        .button
        .background_color
        .zip(button_fg)
        .map(|(button, text)| mix(button, text, CHECKED_BUTTON_MIX));
    variant.button.checked_text_color = button_fg;
    // A pressed button (docs/platform-facts.md §2.3): the button colour moved
    // `Metrics::Blend_Value` towards the highlight (breezehelper.cpp:694 at
    // breeze f0b1d75); its text stays the button text, which
    // `active_text_color` inherits from the font.
    variant.button.active_background = variant
        .button
        .background_color
        .zip(get_color(ini, "Colors:Selection", "BackgroundNormal"))
        .map(|(button, highlight)| mix(button, highlight, PRESSED_BUTTON_MIX));
    // A hovered button keeps its fill and its text: Breeze's hover changes
    // only the pen (breezehelper.cpp:693-707, :710-712 at breeze f0b1d75), and
    // a non-flat button is lettered in `QPalette::ButtonText` in every state
    // (breezestyle.cpp:5350-5362; docs/platform-facts.md §2.3).
    variant.button.hover_background = variant.button.background_color;
    variant.button.hover_text_color = button_fg;

    // Tooltip
    variant.tooltip.background_color = get_color(ini, "Colors:Tooltip", "BackgroundNormal");
    if let Some(color) = get_color(ini, "Colors:Tooltip", "ForegroundNormal") {
        variant.tooltip.font.get_or_insert_default().color = Some(color);
    }

    // Sidebar (docs/platform-facts.md:1370, :1372, §2.12): the View set, as the KDE HIG's
    // sidebar, the non-modal GlobalDrawer, is docked in it (Kirigami GlobalDrawer.qml:337)
    // and System Settings' sidebar sets it (CategoriesPage.qml:19-20).
    variant.sidebar.background_color = get_color(ini, "Colors:View", "BackgroundNormal");
    if let Some(color) = get_color(ini, "Colors:View", "ForegroundNormal") {
        variant.sidebar.font.get_or_insert_default().color = Some(color);
    }

    // Input
    variant.input.background_color = get_color(ini, "Colors:View", "BackgroundNormal");
    if let Some(color) = get_color(ini, "Colors:View", "ForegroundNormal") {
        variant.input.font.get_or_insert_default().color = Some(color);
    }
    // KDE-02: placeholder from View/ForegroundInactive
    variant.input.placeholder_color = get_color(ini, "Colors:View", "ForegroundInactive");
    // input.caret from View/DecorationFocus (the focus decoration color)
    variant.input.caret_color = view_focus;
    // A field's frame (docs/platform-facts.md §2.4): `frameOutlineColor`
    // strokes a hovered one in `hoverColor()` and a focused one in
    // `focusColor()`, focus winning, the View set's DecorationHover and
    // DecorationFocus (breezehelper.cpp:89-90, :142-168 at breeze f0b1d75).
    let view_hover = get_color(ini, "Colors:View", "DecorationHover");
    variant.input.hover_border_color = view_hover;
    variant.input.focus_border_color = view_focus;

    // Checkbox and radio (docs/platform-facts.md §2.5): Breeze fills an
    // unchecked indicator with the button colour and outlines it in
    // `separatorColor()`; a checked one is that fill under the selection
    // colour at `Metrics::Blend_Value`, outlined in the selection colour; the
    // mark is the text colour.
    let button_bg = get_color(ini, "Colors:Button", "BackgroundNormal");
    let selection_bg = get_color(ini, "Colors:Selection", "BackgroundNormal");
    variant.checkbox.unchecked_background = button_bg;
    variant.checkbox.checked_background = button_bg
        .zip(selection_bg)
        .map(|(button, selection)| over(translucent(selection, BLEND_VALUE), button));
    variant.checkbox.indicator_color = get_color(ini, "Colors:View", "ForegroundNormal");
    if let Some(color) = selection_bg {
        variant.checkbox.border.get_or_insert_default().color = Some(color);
    }
    variant.checkbox.unchecked_border_color = get_color(ini, "Colors:Window", "BackgroundNormal")
        .zip(window_fg)
        .map(|(window, text)| mix(window, text, frame_contrast(ini)));

    // Slider (docs/platform-facts.md §2.9): Breeze fills the handle with the
    // button colour (breezestyle.cpp:8122 at breeze f0b1d75).
    variant.slider.thumb_color = button_bg;

    // Switch (docs/platform-facts.md §2.21): qqc2-desktop-style's tracks, the
    // off one the Button set's text tinted by its background, the on one the
    // highlight at half alpha over it; hover changes no fill (SwitchIndicator.qml
    // :49, :62, :77 at v6.31.0). The handle is the Button set's background
    // (:29). The off track and the handle are outlined in the Button set's
    // background moved towards its text by the frame contrast, the on track
    // in the highlight (:27, :30, :50, :78).
    let switch_off = button_bg
        .zip(button_fg)
        .map(|(background, text)| switch_off_track(background, text));
    variant.switch.unchecked_background = switch_off.map(QtColor::to_rgba);
    variant.switch.checked_background = switch_off
        .zip(selection_bg)
        .map(|(off, highlight)| switch_on_track(off, highlight));
    variant.switch.hover_unchecked_background = variant.switch.unchecked_background;
    variant.switch.hover_checked_background = variant.switch.checked_background;
    variant.switch.thumb_background = button_bg;
    let switch_edge = button_bg
        .zip(button_fg)
        .map(|(bg, fg)| linear_interpolation(bg, fg, frame_contrast(ini)));
    variant.switch.unchecked_track_border_color = switch_edge;
    variant.switch.thumb_border_color = switch_edge;
    variant.switch.checked_track_border_color = selection_bg;

    // Tab (docs/platform-facts.md §2.11): Breeze marks the selected tab with a
    // strip of `QPalette::Highlight`, the selection background.
    variant.tab.active_indicator_color = selection_bg;
    // A hovered, unselected tab is filled with the hover decoration at alpha
    // 0.2 in place of its own fill (breezehelper.cpp:1496-1503 at breeze
    // f0b1d75; docs/platform-facts.md §2.11).
    variant.tab.hover_background = view_hover.map(|hover| translucent(hover, TAB_HOVER_ALPHA));

    // Segmented control (docs/platform-facts.md §2.25): the selected segment
    // is the selection background; its text, the selection's text, reaches
    // it through `defaults.accent_text_color`.
    variant.segmented_control.active_background = selection_bg;

    // Progress bar (docs/platform-facts.md §2.10): Breeze fills the contents
    // with `QPalette::Accent`, the selection background
    // (kcolorscheme.cpp:681 at 27066d47), at alpha 0.7 over the window, and
    // strokes the groove in the window text at alpha `frameContrast`.
    let window_bg = get_color(ini, "Colors:Window", "BackgroundNormal");
    variant.progress_bar.fill_color = selection_bg
        .zip(window_bg)
        .map(|(accent, window)| overlay_colors(window, accent, PROGRESS_FILL_ALPHA));
    if let Some(text) = window_fg {
        variant.progress_bar.border.get_or_insert_default().color =
            Some(translucent(text, frame_contrast(ini) as f32));
    }

    // Scroll bar (docs/platform-facts.md §2.8): a hovered handle is the hover
    // decoration, at full alpha while the pointer is over the scroll bar,
    // filled at half alpha over the window (breezehelper.cpp:292, :1284;
    // breezestyle.cpp:6400-6405 at breeze f0b1d75); Breeze has no colour of
    // its own for a dragged handle, which keeps the hovered one.
    let handle = window_bg
        .zip(view_hover)
        .map(|(window, hover)| overlay_colors(window, hover, SCROLLBAR_HANDLE_ALPHA));
    variant.scrollbar.thumb_hover_color = handle;
    variant.scrollbar.thumb_active_color = handle;
    // A handle at rest, the pointer off the bar, is the window text at alpha
    // 0.175 over the window (breezehelper.cpp:279, :1284;
    // breezestyle.cpp:6401-6405 at breeze f0b1d75; docs/platform-facts.md
    // §2.8); the focused view's handle and the pointer-on-the-bar shade have
    // no field. Breeze paints no groove, which kde-breeze states as a
    // transparent track.
    variant.scrollbar.thumb_color = window_bg
        .zip(window_fg)
        .map(|(window, text)| overlay_colors(window, text, SCROLLBAR_IDLE_HANDLE_ALPHA));

    // Toolbar (docs/platform-facts.md §2.13): Breeze fills a main window's
    // tools area with the Header set's background and strokes its bottom edge
    // in that set's `separatorColor()` (breezestyle.cpp:1322, :1341-1350,
    // breezehelper.cpp:329-332, breezetoolsareamanager.cpp:201-215 at breeze
    // f0b1d75). KColorScheme takes each key the Header group lacks, or the
    // whole set where the scheme has no Header group, from `[Colors:Window]`
    // (kcolorscheme.cpp:330-332 at 27066d47); without the group Breeze paints
    // no fill and the toolbar shows the window, the same colour.
    let header_bg = get_color(ini, "Colors:Header", "BackgroundNormal").or(window_bg);
    let header_fg = get_color(ini, "Colors:Header", "ForegroundNormal").or(window_fg);
    variant.toolbar.background_color = header_bg;
    if let Some(edge) = header_bg
        .zip(header_fg)
        .map(|(bg, fg)| mix(bg, fg, frame_contrast(ini)))
    {
        variant.toolbar.border.get_or_insert_default().color = Some(edge);
    }

    // Popover (docs/platform-facts.md §2.16): its fill and text inherit the
    // window's, as qqc2-desktop-style's Popup fills the Window set's; its edge
    // is the Window set's background moved towards its text by the frame
    // contrast (Popup.qml:50 at v6.31.0). A separator inherits the border
    // colour (§2.18), not set here.
    if let Some(edge) = window_bg
        .zip(window_fg)
        .map(|(bg, fg)| linear_interpolation(bg, fg, frame_contrast(ini)))
    {
        variant.popover.border.get_or_insert_default().color = Some(edge);
    }

    // Card (docs/platform-facts.md §2.26): Kirigami's Card takes the View set,
    // its edge the background moved towards the text by the frame contrast
    // (DefaultCardBackground.qml:70, templates/AbstractCard.qml:92-93 at
    // kirigami 8319acc7); its fill inherits the surface colour.
    let view_bg = get_color(ini, "Colors:View", "BackgroundNormal");
    let view_fg = get_color(ini, "Colors:View", "ForegroundNormal");
    if let Some(edge) = view_bg
        .zip(view_fg)
        .map(|(bg, fg)| linear_interpolation(bg, fg, frame_contrast(ini)))
    {
        variant.card.border.get_or_insert_default().color = Some(edge);
    }

    // Combo box (docs/platform-facts.md §2.24): the Button set.
    variant.combo_box.background_color = button_bg;
    if let Some(color) = button_fg {
        variant.combo_box.font.get_or_insert_default().color = Some(color);
    }

    // Menu (docs/platform-facts.md §2.6): the panel is `frameBackgroundColor()`,
    // the window moved towards the view by `Blend_Value`; the text is the
    // window text; a hovered item is the focus colour at `Blend_Value`
    // lettered in the window text; the separator is `separatorColor()`, the
    // window moved towards its text by the frame contrast (breezestyle.cpp:4708,
    // :5984, :6021-6029, :6155; breezehelper.cpp:200-203, :329-332 at breeze
    // f0b1d75).
    if let Some(color) = window_fg {
        variant.menu.font.get_or_insert_default().color = Some(color);
    }
    variant.menu.hover_background = view_focus.map(|focus| translucent(focus, BLEND_VALUE));
    variant.menu.hover_text_color = window_fg;
    variant.menu.separator_color = window_bg
        .zip(window_fg)
        .map(|(window, text)| mix(window, text, frame_contrast(ini)));
    variant.menu.background_color = window_bg
        .zip(view_bg)
        .map(|(window, base)| mix(window, base, MENU_PANEL_MIX));

    // KDE-02: list fields (Colors:View is the native source for list/table content areas); a
    // hovered row is the selection at `Blend_Value` (breezestyle.cpp:4871-4875)
    variant.list.background_color = get_color(ini, "Colors:View", "BackgroundNormal");
    if let Some(color) = get_color(ini, "Colors:View", "ForegroundNormal") {
        variant.list.item_font.get_or_insert_default().color = Some(color);
    }
    variant.list.alternate_row_background = get_color(ini, "Colors:View", "BackgroundAlternate");
    // A table's grid is `QPalette::Mid`, the window's `MidShade` at the
    // scheme's contrast (qcommonstyle.cpp:5126-5128 at qtbase v6.11.0;
    // kcolorscheme.cpp:685 at 27066d47; docs/platform-facts.md §2.15).
    variant.list.grid_color = window_bg.map(|window| mid_shade(window, contrast(ini)));
    variant.list.hover_background =
        selection_bg.map(|selection| translucent(selection, BLEND_VALUE));
    // The header is the Button set: Breeze fills a `QHeaderView` section with
    // `QPalette::Button` (breezestyle.cpp:6827, :6838-6839 at breeze f0b1d75) and Qt
    // letters it `ButtonText` (qcommonstyle.cpp:1659-1660), which Breeze does not
    // override; neither reads the Header set (docs/platform-facts.md §2.15).
    variant.list.header_background = button_bg;
    if let Some(color) = button_fg {
        variant.list.header_font.get_or_insert_default().color = Some(color);
    }

    // KDE-02: link.visited
    variant.link.visited_text_color = get_color(ini, "Colors:View", "ForegroundVisited");
    // link.font.color: not set here -- it inherits View/ForegroundLink from
    // defaults.link_color, so an overlay's link_color recolours link text.

    // === KDE-01: Window Manager title bar colors ===
    variant.window.title_bar_background = get_color(ini, "WM", "activeBackground");
    if let Some(color) = get_color(ini, "WM", "activeForeground") {
        variant.window.title_bar_font.get_or_insert_default().color = Some(color);
    }
    variant.window.inactive_title_bar_background = get_color(ini, "WM", "inactiveBackground");
    variant.window.inactive_title_bar_text_color = get_color(ini, "WM", "inactiveForeground");
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::ThemeMode;

    /// Full Breeze Dark kdeglobals fixture with all 6 color groups + WM + Header.
    const BREEZE_DARK: &str = "\
[Colors:View]
BackgroundNormal=35,38,41
BackgroundAlternate=30,33,36
ForegroundNormal=252,252,252
ForegroundInactive=161,169,177
ForegroundActive=61,174,233
ForegroundLink=29,153,243
ForegroundNegative=218,68,83
ForegroundNeutral=246,116,0
ForegroundPositive=39,174,96
ForegroundVisited=155,89,182
DecorationFocus=61,174,233
DecorationHover=29,153,243

[Colors:Window]
BackgroundNormal=49,54,59
BackgroundAlternate=44,49,54
ForegroundNormal=239,240,241
ForegroundInactive=161,169,177
ForegroundActive=61,174,233
ForegroundLink=29,153,243
ForegroundNegative=218,68,83
ForegroundNeutral=246,116,0
ForegroundPositive=39,174,96
DecorationFocus=61,174,233
DecorationHover=29,153,243

[Colors:Button]
BackgroundNormal=49,54,59
BackgroundAlternate=44,49,54
ForegroundNormal=239,240,241
ForegroundInactive=161,169,177

[Colors:Selection]
BackgroundNormal=61,174,233
BackgroundAlternate=29,153,243
ForegroundNormal=252,252,252
ForegroundInactive=161,169,177

[Colors:Tooltip]
BackgroundNormal=49,54,59
ForegroundNormal=252,252,252

[Colors:Complementary]
BackgroundNormal=42,46,50
ForegroundNormal=239,240,241

[Colors:Header]
BackgroundNormal=35,38,41
ForegroundNormal=252,252,252

[WM]
activeBackground=49,54,59
activeForeground=239,240,241
inactiveBackground=42,46,50
inactiveForeground=161,169,177
";

    fn populate_fixture(content: &str) -> ThemeMode {
        let mut ini = super::super::create_kde_parser();
        ini.read(content.to_string()).unwrap();
        let mut variant = ThemeMode::default();
        populate_colors(&ini, &mut variant);
        variant
    }

    // === defaults-level color mapping ===

    #[test]
    fn test_accent_from_view_decoration_focus() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.defaults.accent_color, Some(Rgba::rgb(61, 174, 233)));
    }

    /// A scheme's accent other than Breeze's reaches the slider's value through
    /// inheritance (docs/platform-facts.md:1317, KDE ← `defaults.accent_color`),
    /// which kde-breeze does not state, and its selection colour reaches the
    /// three blends the reader writes, the checked checkbox, the on switch and
    /// the progress bar.
    #[test]
    fn a_custom_accent_reaches_the_accent_fills() {
        let breeze = populate_fixture(BREEZE_DARK);
        let custom = BREEZE_DARK
            .replace(
                "[Colors:Selection]\nBackgroundNormal=61,174,233",
                "[Colors:Selection]\nBackgroundNormal=200,40,90",
            )
            .replace("DecorationFocus=61,174,233", "DecorationFocus=200,40,90");
        assert_ne!(custom, BREEZE_DARK, "the fixture's accent moved");
        let v = populate_fixture(&custom);
        assert_eq!(
            v.slider.fill_color, None,
            "the reader leaves the slider to inheritance"
        );
        let mut mode = crate::Theme::preset("kde-breeze")
            .ok()
            .and_then(|t| t.dark)
            .unwrap_or_default();
        assert_eq!(
            mode.slider.fill_color, None,
            "kde-breeze states no slider fill"
        );
        mode.merge(&v);
        let r = mode
            .into_resolved(&crate::ResolutionContext::for_tests())
            .unwrap();
        assert_eq!(r.slider.fill_color, Rgba::rgb(200, 40, 90));
        for (field, ours, breezes) in [
            (
                "checkbox",
                v.checkbox.checked_background,
                breeze.checkbox.checked_background,
            ),
            (
                "switch",
                v.switch.checked_background,
                breeze.switch.checked_background,
            ),
            (
                "progress_bar",
                v.progress_bar.fill_color,
                breeze.progress_bar.fill_color,
            ),
        ] {
            assert!(ours.is_some(), "{field}: no fill");
            assert_ne!(
                ours, breezes,
                "{field}: the custom selection colour did not reach it"
            );
        }
    }

    /// D7: the colours Breeze paints a pressed button, a hovered and a
    /// focused field's frame, a hovered tab, the selected segment and a
    /// hovered or dragged scroll-bar handle with, each from the key it reads
    /// (docs/platform-facts.md §2.3, §2.4, §2.11, §2.25, §2.8). The fixture's
    /// DecorationHover differs from its DecorationFocus, so the frames are
    /// told apart.
    #[test]
    fn the_pressed_hovered_and_focused_colours_come_from_their_keys() {
        let v = populate_fixture(BREEZE_DARK);
        let button = Rgba::rgb(49, 54, 59);
        let selection = Rgba::rgb(61, 174, 233);
        let hover = Rgba::rgb(29, 153, 243);
        let window = Rgba::rgb(49, 54, 59);
        assert_eq!(
            v.button.active_background,
            Some(mix(button, selection, PRESSED_BUTTON_MIX))
        );
        assert_eq!(v.input.hover_border_color, Some(hover));
        assert_eq!(v.input.focus_border_color, Some(selection));
        assert_eq!(
            v.tab.hover_background,
            Some(translucent(hover, TAB_HOVER_ALPHA))
        );
        assert_eq!(v.tab.hover_background.map(|c| c.a), Some(0x33));
        assert_eq!(v.segmented_control.active_background, Some(selection));
        let handle = Some(overlay_colors(window, hover, SCROLLBAR_HANDLE_ALPHA));
        assert_eq!(v.scrollbar.thumb_hover_color, handle);
        assert_eq!(v.scrollbar.thumb_active_color, handle);
    }

    /// D7: a scheme whose selection, focus and hover colours are not
    /// Breeze's reaches the same fields, so a live desktop does not keep
    /// kde-breeze's blue there.
    #[test]
    fn a_custom_scheme_reaches_the_pressed_hovered_and_focused_colours() {
        let breeze = populate_fixture(BREEZE_DARK);
        let custom = BREEZE_DARK
            .replace(
                "[Colors:Selection]\nBackgroundNormal=61,174,233",
                "[Colors:Selection]\nBackgroundNormal=200,40,90",
            )
            .replace("DecorationFocus=61,174,233", "DecorationFocus=20,200,90")
            .replace("DecorationHover=29,153,243", "DecorationHover=240,160,20");
        assert_ne!(custom, BREEZE_DARK, "the fixture's colours moved");
        let v = populate_fixture(&custom);
        assert_eq!(v.input.hover_border_color, Some(Rgba::rgb(240, 160, 20)));
        assert_eq!(v.input.focus_border_color, Some(Rgba::rgb(20, 200, 90)));
        assert_eq!(
            v.segmented_control.active_background,
            Some(Rgba::rgb(200, 40, 90))
        );
        for (field, ours, breezes) in [
            (
                "button.active_background",
                v.button.active_background,
                breeze.button.active_background,
            ),
            (
                "tab.hover_background",
                v.tab.hover_background,
                breeze.tab.hover_background,
            ),
            (
                "scrollbar.thumb_hover_color",
                v.scrollbar.thumb_hover_color,
                breeze.scrollbar.thumb_hover_color,
            ),
            (
                "scrollbar.thumb_active_color",
                v.scrollbar.thumb_active_color,
                breeze.scrollbar.thumb_active_color,
            ),
        ] {
            assert!(ours.is_some(), "{field}: not written");
            assert_ne!(ours, breezes, "{field}: the custom scheme did not reach it");
        }
    }

    #[test]
    fn test_background_from_window() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.defaults.background_color, Some(Rgba::rgb(49, 54, 59)));
    }

    #[test]
    fn test_text_color_from_window() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.defaults.text_color, Some(Rgba::rgb(239, 240, 241)));
    }

    #[test]
    fn test_surface_from_view() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.defaults.surface_color, Some(Rgba::rgb(35, 38, 41)));
    }

    #[test]
    fn test_border_not_set_by_reader() {
        // Border is a preset value, not reader-provided (platform-facts: "(preset)")
        let v = populate_fixture(BREEZE_DARK);
        assert!(v.defaults.border.is_empty());
    }

    #[test]
    fn test_muted_from_window_foreground_inactive() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.defaults.muted_color, Some(Rgba::rgb(161, 169, 177)));
    }

    // === Status colors ===

    #[test]
    fn test_status_colors() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.defaults.danger_color, Some(Rgba::rgb(218, 68, 83)));
        assert_eq!(v.defaults.warning_color, Some(Rgba::rgb(246, 116, 0)));
        assert_eq!(v.defaults.success_color, Some(Rgba::rgb(39, 174, 96)));
        assert_eq!(v.defaults.info_color, Some(Rgba::rgb(61, 174, 233)));
    }

    // === Selection ===

    #[test]
    fn test_selection_colors() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(
            v.defaults.selection_background,
            Some(Rgba::rgb(61, 174, 233))
        );
        assert_eq!(
            v.defaults.selection_text_color,
            Some(Rgba::rgb(252, 252, 252))
        );
    }

    // === Per-widget: Checkbox and radio ===

    /// The groups Breeze's checkbox reads, from BreezeLight.colors and
    /// BreezeDark.colors at breeze v6.7.5 (:29-39, :85-95, :113-137).
    fn breeze_checkbox_groups(
        window: &str,
        window_fg: &str,
        button: &str,
        view_fg: &str,
    ) -> String {
        format!(
            "[Colors:Window]\nBackgroundNormal={window}\nForegroundNormal={window_fg}\n\n\
             [Colors:Button]\nBackgroundNormal={button}\n\n\
             [Colors:View]\nForegroundNormal={view_fg}\n\n\
             [Colors:Selection]\nBackgroundNormal=61,174,233\n"
        )
    }

    /// The groups the menu, scroll-bar and list colours read, from BreezeLight.colors
    /// and BreezeDark.colors at breeze v6.7.5: Window :129/:130, :137/:138; Button
    /// :31/:32, :39/:40; Selection :87/:88; View DecorationFocus :116/:117.
    fn breeze_menu_list_groups(
        window: &str,
        window_fg: &str,
        button: &str,
        button_fg: &str,
    ) -> String {
        format!(
            "[Colors:Window]\nBackgroundNormal={window}\nForegroundNormal={window_fg}\n\n\
             [Colors:Button]\nBackgroundNormal={button}\nForegroundNormal={button_fg}\n\n\
             [Colors:Selection]\nBackgroundNormal=61,174,233\n\n\
             [Colors:View]\nDecorationFocus=61,174,233\n"
        )
    }

    /// docs/platform-facts.md §2.5's KDE cells, which the kde-breeze preset states.
    #[test]
    fn test_checkbox_colors_are_breezes() {
        let cases = [
            (
                breeze_checkbox_groups("239,240,241", "35,38,41", "252,252,252", "35,38,41"),
                [
                    (194, 228, 246),
                    (252, 252, 252),
                    (35, 38, 41),
                    (198, 200, 201),
                ],
            ),
            (
                breeze_checkbox_groups("32,35,38", "252,252,252", "41,44,48", "252,252,252"),
                [(47, 83, 104), (41, 44, 48), (252, 252, 252), (76, 78, 81)],
            ),
        ];
        for (content, [checked, unchecked, mark, unchecked_border]) in cases {
            let v = populate_fixture(&content);
            let rgb = |(r, g, b): (u8, u8, u8)| Some(Rgba::rgb(r, g, b));
            assert_eq!(v.checkbox.checked_background, rgb(checked));
            assert_eq!(v.checkbox.unchecked_background, rgb(unchecked));
            assert_eq!(v.checkbox.indicator_color, rgb(mark));
            assert_eq!(v.checkbox.unchecked_border_color, rgb(unchecked_border));
            assert_eq!(
                v.checkbox.border.as_ref().and_then(|b| b.color),
                rgb((61, 174, 233))
            );
        }
    }

    #[test]
    fn test_checkbox_unchecked_border_follows_frame_contrast() {
        let content = breeze_checkbox_groups("0,0,0", "200,100,0", "0,0,0", "0,0,0");
        let with = |contrast: &str| {
            populate_fixture(&format!("{content}\n[KDE]\nframeContrast={contrast}\n"))
                .checkbox
                .unchecked_border_color
        };
        assert_eq!(with("0.5"), Some(Rgba::rgb(100, 50, 0)));
        assert_eq!(with("2"), Some(Rgba::rgb(200, 100, 0)), "clamped to 1");
        assert_eq!(with("none"), Some(Rgba::rgb(40, 20, 0)), "the 0.2 default");
    }

    /// Pixels `KColorUtils::overlayColors` returned under Qt 6.11.2 and KF6
    /// KGuiAddons for a colour `setAlphaF`'d to the alpha, over an opaque base
    /// (scratchpad program `ovtest.cpp`, 20 000 random inputs, of which these
    /// are six); `overlay_colors` matched all of them.
    #[test]
    fn overlay_colors_is_qts_pixel() {
        let cases = [
            ((220, 4, 101), (170, 31, 173), 0.29, (205, 12, 122)),
            ((90, 218, 229), (172, 27, 30), 0.95, (168, 37, 40)),
            ((209, 238, 57), (16, 203, 72), 0.48, (117, 221, 65)),
            ((116, 55, 154), (111, 144, 12), 0.19, (115, 72, 127)),
            ((164, 249, 31), (161, 98, 185), 0.75, (162, 135, 147)),
            ((148, 102, 144), (95, 160, 85), 0.04, (146, 104, 141)),
        ];
        for (base, paint, alpha, expected) in cases {
            let rgb = |(r, g, b): (u8, u8, u8)| Rgba::rgb(r, g, b);
            assert_eq!(
                overlay_colors(rgb(base), rgb(paint), alpha),
                rgb(expected),
                "{base:?} under {paint:?} at {alpha}"
            );
        }
    }

    /// A test vector: two colours, the factor, and the 16-bit red, green,
    /// blue and alpha `QColor` holds for the result.
    type QtVector = ([u8; 3], [u8; 3], f64, [u16; 4]);

    include!("qt_color_vectors.rs");

    fn rgb([r, g, b]: [u8; 3]) -> Rgba {
        Rgba::rgb(r, g, b)
    }

    /// `tint` gives the colour Qt 6.11.2's own `Qt.tint` stores, to the
    /// 16-bit channel (scripts/generate_qt_color_vectors.py).
    #[test]
    fn tint_is_qts() {
        for (base, over, alpha, [r, g, b, a]) in QT_TINT {
            let tinted = tint(
                QtColor::from_rgba(rgb(base)),
                QtColor::from_rgba(rgb(over)).with_alpha_f(alpha as f32),
            );
            assert_eq!(
                tinted,
                QtColor { r, g, b, a },
                "{base:?} tinted by {over:?} at {alpha}"
            );
        }
    }

    /// `mix_qt` gives the colour `KColorUtils::mix` makes over Qt 6.11.2's
    /// `QColor`, to the 16-bit channel (scripts/generate_qt_color_vectors.py).
    #[test]
    fn mix_is_kcolorutils() {
        for (one, two, bias, [r, g, b, a]) in KCOLORUTILS_MIX {
            assert_eq!(
                mix_qt(rgb(one), rgb(two), bias),
                QtColor { r, g, b, a },
                "{one:?} towards {two:?} by {bias}"
            );
        }
    }

    /// `linear_interpolation_qt` gives the colour Kirigami's
    /// `linearInterpolation` makes over Qt 6.11.2's `QColor`, to the 16-bit
    /// channel (scripts/generate_qt_color_vectors.py).
    #[test]
    fn linear_interpolation_is_kirigamis() {
        for (one, two, balance, [r, g, b, a]) in KIRIGAMI_LINEAR_INTERPOLATION {
            assert_eq!(
                linear_interpolation_qt(rgb(one), rgb(two), balance),
                QtColor { r, g, b, a },
                "{one:?} towards {two:?} by {balance}"
            );
        }
    }

    /// docs/platform-facts.md §2.10's KDE cells, which the kde-breeze preset
    /// states: the accent at 0.7 over the window, and the window text at the
    /// frame contrast for the outline.
    #[test]
    fn test_progress_bar_colors_are_breezes() {
        let cases = [
            (
                breeze_checkbox_groups("239,240,241", "35,38,41", "252,252,252", "35,38,41"),
                Rgba::rgb(0x72, 0xc2, 0xeb),
                Rgba::new(0x23, 0x26, 0x29, 0x33),
            ),
            (
                breeze_checkbox_groups("32,35,38", "252,252,252", "41,44,48", "252,252,252"),
                Rgba::rgb(0x35, 0x84, 0xae),
                Rgba::new(0xfc, 0xfc, 0xfc, 0x33),
            ),
        ];
        for (content, fill, outline) in cases {
            let v = populate_fixture(&content);
            assert_eq!(v.progress_bar.fill_color, Some(fill));
            assert_eq!(
                v.progress_bar.border.as_ref().and_then(|b| b.color),
                Some(outline)
            );
        }
    }

    /// docs/platform-facts.md §2.3's KDE cells, which the kde-breeze preset
    /// states: a checked button is `Button` mixed an eighth towards
    /// `ButtonText`, lettered in `ButtonText`.
    #[test]
    fn test_checked_button_colors_are_breezes() {
        let cases = [
            ("252,252,252", "35,38,41", Rgba::rgb(0xe1, 0xe1, 0xe2)),
            ("41,44,48", "252,252,252", Rgba::rgb(0x43, 0x46, 0x4a)),
        ];
        for (button, text, checked) in cases {
            let v = populate_fixture(&format!(
                "[Colors:Button]\nBackgroundNormal={button}\nForegroundNormal={text}\n"
            ));
            assert_eq!(v.button.checked_background, Some(checked), "{button}");
            assert_eq!(
                v.button.checked_text_color,
                v.button.font.as_ref().and_then(|f| f.color)
            );
        }
    }

    // === Per-widget: Button ===

    #[test]
    fn test_button_background_from_colors_button() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.button.background_color, Some(Rgba::rgb(49, 54, 59)));
    }

    #[test]
    fn test_button_font_color_from_colors_button() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(
            v.button.font.as_ref().and_then(|f| f.color),
            Some(Rgba::rgb(239, 240, 241))
        );
    }

    // === Per-widget: Tooltip ===

    #[test]
    fn test_tooltip_background_from_colors_tooltip() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.tooltip.background_color, Some(Rgba::rgb(49, 54, 59)));
    }

    #[test]
    fn test_tooltip_font_color_from_colors_tooltip() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(
            v.tooltip.font.as_ref().and_then(|f| f.color),
            Some(Rgba::rgb(252, 252, 252))
        );
    }

    // === Per-widget: Sidebar (View) ===

    #[test]
    fn test_sidebar_background_from_view() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.sidebar.background_color, Some(Rgba::rgb(35, 38, 41)));
    }

    #[test]
    fn test_sidebar_font_color_from_view() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(
            v.sidebar.font.as_ref().and_then(|f| f.color),
            Some(Rgba::rgb(252, 252, 252))
        );
    }

    /// The Complementary set, which no KDE sidebar uses, does not reach the
    /// sidebar (it gave Breeze Light a dark one).
    #[test]
    fn test_sidebar_ignores_complementary() {
        let content = "\
[Colors:Complementary]
BackgroundNormal=42,46,50
ForegroundNormal=239,240,241
";
        let v = populate_fixture(content);
        assert!(v.sidebar.background_color.is_none());
        assert!(v.sidebar.font.is_none());
    }

    // === KDE-01: Title bar from WM ===

    #[test]
    fn test_title_bar_background_from_wm_active() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.window.title_bar_background, Some(Rgba::rgb(49, 54, 59)));
    }

    #[test]
    fn test_title_bar_font_color_from_wm_active() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(
            v.window.title_bar_font.as_ref().and_then(|f| f.color),
            Some(Rgba::rgb(239, 240, 241))
        );
    }

    #[test]
    fn test_inactive_title_bar_background_from_wm() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(
            v.window.inactive_title_bar_background,
            Some(Rgba::rgb(42, 46, 50))
        );
    }

    #[test]
    fn test_inactive_title_bar_text_color_from_wm() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(
            v.window.inactive_title_bar_text_color,
            Some(Rgba::rgb(161, 169, 177))
        );
    }

    // === KDE-02: Input placeholder and caret ===

    #[test]
    fn test_input_placeholder_from_view_foreground_inactive() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.input.placeholder_color, Some(Rgba::rgb(161, 169, 177)));
    }

    #[test]
    fn test_input_caret_from_view_decoration_focus() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.input.caret_color, Some(Rgba::rgb(61, 174, 233)));
    }

    // === accent_text_color from Selection ===

    #[test]
    fn test_accent_text_color_from_selection() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.defaults.accent_text_color, Some(Rgba::rgb(252, 252, 252)));
    }

    // === KDE-02: List background/item_font from View ===

    #[test]
    fn test_list_background_from_view() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.list.background_color, Some(Rgba::rgb(35, 38, 41)));
    }

    #[test]
    fn test_list_item_font_color_from_view() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(
            v.list.item_font.as_ref().and_then(|f| f.color),
            Some(Rgba::rgb(252, 252, 252))
        );
    }

    // === KDE-02: List alternate_row ===

    #[test]
    fn test_list_alternate_row_from_view() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.list.alternate_row_background, Some(Rgba::rgb(30, 33, 36)));
    }

    // === List header from the Button set ===

    #[test]
    fn test_list_header_background_from_button() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.list.header_background, Some(Rgba::rgb(49, 54, 59)));
    }

    #[test]
    fn test_list_header_font_color_from_button() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(
            v.list.header_font.as_ref().and_then(|f| f.color),
            Some(Rgba::rgb(239, 240, 241))
        );
    }

    // === Menu, scroll bar and list colours Breeze derives ===

    /// The Breeze Light and Breeze Dark groups of `breeze_menu_list_groups`.
    fn breeze_light_and_dark() -> [String; 2] {
        [
            breeze_menu_list_groups("239,240,241", "35,38,41", "252,252,252", "35,38,41"),
            breeze_menu_list_groups("32,35,38", "252,252,252", "41,44,48", "252,252,252"),
        ]
    }

    #[test]
    fn test_menu_hover_and_separator_are_breezes() {
        let [light, dark] = breeze_light_and_dark();
        let cases = [
            (
                light,
                Rgba::rgb(0x23, 0x26, 0x29),
                Rgba::rgb(0xc6, 0xc8, 0xc9),
            ),
            (
                dark,
                Rgba::rgb(0xfc, 0xfc, 0xfc),
                Rgba::rgb(0x4c, 0x4e, 0x51),
            ),
        ];
        for (content, text, separator) in cases {
            let v = populate_fixture(&content);
            assert_eq!(
                v.menu.hover_background,
                Some(Rgba::new(0x3d, 0xae, 0xe9, 0x4d))
            );
            assert_eq!(v.menu.hover_text_color, Some(text));
            assert_eq!(v.menu.separator_color, Some(separator));
        }
    }

    #[test]
    fn test_scrollbar_thumb_is_breezes() {
        let [light, dark] = breeze_light_and_dark();
        let cases = [
            (light, Rgba::rgb(0xcb, 0xcd, 0xcd)),
            (dark, Rgba::rgb(0x46, 0x49, 0x4b)),
        ];
        for (content, thumb) in cases {
            let v = populate_fixture(&content);
            assert_eq!(v.scrollbar.thumb_color, Some(thumb));
            assert!(
                v.scrollbar.track_color.is_none(),
                "the reader leaves the track to the preset"
            );
        }
    }

    #[test]
    fn test_list_hover_and_header_are_breezes() {
        let [light, dark] = breeze_light_and_dark();
        let cases = [
            (
                light,
                Rgba::rgb(0xfc, 0xfc, 0xfc),
                Rgba::rgb(0x23, 0x26, 0x29),
            ),
            (
                dark,
                Rgba::rgb(0x29, 0x2c, 0x30),
                Rgba::rgb(0xfc, 0xfc, 0xfc),
            ),
        ];
        for (content, header, header_text) in cases {
            let v = populate_fixture(&content);
            assert_eq!(
                v.list.hover_background,
                Some(Rgba::new(0x3d, 0xae, 0xe9, 0x4d))
            );
            assert_eq!(v.list.header_background, Some(header));
            assert_eq!(
                v.list.header_font.as_ref().and_then(|f| f.color),
                Some(header_text)
            );
        }
    }

    /// A scheme whose window, button, selection and focus colours are not
    /// Breeze's reaches the menu's, the scroll bar's and the list's colours.
    #[test]
    fn a_custom_scheme_moves_the_menu_scrollbar_and_list_colours() {
        let [light, _] = breeze_light_and_dark();
        let breeze = populate_fixture(&light);
        let custom = breeze_menu_list_groups("230,220,200", "90,40,20", "200,180,150", "60,20,10")
            .replace(
                "[Colors:Selection]\nBackgroundNormal=61,174,233",
                "[Colors:Selection]\nBackgroundNormal=200,40,90",
            )
            .replace("DecorationFocus=61,174,233", "DecorationFocus=20,200,90");
        let v = populate_fixture(&custom);
        let window = Rgba::rgb(230, 220, 200);
        let text = Rgba::rgb(90, 40, 20);
        assert_eq!(v.menu.hover_background, Some(Rgba::new(20, 200, 90, 0x4d)));
        assert_eq!(v.menu.hover_text_color, Some(text));
        assert_eq!(
            v.menu.separator_color,
            Some(mix(window, text, FRAME_CONTRAST_DEFAULT))
        );
        assert_eq!(
            v.scrollbar.thumb_color,
            Some(overlay_colors(window, text, SCROLLBAR_IDLE_HANDLE_ALPHA))
        );
        assert_eq!(v.list.hover_background, Some(Rgba::new(200, 40, 90, 0x4d)));
        assert_eq!(v.list.header_background, Some(Rgba::rgb(200, 180, 150)));
        let header_text = |mode: &ThemeMode| mode.list.header_font.as_ref().and_then(|f| f.color);
        assert_eq!(header_text(&v), Some(Rgba::rgb(60, 20, 10)));
        for (field, ours, breezes) in [
            (
                "menu.hover_background",
                v.menu.hover_background,
                breeze.menu.hover_background,
            ),
            (
                "menu.hover_text_color",
                v.menu.hover_text_color,
                breeze.menu.hover_text_color,
            ),
            (
                "menu.separator_color",
                v.menu.separator_color,
                breeze.menu.separator_color,
            ),
            (
                "scrollbar.thumb_color",
                v.scrollbar.thumb_color,
                breeze.scrollbar.thumb_color,
            ),
            (
                "list.hover_background",
                v.list.hover_background,
                breeze.list.hover_background,
            ),
            (
                "list.header_background",
                v.list.header_background,
                breeze.list.header_background,
            ),
            (
                "list.header_font.color",
                header_text(&v),
                header_text(&breeze),
            ),
        ] {
            assert_ne!(ours, breezes, "{field}: the custom scheme did not reach it");
        }
    }

    #[test]
    fn test_menu_separator_follows_frame_contrast() {
        let content = breeze_menu_list_groups("0,0,0", "200,100,0", "252,252,252", "35,38,41");
        let with = |contrast: &str| {
            populate_fixture(&format!("{content}\n[KDE]\nframeContrast={contrast}\n"))
                .menu
                .separator_color
        };
        assert_eq!(with("0.5"), Some(Rgba::rgb(100, 50, 0)));
        assert_eq!(with("2"), Some(Rgba::rgb(200, 100, 0)), "clamped to 1");
        assert_eq!(with("none"), Some(Rgba::rgb(40, 20, 0)), "the 0.2 default");
    }

    /// The column header is the Button set's; the Header set reaches the
    /// toolbar only.
    #[test]
    fn test_the_header_ignores_the_header_set() {
        let v = populate_fixture(
            "[Colors:Header]\nBackgroundNormal=1,2,3\nForegroundNormal=4,5,6\n\n\
             [Colors:Button]\nBackgroundNormal=7,8,9\nForegroundNormal=10,11,12\n",
        );
        assert_eq!(v.list.header_background, Some(Rgba::rgb(7, 8, 9)));
        assert_eq!(
            v.list.header_font.as_ref().and_then(|f| f.color),
            Some(Rgba::rgb(10, 11, 12))
        );
        assert_eq!(v.toolbar.background_color, Some(Rgba::rgb(1, 2, 3)));
    }

    #[test]
    fn test_missing_groups_leave_the_new_fields_to_the_preset() {
        let v = populate_fixture("[Colors:Selection]\nBackgroundNormal=61,174,233\n");
        assert!(v.list.hover_background.is_some());
        assert!(v.menu.hover_background.is_none());
        assert!(v.menu.hover_text_color.is_none());
        assert!(v.menu.separator_color.is_none());
        assert!(v.scrollbar.thumb_color.is_none());
        assert!(v.list.header_background.is_none());
        assert!(v.list.header_font.is_none());

        let v = populate_fixture("[Colors:Window]\nBackgroundNormal=239,240,241\n");
        assert!(v.menu.separator_color.is_none());
        assert!(v.scrollbar.thumb_color.is_none());
        assert!(v.list.hover_background.is_none());
        assert!(v.menu.hover_background.is_none());
        assert!(v.menu.hover_text_color.is_none());
    }

    /// A table's grid is `QPalette::Mid`, the window's `MidShade` at the
    /// scheme's contrast: Breeze's measured #c4c8cc / #1c1f21
    /// (docs/platform-facts.md §2.15).
    #[test]
    fn test_table_grid_is_qts_mid_shade() {
        for (window, expected) in [
            ("239,240,241", Rgba::rgb(0xc4, 0xc8, 0xcc)),
            ("32,35,38", Rgba::rgb(0x1c, 0x1f, 0x21)),
        ] {
            let v = populate_fixture(&format!(
                "[Colors:Window]\nBackgroundNormal={window}\n\n[KDE]\ncontrast=4\n"
            ));
            assert_eq!(v.list.grid_color, Some(expected), "{window}");
        }
    }

    /// The grid follows `[KDE] contrast`; a step beyond 10 acts as 10, and a
    /// missing or non-integer value is KColorScheme's default, 7.
    #[test]
    fn test_table_grid_follows_the_contrast() {
        let grid = |kde: &str| {
            populate_fixture(&format!(
                "[Colors:Window]\nBackgroundNormal=239,240,241\n\n{kde}"
            ))
            .list
            .grid_color
        };
        assert_eq!(
            grid("[KDE]\ncontrast=0\n"),
            Some(Rgba::rgb(0xce, 0xd1, 0xd4))
        );
        assert_eq!(
            grid("[KDE]\ncontrast=-3\n"),
            Some(Rgba::rgb(0xd5, 0xd7, 0xda))
        );
        assert_eq!(
            grid("[KDE]\ncontrast=10\n"),
            Some(Rgba::rgb(0xb5, 0xba, 0xbf))
        );
        assert_eq!(
            grid("[KDE]\ncontrast=12\n"),
            grid("[KDE]\ncontrast=10\n"),
            "a step beyond 10 acts as 10"
        );
        assert_eq!(
            grid(""),
            Some(Rgba::rgb(0xbd, 0xc1, 0xc6)),
            "KColorScheme's default, 7"
        );
        assert_eq!(grid("[KDE]\ncontrast=high\n"), grid(""), "not an integer");
        assert_eq!(grid("[KDE]\ncontrast=4.5\n"), grid(""), "not an integer");
    }

    /// Breeze's menu panel, `mix(Window, Base, 0.3)`: the measured #f4f5f5 /
    /// #1c1f22 (docs/platform-facts.md §2.6).
    #[test]
    fn test_menu_panel_is_breezes() {
        for (window, view, expected) in [
            ("239,240,241", "255,255,255", Rgba::rgb(0xf4, 0xf5, 0xf5)),
            ("32,35,38", "20,22,24", Rgba::rgb(0x1c, 0x1f, 0x22)),
        ] {
            let v = populate_fixture(&format!(
                "[Colors:Window]\nBackgroundNormal={window}\n\n\
                 [Colors:View]\nBackgroundNormal={view}\n"
            ));
            assert_eq!(v.menu.background_color, Some(expected), "{window}");
        }
    }

    /// Another scheme's Window and View sets give their own panel and grid,
    /// the grid at the default step where the scheme states no contrast.
    #[test]
    fn a_custom_scheme_moves_the_menu_panel_and_the_grid() {
        let window = Rgba::rgb(230, 220, 200);
        let view = Rgba::rgb(250, 240, 230);
        let v = populate_fixture(
            "[Colors:Window]\nBackgroundNormal=230,220,200\n\n\
             [Colors:View]\nBackgroundNormal=250,240,230\n",
        );
        assert_eq!(
            v.menu.background_color,
            Some(mix(window, view, MENU_PANEL_MIX))
        );
        assert_eq!(v.list.grid_color, Some(mid_shade(window, CONTRAST_DEFAULT)));
        assert_ne!(v.menu.background_color, Some(Rgba::rgb(0xf4, 0xf5, 0xf5)));
        assert_ne!(v.list.grid_color, Some(Rgba::rgb(0xc4, 0xc8, 0xcc)));
    }

    #[test]
    fn test_missing_groups_leave_the_panel_and_the_grid_to_the_preset() {
        let v = populate_fixture("[Colors:View]\nBackgroundNormal=255,255,255\n");
        assert!(v.list.grid_color.is_none());
        assert!(v.menu.background_color.is_none());

        let v = populate_fixture("[Colors:Window]\nBackgroundNormal=239,240,241\n");
        assert!(v.list.grid_color.is_some());
        assert!(v.menu.background_color.is_none());
    }

    // === KDE-02: Link visited ===

    #[test]
    fn test_link_visited_from_view() {
        let v = populate_fixture(BREEZE_DARK);
        assert_eq!(v.link.visited_text_color, Some(Rgba::rgb(155, 89, 182)));
    }

    // === Combo box from Button, menu text from Window ===

    /// docs/platform-facts.md §2.24 and §2.6's KDE cells: the combo box in
    /// the Button set, the menu text in the Window set's foreground.
    #[test]
    fn test_combo_box_from_button_and_menu_text_from_window() {
        let v = populate_fixture(
            "[Colors:Window]\nBackgroundNormal=1,1,1\nForegroundNormal=2,2,2\n\n\
             [Colors:Button]\nBackgroundNormal=3,3,3\nForegroundNormal=4,4,4\n",
        );
        assert_eq!(v.combo_box.background_color, Some(Rgba::rgb(3, 3, 3)));
        assert_eq!(
            v.combo_box.font.as_ref().and_then(|f| f.color),
            Some(Rgba::rgb(4, 4, 4))
        );
        assert_eq!(
            v.menu.font.as_ref().and_then(|f| f.color),
            Some(Rgba::rgb(2, 2, 2))
        );
    }

    /// docs/platform-facts.md §2.16 and §2.18's KDE cells: a popover takes
    /// the window's fill and text and a separator the border colour, by
    /// inheritance, so the reader states none of them; it derives only the
    /// popover's edge.
    #[test]
    fn test_popover_and_separator_are_inherited() {
        let v = populate_fixture(BREEZE_DARK);
        assert!(v.popover.background_color.is_none());
        assert!(v.popover.font.is_none());
        assert!(v.separator.line_color.is_none());
    }

    // === Sidebar None when View missing ===

    #[test]
    fn test_sidebar_none_without_view() {
        let content = "\
[Colors:Window]
BackgroundNormal=49,54,59
ForegroundNormal=252,252,252
";
        let v = populate_fixture(content);
        assert!(v.sidebar.background_color.is_none());
        assert!(v.sidebar.font.is_none());
    }

    // === Empty INI ===

    #[test]
    fn test_empty_ini() {
        let ini = super::super::create_kde_parser();
        let mut variant = ThemeMode::default();
        populate_colors(&ini, &mut variant);
        assert!(variant.defaults.accent_color.is_none());
        assert!(variant.defaults.background_color.is_none());
        assert!(variant.defaults.text_color.is_none());
        assert!(variant.button.background_color.is_none());
    }

    // === Partial sections ===

    #[test]
    fn test_partial_sections() {
        let content = "\
[Colors:Window]
BackgroundNormal=49,54,59
ForegroundNormal=239,240,241
ForegroundInactive=161,169,177
DecorationFocus=61,174,233
";
        let v = populate_fixture(content);
        assert_eq!(v.defaults.background_color, Some(Rgba::rgb(49, 54, 59)));
        assert_eq!(v.defaults.text_color, Some(Rgba::rgb(239, 240, 241)));
        assert_eq!(v.defaults.muted_color, Some(Rgba::rgb(161, 169, 177)));
        assert!(v.defaults.border.is_empty()); // border not set by reader
        assert!(v.defaults.accent_color.is_none());
        assert!(v.defaults.surface_color.is_none());
        assert!(v.button.background_color.is_none());
    }

    // === Malformed values ===

    #[test]
    fn test_malformed_values() {
        let content = "\
[Colors:View]
BackgroundNormal=abc,def,ghi
ForegroundNormal=252,252,252
DecorationFocus=61,174,233
";
        let v = populate_fixture(content);
        assert!(v.defaults.surface_color.is_none()); // BackgroundNormal was malformed
        assert_eq!(v.defaults.accent_color, Some(Rgba::rgb(61, 174, 233)));
    }

    // === Full mapping: all non-None fields populated ===

    #[test]
    fn test_full_mapping_completeness() {
        let v = populate_fixture(BREEZE_DARK);

        // defaults-level
        assert!(v.defaults.accent_color.is_some(), "accent_color missing");
        assert!(
            v.defaults.background_color.is_some(),
            "background_color missing"
        );
        assert!(v.defaults.text_color.is_some(), "text_color missing");
        assert!(v.defaults.surface_color.is_some(), "surface_color missing");
        // border is not set by reader (preset value)
        assert!(v.defaults.muted_color.is_some(), "muted_color missing");
        assert!(v.defaults.link_color.is_some(), "link_color missing");
        assert!(
            v.defaults.selection_background.is_some(),
            "selection_background missing"
        );
        assert!(
            v.defaults.selection_text_color.is_some(),
            "selection_text_color missing"
        );
        assert!(
            v.defaults.accent_text_color.is_some(),
            "accent_text_color missing"
        );
        assert!(v.defaults.danger_color.is_some(), "danger_color missing");
        assert!(v.defaults.warning_color.is_some(), "warning_color missing");
        assert!(v.defaults.success_color.is_some(), "success_color missing");
        assert!(v.defaults.info_color.is_some(), "info_color missing");

        // per-widget
        assert!(
            v.button.background_color.is_some(),
            "button.background_color missing"
        );
        assert!(
            v.button.font.as_ref().and_then(|f| f.color).is_some(),
            "button.font.color missing"
        );
        assert!(
            v.tooltip.background_color.is_some(),
            "tooltip.background_color missing"
        );
        assert!(
            v.tooltip.font.as_ref().and_then(|f| f.color).is_some(),
            "tooltip.font.color missing"
        );
        assert!(
            v.sidebar.background_color.is_some(),
            "sidebar.background_color missing"
        );
        assert!(
            v.sidebar.font.as_ref().and_then(|f| f.color).is_some(),
            "sidebar.font.color missing"
        );
        assert!(
            v.input.background_color.is_some(),
            "input.background_color missing"
        );
        assert!(
            v.input.font.as_ref().and_then(|f| f.color).is_some(),
            "input.font.color missing"
        );
        assert!(
            v.input.placeholder_color.is_some(),
            "input.placeholder_color missing"
        );
        assert!(v.input.caret_color.is_some(), "input.caret_color missing");
        assert!(
            v.combo_box.background_color.is_some(),
            "combo_box.background_color missing"
        );
        assert!(
            v.combo_box.font.as_ref().and_then(|f| f.color).is_some(),
            "combo_box.font.color missing"
        );
        assert!(
            v.menu.font.as_ref().and_then(|f| f.color).is_some(),
            "menu.font.color missing"
        );
        assert!(
            v.menu.hover_background.is_some(),
            "menu.hover_background missing"
        );
        assert!(
            v.menu.hover_text_color.is_some(),
            "menu.hover_text_color missing"
        );
        assert!(
            v.menu.separator_color.is_some(),
            "menu.separator_color missing"
        );
        assert!(
            v.scrollbar.thumb_color.is_some(),
            "scrollbar.thumb_color missing"
        );
        assert!(
            v.list.hover_background.is_some(),
            "list.hover_background missing"
        );
        assert!(
            v.list.background_color.is_some(),
            "list.background_color missing"
        );
        assert!(
            v.list.item_font.as_ref().and_then(|f| f.color).is_some(),
            "list.item_font.color missing"
        );
        assert!(
            v.list.alternate_row_background.is_some(),
            "list.alternate_row_background missing"
        );
        assert!(
            v.list.header_background.is_some(),
            "list.header_background missing"
        );
        assert!(
            v.list.header_font.as_ref().and_then(|f| f.color).is_some(),
            "list.header_font.color missing"
        );
        assert!(v.list.grid_color.is_some(), "list.grid_color missing");
        assert!(
            v.menu.background_color.is_some(),
            "menu.background_color missing"
        );
        assert!(
            v.link.visited_text_color.is_some(),
            "link.visited_text_color missing"
        );

        // WM title bar
        assert!(
            v.window.title_bar_background.is_some(),
            "window.title_bar_background missing"
        );
        assert!(
            v.window
                .title_bar_font
                .as_ref()
                .and_then(|f| f.color)
                .is_some(),
            "window.title_bar_font.color missing"
        );
        assert!(
            v.window.inactive_title_bar_background.is_some(),
            "window.inactive_title_bar_background missing"
        );
        assert!(
            v.window.inactive_title_bar_text_color.is_some(),
            "window.inactive_title_bar_text_color missing"
        );
    }
}