nsh-core 0.2.0

Platform-independent core: shadow tree, styles, layout and mount instructions.
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
//! CSS value syntax shared by style strings and Tailwind classes: colors
//! (hex, `rgb()`, `hsl()`, `oklch()`, `oklab()`, named, and `color-mix()`
//! of any of them in `srgb`, `srgb-linear`, `xyz`, `oklab`, `oklch` or
//! `hsl`, as CSS Color 5 mixes), lengths, angles,
//! and the lists of `box-shadow`, `filter` and gradients. Lengths are
//! points: `px` and bare numbers are points, `rem` is 16 points.

use crate::style::Color;

/// Points per `rem` in style strings.
pub(crate) const REM: f32 = 16.0;

/// Splits `s` at `sep` outside parentheses and brackets.
pub(crate) fn split_top(s: &str, sep: char) -> Vec<&str> {
    let mut parts = Vec::new();
    let mut depth = 0i32;
    let mut start = 0;
    for (i, c) in s.char_indices() {
        match c {
            '(' | '[' => depth += 1,
            ')' | ']' => depth -= 1,
            c if c == sep && depth == 0 => {
                parts.push(s[start..i].trim());
                start = i + c.len_utf8();
            }
            _ => {}
        }
    }
    parts.push(s[start..].trim());
    parts
}

/// The whitespace-separated words of `s`, keeping functions (`rgb(0 0 0)`)
/// whole.
pub(crate) fn words(s: &str) -> Vec<&str> {
    let mut out = Vec::new();
    let mut depth = 0i32;
    let mut start: Option<usize> = None;
    for (i, c) in s.char_indices() {
        match c {
            '(' | '[' => {
                depth += 1;
                start.get_or_insert(i);
            }
            ')' | ']' => depth -= 1,
            c if c.is_whitespace() && depth == 0 => {
                if let Some(st) = start.take() {
                    out.push(&s[st..i]);
                }
            }
            _ => {
                start.get_or_insert(i);
            }
        }
    }
    if let Some(st) = start {
        out.push(&s[st..]);
    }
    out
}

/// A number, with an optional `%` (as a fraction when `percent` is set).
pub(crate) fn number(s: &str) -> Option<f32> {
    s.trim().parse::<f32>().ok().filter(|n| n.is_finite())
}

/// A length in points: `4`, `4px`, `0.5rem`, `1pt`.
pub(crate) fn length(s: &str) -> Option<f32> {
    let s = s.trim();
    if let Some(n) = s.strip_suffix("px").or_else(|| s.strip_suffix("pt")) {
        return number(n);
    }
    if let Some(n) = s.strip_suffix("rem") {
        return number(n).map(|n| n * REM);
    }
    number(s)
}

/// A length (in points) or a percentage (a fraction).
pub(crate) fn length_or_percent(s: &str) -> Option<DefiniteLength> {
    match s.trim().strip_suffix('%') {
        Some(p) => number(p).map(|p| percentage(p / 100.0).into()),
        None => length(s).map(|l| px(l).into()),
    }
}

/// An angle in degrees: `45deg`, `0.5turn`, `1rad`, `100grad`, `0`.
pub(crate) fn angle(s: &str) -> Option<f32> {
    let s = s.trim();
    if let Some(n) = s.strip_suffix("deg") {
        return number(n);
    }
    if let Some(n) = s.strip_suffix("grad") {
        return number(n).map(|n| n * 0.9);
    }
    if let Some(n) = s.strip_suffix("rad") {
        return number(n).map(f32::to_degrees);
    }
    if let Some(n) = s.strip_suffix("turn") {
        return number(n).map(|n| n * 360.0);
    }
    number(s).filter(|&n| n == 0.0)
}

/// A number or a percentage, as a factor: `1.2`, `120%`.
pub(crate) fn amount(s: &str) -> Option<f32> {
    match s.trim().strip_suffix('%') {
        Some(p) => number(p).map(|p| p / 100.0),
        None => number(s),
    }
}

// --- Colors ---------------------------------------------------------------

/// Parses a CSS color.
pub(crate) fn color(s: &str) -> Option<Color> {
    let s = s.trim();
    if let Some(hex) = s.strip_prefix('#') {
        return parse_hex(hex.as_bytes());
    }
    if let Some(open) = s.find('(') {
        let name = s[..open].trim().to_ascii_lowercase();
        let args = s[open + 1..].strip_suffix(')')?;
        return match name.as_str() {
            "rgb" | "rgba" => rgb(args),
            "hsl" | "hsla" => hsl(args),
            "oklch" => oklab(args, true),
            "oklab" => oklab(args, false),
            "color-mix" => color_mix(args),
            _ => None,
        };
    }
    let lower;
    let name = if s.bytes().any(|b| b.is_ascii_uppercase()) {
        lower = s.to_ascii_lowercase();
        lower.as_str()
    } else {
        s
    };
    if name == "transparent" {
        return Some(Color::TRANSPARENT);
    }
    crate::css_named::NAMED
        .binary_search_by(|(n, _)| (*n).cmp(name))
        .ok()
        .map(|i| Color(crate::css_named::NAMED[i].1))
}

#[inline]
fn hex_digit(b: u8) -> Option<u32> {
    match b {
        b'0'..=b'9' => Some((b - b'0') as u32),
        b'a'..=b'f' => Some((b - b'a' + 10) as u32),
        b'A'..=b'F' => Some((b - b'A' + 10) as u32),
        _ => None,
    }
}

fn parse_hex(hex: &[u8]) -> Option<Color> {
    let mut v = 0u32;
    for &b in hex {
        v = v << 4 | hex_digit(b)?;
    }
    Some(match hex.len() {
        3 | 4 => {
            // Expand each nibble: 0xabc -> 0xaabbcc.
            let (rgb, a) = if hex.len() == 3 {
                (v, 0xf)
            } else {
                (v >> 4, v & 0xf)
            };
            let r = (rgb >> 8) & 0xf;
            let g = (rgb >> 4) & 0xf;
            let b = rgb & 0xf;
            Color((r * 0x11) << 24 | (g * 0x11) << 16 | (b * 0x11) << 8 | (a * 0x11))
        }
        6 => Color(v << 8 | 0xff),
        8 => Color(v),
        _ => return None,
    })
}

/// The channels of a color function, either legacy (`1, 2, 3, 0.5`) or
/// modern (`1 2 3 / 50%`), and the alpha (1 when absent).
fn channels(args: &str) -> Option<([&str; 3], f32)> {
    let (main, alpha) = match args.split_once('/') {
        Some((m, a)) => (m, Some(a.trim())),
        None => (args, None),
    };
    let parts: Vec<&str> = if main.contains(',') {
        main.split(',').map(str::trim).collect()
    } else {
        main.split_whitespace().collect()
    };
    let (c, alpha) = match (parts.as_slice(), alpha) {
        ([a, b, c], alpha) => ([*a, *b, *c], alpha),
        ([a, b, c, d], None) => ([*a, *b, *c], Some(*d)),
        _ => return None,
    };
    let alpha = match alpha {
        Some(a) => amount(a)?.clamp(0.0, 1.0),
        None => 1.0,
    };
    Some((c, alpha))
}

fn rgb(args: &str) -> Option<Color> {
    let (c, a) = channels(args)?;
    let channel = |s: &str| -> Option<f32> {
        match s.strip_suffix('%') {
            Some(p) => number(p).map(|p| p / 100.0),
            None => number(s).map(|n| n / 255.0),
        }
    };
    Some(from_unit(channel(c[0])?, channel(c[1])?, channel(c[2])?, a))
}

fn hsl(args: &str) -> Option<Color> {
    let (c, a) = channels(args)?;
    let h = angle(c[0]).or_else(|| number(c[0]))?.rem_euclid(360.0);
    let pct = |s: &str| -> Option<f32> {
        number(s.strip_suffix('%').unwrap_or(s)).map(|v| (v / 100.0).clamp(0.0, 1.0))
    };
    let (s, l) = (pct(c[1])?, pct(c[2])?);
    let (r, g, b) = crate::color::hsl_to_rgb(h, s, l);
    Some(from_unit(r, g, b, a))
}

/// `oklch(L C H)` (`polar`) or `oklab(L a b)`, to sRGB with clamped
/// channels.
fn oklab(args: &str, polar: bool) -> Option<Color> {
    let (c, alpha) = channels(args)?;
    let l = match c[0].strip_suffix('%') {
        Some(p) => number(p)? / 100.0,
        None => number(c[0])?,
    };
    let (a, b) = if polar {
        let chroma = match c[1].strip_suffix('%') {
            Some(p) => number(p)? / 100.0 * 0.4,
            None => number(c[1])?,
        };
        let h = angle(c[2])
            .or_else(|| number(c[2]))
            .unwrap_or(0.0)
            .to_radians();
        (chroma * h.cos(), chroma * h.sin())
    } else {
        let axis = |s: &str| -> Option<f32> {
            match s.strip_suffix('%') {
                Some(p) => number(p).map(|p| p / 100.0 * 0.4),
                None => number(s),
            }
        };
        (axis(c[1])?, axis(c[2])?)
    };
    Some(oklab_to_color(l, a, b, alpha))
}

/// OKLab to sRGB (Björn Ottosson's matrices).
pub(crate) fn oklab_to_color(l: f32, a: f32, b: f32, alpha: f32) -> Color {
    let (r, g, b) = crate::color::oklab_to_rgb(l, a, b);
    from_unit(r, g, b, alpha)
}

fn from_unit(r: f32, g: f32, b: f32, a: f32) -> Color {
    use crate::color::byte;
    Color::rgba(byte(r), byte(g), byte(b), byte(a))
}

/// The color space `color-mix()` interpolates in.
#[derive(Clone, Copy, PartialEq)]
enum MixSpace {
    Srgb,
    /// Also `xyz`, `xyz-d65` and `xyz-d50`: they are linear transforms of
    /// linear sRGB, and a linear transform commutes with the (premultiplied)
    /// interpolation, so mixing in them gives the same color.
    SrgbLinear,
    Oklab,
    Oklch,
    Hsl,
}

/// How `color-mix()` interpolates a hue (CSS Color 4 "hue-interpolation").
#[derive(Clone, Copy)]
enum HueMethod {
    Shorter,
    Longer,
    Increasing,
    Decreasing,
}

/// A chroma (OKLCh) or saturation (HSL) below which the hue is powerless:
/// colors arrive packed to 8 bits, so a grey's chroma is float noise, not 0.
const ACHROMATIC: f32 = 1e-4;

/// `color-mix(in <space> [<hue-method> hue]?, <color> <pct>?, <color>
/// <pct>?)`, as CSS Color 5 defines it: percentages normalized (both
/// omitted 50 / 50, one omitted the rest of 100%, a sum other than 100%
/// scaled to it and a sum below 100% kept as an alpha factor), channels
/// interpolated with premultiplied alpha, a powerless hue taking the other
/// color's.
fn color_mix(args: &str) -> Option<Color> {
    let [method, first, second] = split_top(args, ',')[..] else {
        return None;
    };
    let (space, hue) = mix_method(method)?;
    let (c1, p1) = mix_color(first)?;
    let (c2, p2) = mix_color(second)?;
    let (p1, p2) = match (p1, p2) {
        (None, None) => (0.5, 0.5),
        (Some(p1), None) => (p1, 1.0 - p1),
        (None, Some(p2)) => (1.0 - p2, p2),
        (Some(p1), Some(p2)) => (p1, p2),
    };
    let sum = p1 + p2;
    if sum <= 0.0 {
        return None;
    }
    let w1 = p1 / sum;
    let (mut v1, a1, missing1) = to_mix_space(c1, space);
    let (mut v2, a2, missing2) = to_mix_space(c2, space);
    let alpha = a1 * w1 + a2 * (1.0 - w1);
    let hue_index = match space {
        MixSpace::Oklch => Some(2),
        MixSpace::Hsl => Some(0),
        _ => None,
    };
    let mut out = [0.0; 3];
    for i in 0..3 {
        out[i] = if hue_index == Some(i) {
            // A missing hue takes the other color's, so mixing with a grey
            // keeps the hue instead of sweeping from red (hue 0).
            match (missing1, missing2) {
                (true, false) => v1[i] = v2[i],
                (false, true) => v2[i] = v1[i],
                _ => {}
            }
            let (h1, h2) = fix_hues(v1[i], v2[i], hue.unwrap_or(HueMethod::Shorter));
            (h1 * w1 + h2 * (1.0 - w1)).rem_euclid(360.0)
        } else if alpha > 0.0 {
            (v1[i] * a1 * w1 + v2[i] * a2 * (1.0 - w1)) / alpha
        } else {
            v1[i] * w1 + v2[i] * (1.0 - w1)
        };
    }
    // A sum below 100% leaves the rest transparent.
    let alpha = alpha * sum.min(1.0);
    Some(from_mix_space(out, alpha, space))
}

/// `in <space> [<hue-method> hue]?`; a hue method only for polar spaces.
fn mix_method(s: &str) -> Option<(MixSpace, Option<HueMethod>)> {
    let lower = s.to_ascii_lowercase();
    let w: Vec<&str> = lower.split_whitespace().collect();
    let (space, rest) = match w.as_slice() {
        ["in", space, rest @ ..] => (*space, rest),
        _ => return None,
    };
    let space = match space {
        "srgb" => MixSpace::Srgb,
        "srgb-linear" | "xyz" | "xyz-d65" | "xyz-d50" => MixSpace::SrgbLinear,
        "oklab" => MixSpace::Oklab,
        "oklch" => MixSpace::Oklch,
        "hsl" => MixSpace::Hsl,
        _ => return None,
    };
    let hue = match rest {
        [] => None,
        [method, "hue"] if matches!(space, MixSpace::Oklch | MixSpace::Hsl) => {
            Some(match *method {
                "shorter" => HueMethod::Shorter,
                "longer" => HueMethod::Longer,
                "increasing" => HueMethod::Increasing,
                "decreasing" => HueMethod::Decreasing,
                _ => return None,
            })
        }
        _ => return None,
    };
    Some((space, hue))
}

/// `<color> <percentage>?` in either order; the percentage as a fraction,
/// only within `0%..=100%`.
fn mix_color(s: &str) -> Option<(Color, Option<f32>)> {
    let percent = |w: &str| -> Option<f32> {
        let p = number(w.strip_suffix('%')?)?;
        (0.0..=100.0).contains(&p).then_some(p / 100.0)
    };
    match words(s).as_slice() {
        [c] => Some((color(c)?, None)),
        [a, b] if a.ends_with('%') => Some((color(b)?, Some(percent(a)?))),
        [a, b] => Some((color(a)?, Some(percent(b)?))),
        _ => None,
    }
}

/// A color's coordinates in `space` (the hue in degrees), its alpha, and
/// whether its hue is powerless (missing, in CSS's terms).
fn to_mix_space(c: Color, space: MixSpace) -> ([f32; 3], f32, bool) {
    use crate::color::{Rgba, rgb_to_oklab, srgb_to_linear};
    let rgba = Rgba::from(c);
    let v = match space {
        MixSpace::Srgb => [rgba.r, rgba.g, rgba.b],
        MixSpace::SrgbLinear => [
            srgb_to_linear(rgba.r),
            srgb_to_linear(rgba.g),
            srgb_to_linear(rgba.b),
        ],
        MixSpace::Oklab | MixSpace::Oklch => {
            let (l, a, b) = rgb_to_oklab(rgba);
            if space == MixSpace::Oklab {
                [l, a, b]
            } else {
                let chroma = a.hypot(b);
                let hue = b.atan2(a).to_degrees().rem_euclid(360.0);
                return ([l, chroma, hue], rgba.a, chroma < ACHROMATIC);
            }
        }
        MixSpace::Hsl => {
            let hsla = rgba.to_hsla();
            return (
                [hsla.h * 360.0, hsla.s, hsla.l],
                rgba.a,
                hsla.s < ACHROMATIC,
            );
        }
    };
    (v, rgba.a, false)
}

/// The mixed coordinates back to a packed sRGB color, channels clamped.
fn from_mix_space(v: [f32; 3], alpha: f32, space: MixSpace) -> Color {
    use crate::color::linear_to_srgb;
    match space {
        MixSpace::Srgb => from_unit(v[0], v[1], v[2], alpha),
        MixSpace::SrgbLinear => from_unit(
            linear_to_srgb(v[0]),
            linear_to_srgb(v[1]),
            linear_to_srgb(v[2]),
            alpha,
        ),
        MixSpace::Oklab => oklab_to_color(v[0], v[1], v[2], alpha),
        MixSpace::Oklch => {
            let h = v[2].to_radians();
            oklab_to_color(v[0], v[1] * h.cos(), v[1] * h.sin(), alpha)
        }
        MixSpace::Hsl => {
            let (r, g, b) = crate::color::hsl_to_rgb(v[0], v[1], v[2]);
            from_unit(r, g, b, alpha)
        }
    }
}

/// Two hues (degrees in `0..360`) adjusted so a straight interpolation
/// between them goes the way `method` asks (CSS Color 4 "hue-interpolation").
fn fix_hues(mut h1: f32, mut h2: f32, method: HueMethod) -> (f32, f32) {
    let d = h2 - h1;
    match method {
        HueMethod::Shorter if d > 180.0 => h1 += 360.0,
        HueMethod::Shorter if d < -180.0 => h2 += 360.0,
        HueMethod::Longer if 0.0 < d && d < 180.0 => h1 += 360.0,
        HueMethod::Longer if -180.0 < d && d <= 0.0 => h2 += 360.0,
        HueMethod::Increasing if h2 < h1 => h2 += 360.0,
        HueMethod::Decreasing if h1 < h2 => h1 += 360.0,
        _ => {}
    }
    (h1, h2)
}

impl Color {
    /// The color with its alpha multiplied by `factor`.
    pub fn with_alpha(self, factor: f32) -> Color {
        let a = ((self.0 & 0xff) as f32 * factor.clamp(0.0, 1.0)).round() as u32;
        Color(self.0 & 0xffff_ff00 | a)
    }
}

// --- Shadows, gradients, filters --------------------------------------------

use crate::{
    effects::{BoxShadow, ColorStop, Filter, Gradient, GradientKind, GradientSize},
    geometry::{DefiniteLength, Percentage, deg, percentage, px},
};

/// A `box-shadow` list (`none` is empty).
pub(crate) fn box_shadows(s: &str) -> Option<Vec<BoxShadow>> {
    let s = s.trim();
    if s.is_empty() || s == "none" {
        return Some(Vec::new());
    }
    split_top(s, ',').into_iter().map(box_shadow).collect()
}

/// One shadow: `[inset] <x> <y> [<blur> [<spread>]] [<color>]`, words in
/// any order around the lengths.
pub(crate) fn box_shadow(s: &str) -> Option<BoxShadow> {
    let mut shadow = BoxShadow {
        color: Color(0x0000_00ff),
        ..BoxShadow::default()
    };
    let mut lengths = Vec::with_capacity(4);
    let mut color_seen = false;
    for word in words(s) {
        if word == "inset" {
            shadow.inset = true;
        } else if let Some(l) = length(word) {
            lengths.push(l);
        } else if !color_seen && let Some(c) = color(word) {
            shadow.color = c;
            color_seen = true;
        } else {
            return None;
        }
    }
    match lengths.as_slice() {
        [x, y] => (shadow.offset_x, shadow.offset_y) = (px(*x), px(*y)),
        [x, y, blur] => {
            (shadow.offset_x, shadow.offset_y, shadow.blur) = (px(*x), px(*y), px(blur.max(0.0)))
        }
        [x, y, blur, spread] => {
            (shadow.offset_x, shadow.offset_y, shadow.blur, shadow.spread) =
                (px(*x), px(*y), px(blur.max(0.0)), px(*spread))
        }
        _ => return None,
    }
    Some(shadow)
}

/// A `background-image` list of gradients (`none` is empty).
pub(crate) fn gradients(s: &str) -> Option<Vec<Gradient>> {
    let s = s.trim();
    if s.is_empty() || s == "none" {
        return Some(Vec::new());
    }
    split_top(s, ',').into_iter().map(gradient).collect()
}

/// `linear-gradient(...)`, `radial-gradient(...)`, `conic-gradient(...)`
/// (and their `repeating-` forms).
pub(crate) fn gradient(s: &str) -> Option<Gradient> {
    let s = s.trim();
    let open = s.find('(')?;
    let name = s[..open].trim();
    let args = s[open + 1..].strip_suffix(')')?;
    let (repeating, base) = match name.strip_prefix("repeating-") {
        Some(b) => (true, b),
        None => (false, name),
    };
    let parts = split_top(args, ',');
    let mut parts = parts.as_slice();
    let mut kind = match base {
        "linear-gradient" => GradientKind::Linear { angle: deg(180.0) },
        "radial-gradient" => GradientKind::Radial {
            circle: false,
            size: GradientSize::FarthestCorner,
            x: percentage(0.5),
            y: percentage(0.5),
        },
        "conic-gradient" => GradientKind::Conic {
            from: deg(0.0),
            x: percentage(0.5),
            y: percentage(0.5),
        },
        _ => return None,
    };
    // The first argument configures the gradient when it is not a stop.
    if let Some(first) = parts.first()
        && configure(&mut kind, first)
    {
        parts = &parts[1..];
    }
    let conic = matches!(kind, GradientKind::Conic { .. });
    let mut stops = Vec::with_capacity(parts.len());
    for part in parts {
        let w = words(part);
        let (c, positions) = w.split_first()?;
        let color = color(c)?;
        // A stop takes up to two positions (`red 0 10px`: a band of one
        // color).
        if positions.len() > 2 {
            return None;
        }
        if positions.is_empty() {
            stops.push(ColorStop {
                color,
                position: None,
            });
        }
        for p in positions {
            stops.push(ColorStop {
                color,
                position: Some(stop_position(p, conic)?),
            });
        }
    }
    if stops.is_empty() {
        return None;
    }
    if stops.len() == 1 {
        let only = stops[0];
        stops.push(only);
    }
    Some(Gradient {
        kind,
        stops,
        repeating,
    })
}

/// A stop's position: a percentage, or a length along a linear or radial
/// gradient's line (`10px`, `1rem`; resolved against the box when it is
/// drawn), or an angle around a conic one (`90deg`, `0.25turn`, as a
/// fraction of a turn).
fn stop_position(s: &str, conic: bool) -> Option<DefiniteLength> {
    let s = s.trim();
    if let Some(p) = s.strip_suffix('%') {
        return number(p).map(|p| percentage(p / 100.0).into());
    }
    if conic {
        angle(s).map(|a| percentage(a / 360.0).into())
    } else {
        // A zero length is the line's start whatever its length, as
        // before lengths were taken.
        length(s).map(|l| {
            if l == 0.0 {
                percentage(0.0).into()
            } else {
                px(l).into()
            }
        })
    }
}

/// Reads a gradient's configuration argument into `kind`; false when the
/// argument is a color stop.
fn configure(kind: &mut GradientKind, arg: &str) -> bool {
    // An interpolation space (`in oklab`) changes how colors mix; sRGB is
    // what the platforms draw.
    let arg = match arg
        .find(" in ")
        .or_else(|| arg.starts_with("in ").then_some(0))
    {
        Some(0) => "",
        Some(i) => arg[..i].trim(),
        None => arg,
    };
    let w = words(arg);
    if w.is_empty() {
        return true;
    }
    match kind {
        GradientKind::Linear { .. } | GradientKind::LinearCorner { .. } => {
            if w[0] == "to" {
                let mut h = 0.0f32;
                let mut v = 0.0f32;
                for side in &w[1..] {
                    match *side {
                        "top" => v = -1.0,
                        "bottom" => v = 1.0,
                        "left" => h = -1.0,
                        "right" => h = 1.0,
                        _ => return false,
                    }
                }
                *kind = match (h as i8, v as i8) {
                    (0, -1) => GradientKind::Linear { angle: deg(0.0) },
                    (1, 0) => GradientKind::Linear { angle: deg(90.0) },
                    (0, 1) => GradientKind::Linear { angle: deg(180.0) },
                    (-1, 0) => GradientKind::Linear { angle: deg(270.0) },
                    (0, 0) => return false,
                    // A corner: the angle depends on the box's shape.
                    (h, v) => GradientKind::LinearCorner {
                        right: h > 0,
                        bottom: v > 0,
                    },
                };
                true
            } else if let Some(a) = angle(w[0]) {
                *kind = GradientKind::Linear { angle: deg(a) };
                true
            } else {
                false
            }
        }
        GradientKind::Radial { circle, size, x, y } => {
            let mut i = 0;
            let mut any = false;
            while i < w.len() {
                match w[i] {
                    "circle" => *circle = true,
                    "ellipse" => *circle = false,
                    "closest-side" => *size = GradientSize::ClosestSide,
                    "closest-corner" => *size = GradientSize::ClosestCorner,
                    "farthest-side" => *size = GradientSize::FarthestSide,
                    "farthest-corner" => *size = GradientSize::FarthestCorner,
                    "at" => {
                        let Some((px, py)) = position(&w[i + 1..]) else {
                            return false;
                        };
                        (*x, *y) = (Percentage(px), Percentage(py));
                        return true;
                    }
                    _ => return any,
                }
                any = true;
                i += 1;
            }
            any
        }
        GradientKind::Conic { from, x, y } => {
            let mut i = 0;
            let mut any = false;
            while i < w.len() {
                match w[i] {
                    "from" => {
                        let Some(a) = w.get(i + 1).and_then(|s| angle(s)) else {
                            return false;
                        };
                        *from = deg(a);
                        i += 2;
                    }
                    "at" => {
                        let Some((px, py)) = position(&w[i + 1..]) else {
                            return false;
                        };
                        (*x, *y) = (Percentage(px), Percentage(py));
                        return true;
                    }
                    _ => return any,
                }
                any = true;
            }
            any
        }
    }
}

/// A position (`center`, `top left`, `25% 75%`) as fractions of the box.
pub(crate) fn position(w: &[&str]) -> Option<(f32, f32)> {
    let (mut x, mut y) = (0.5, 0.5);
    let mut xs = Vec::new();
    for word in w {
        match *word {
            "left" => x = 0.0,
            "right" => x = 1.0,
            "top" => y = 0.0,
            "bottom" => y = 1.0,
            "center" => {}
            p => xs.push(amount(p)?),
        }
    }
    match xs.as_slice() {
        [] => {}
        [a] => x = *a,
        [a, b] => (x, y) = (*a, *b),
        _ => return None,
    }
    Some((x, y))
}

/// A `filter` list (`none` is empty).
pub(crate) fn filters(s: &str) -> Option<Vec<Filter>> {
    let s = s.trim();
    if s.is_empty() || s == "none" {
        return Some(Vec::new());
    }
    words(s).into_iter().map(filter).collect()
}

fn filter(s: &str) -> Option<Filter> {
    let open = s.find('(')?;
    let name = &s[..open];
    let arg = s[open + 1..].strip_suffix(')')?.trim();
    let factor = |default: f32| {
        if arg.is_empty() {
            Some(default)
        } else {
            amount(arg)
        }
    };
    Some(match name {
        "blur" => Filter::Blur(px(if arg.is_empty() { 0.0 } else { length(arg)? })),
        "brightness" => Filter::Brightness(factor(1.0)?),
        "contrast" => Filter::Contrast(factor(1.0)?),
        "grayscale" => Filter::Grayscale(factor(1.0)?.clamp(0.0, 1.0)),
        "hue-rotate" => Filter::HueRotate(deg(if arg.is_empty() { 0.0 } else { angle(arg)? })),
        "invert" => Filter::Invert(factor(1.0)?.clamp(0.0, 1.0)),
        "opacity" => Filter::Opacity(factor(1.0)?.clamp(0.0, 1.0)),
        "saturate" => Filter::Saturate(factor(1.0)?),
        "sepia" => Filter::Sepia(factor(1.0)?.clamp(0.0, 1.0)),
        "drop-shadow" => {
            let s = box_shadow(arg)?;
            Filter::DropShadow {
                offset_x: s.offset_x,
                offset_y: s.offset_y,
                blur: s.blur,
                color: s.color,
            }
        }
        _ => return None,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The packed values CSS colors had before the Rust API took `Hsla`
    /// (`crate::color`): TSX apps render as they did.
    #[test]
    fn css_colors_keep_their_packed_values() {
        for (css, packed) in [
            ("#fff", 0xffffffff),
            ("#0000", 0x00000000),
            ("#1a2b3c", 0x1a2b3cff),
            ("#1a2b3c80", 0x1a2b3c80),
            ("rgb(255, 0, 10)", 0xff000aff),
            ("rgb(0 0 0 / 0.1)", 0x0000001a),
            ("rgba(0,0,0,0.5)", 0x00000080),
            ("rgb(50% 25% 12.5%)", 0x804020ff),
            ("rgb(12.7 200.4 99.5)", 0x0dc864ff),
            ("hsl(0 100% 50%)", 0xff0000ff),
            ("hsl(120, 100%, 25%)", 0x008000ff),
            ("hsl(240 100% 50%)", 0x0000ffff),
            ("hsl(200 50% 50%)", 0x4095bfff),
            ("hsl(30 80% 60% / 0.4)", 0xeb994766),
            ("hsl(-30 80% 60%)", 0xeb4799ff),
            ("hsl(390 80% 60%)", 0xeb9947ff),
            ("hsl(0.5turn 33% 77%)", 0xb1d8d8ff),
            ("hsl(1rad 20% 30%)", 0x5c5a3dff),
            ("hsla(210, 40%, 96%, 0.85)", 0xf1f5f9d9),
            ("hsl(359.9 100% 50%)", 0xff0000ff),
            ("hsl(17 3% 97%)", 0xf8f7f7ff),
            ("hsl(222.2 84% 4.9%)", 0x020817ff),
            ("hsl(217.2 91.2% 59.8%)", 0x3b82f6ff),
            ("hsl(0 0% 50%)", 0x808080ff),
            ("hsl(60 100% 100%)", 0xffffffff),
            ("hsl(300 76% 72%)", 0xee81eeff),
            ("hsl(45deg 93% 47%)", 0xe7b008ff),
            ("hsl(160 84% 39% / 50%)", 0x10b77f80),
            ("oklch(62.3% 0.214 259.815)", 0x2b7fffff),
            ("oklab(1 0 0)", 0xffffffff),
            ("oklch(0.7 0.1 30 / 0.5)", 0xd5867980),
            ("RebeccaPurple", 0x663399ff),
            ("transparent", 0x00000000),
            ("teal", 0x008080ff),
        ] {
            assert_eq!(color(css), Some(Color(packed)), "{css}");
            // And through the Rust API's colors.
            let hsla: crate::Hsla = css.parse().unwrap();
            assert_eq!(Color::from(hsla), Color(packed), "{css}");
        }
    }

    #[test]
    fn colors() {
        assert_eq!(color("#fff"), Some(Color(0xffff_ffff)));
        assert_eq!(color("rgb(255, 0, 10)"), Some(Color(0xff00_0aff)));
        assert_eq!(color("rgb(0 0 0 / 0.1)"), Some(Color(0x0000_001a)));
        assert_eq!(color("rgba(0,0,0,0.5)"), Some(Color(0x0000_0080)));
        assert_eq!(color("hsl(0 100% 50%)"), Some(Color(0xff00_00ff)));
        assert_eq!(color("hsl(120, 100%, 25%)"), Some(Color(0x0080_00ff)));
        assert_eq!(color("RebeccaPurple"), Some(Color(0x6633_99ff)));
        assert_eq!(color("transparent"), Some(Color::TRANSPARENT));
        // Tailwind's blue-500.
        assert_eq!(
            color("oklch(62.3% 0.214 259.815)"),
            Some(Color(0x2b7f_ffff))
        );
        assert_eq!(color("oklab(1 0 0)"), Some(Color(0xffff_ffff)));
        assert_eq!(color("nope"), None);
    }

    /// Whether two packed colors differ by at most one step per channel:
    /// mixes of polar colors go through 8-bit inputs and `f32` math.
    fn near(a: Option<Color>, b: Option<Color>) -> bool {
        let (Some(Color(a)), Some(Color(b))) = (a, b) else {
            return false;
        };
        (0..4).all(|i| ((a >> (i * 8)) & 0xff).abs_diff((b >> (i * 8)) & 0xff) <= 1)
    }

    /// `color-mix()`, against what CSS Color 5 and browsers compute.
    #[test]
    fn color_mix() {
        for (css, packed) in [
            // Both percentages omitted: 50 / 50.
            ("color-mix(in srgb, red, blue)", 0x800080ff),
            ("COLOR-MIX(IN SRGB, red, blue)", 0x800080ff),
            // One omitted: the rest of 100%, before or after the color.
            ("color-mix(in srgb, red 25%, blue)", 0x4000bfff),
            ("color-mix(in srgb, 25% red, blue)", 0x4000bfff),
            ("color-mix(in srgb, red, blue 75%)", 0x4000bfff),
            ("color-mix(in srgb, red 100%, blue)", 0xff0000ff),
            // A sum below 100%: scaled, and the result's alpha times the sum.
            ("color-mix(in srgb, red 20%, blue 30%)", 0x66009980),
            // A sum above 100%: scaled, alpha kept.
            ("color-mix(in srgb, red 75%, blue 75%)", 0x800080ff),
            // Premultiplied alpha: transparent only lowers the alpha.
            ("color-mix(in srgb, #ff0000 30%, transparent)", 0xff00004d),
            ("color-mix(in oklab, transparent, #3b82f6)", 0x3b82f680),
            ("color-mix(in oklch, transparent, #3b82f6 40%)", 0x3b82f666),
            (
                "color-mix(in srgb, rgb(255 0 0 / 0.5), rgb(0 0 255))",
                0x5500aabf,
            ),
            ("color-mix(in srgb-linear, red, blue)", 0xbc00bcff),
            ("color-mix(in xyz, red, blue)", 0xbc00bcff),
            ("color-mix(in oklab, white, black)", 0x636363ff),
            // HSL hues: shorter and longer ways around.
            (
                "color-mix(in hsl, hsl(350 100% 50%), hsl(30 100% 50%))",
                0xff2b00ff,
            ),
            (
                "color-mix(in hsl longer hue, hsl(350 100% 50%), hsl(30 100% 50%))",
                0x00d5ffff,
            ),
            // Nested.
            (
                "color-mix(in srgb, color-mix(in srgb, red, blue), black)",
                0x400040ff,
            ),
        ] {
            assert!(
                near(color(css), Some(Color(packed))),
                "{css}: {:x?}",
                color(css)
            );
        }
        // OKLCh hue interpolation, against the colors it should land on.
        let a = "oklch(0.7 0.1 350)";
        let b = "oklch(0.7 0.1 30)";
        for (method, expected) in [
            ("", "oklch(0.7 0.1 10)"),
            (" shorter hue", "oklch(0.7 0.1 10)"),
            (" longer hue", "oklch(0.7 0.1 190)"),
            (" increasing hue", "oklch(0.7 0.1 10)"),
            (" decreasing hue", "oklch(0.7 0.1 190)"),
        ] {
            let css = format!("color-mix(in oklch{method}, {a}, {b})");
            assert!(
                near(color(&css), color(expected)),
                "{css}: {:x?}",
                color(&css)
            );
        }
        // A grey's hue is powerless: the mix keeps the other color's.
        assert!(near(
            color("color-mix(in oklch, white, oklch(0.7 0.1 30))"),
            color("oklch(0.85 0.05 30)")
        ));
        assert!(near(
            color("color-mix(in hsl, white, hsl(120 100% 50%))"),
            color("hsl(120 50% 75%)")
        ));
        for css in [
            "color-mix(in srgb, red 0%, blue 0%)",
            "color-mix(in srgb, red -10%, blue)",
            "color-mix(in srgb, red 120%, blue)",
            "color-mix(in srgb, red 10% 20%, blue)",
            "color-mix(in srgb, red 10, blue)",
            "color-mix(srgb, red, blue)",
            "color-mix(in nope, red, blue)",
            "color-mix(in srgb shorter hue, red, blue)",
            "color-mix(in oklch sideways hue, red, blue)",
            "color-mix(in oklch shorter, red, blue)",
            "color-mix(in srgb, red)",
            "color-mix(in srgb, red, blue, green)",
            "color-mix(in srgb, red, nope)",
        ] {
            assert_eq!(color(css), None, "{css}");
        }
    }

    #[test]
    fn shadows() {
        let s = box_shadows("0 1px 3px 0 rgb(0 0 0 / 0.1), inset 0 1px 2px -1px #f00").unwrap();
        assert_eq!(s.len(), 2);
        assert_eq!(
            (s[0].offset_y, s[0].blur, s[0].spread),
            (px(1.0), px(3.0), px(0.0))
        );
        assert_eq!(s[0].color, Color(0x0000_001a));
        assert!(s[1].inset);
        assert_eq!(s[1].spread, px(-1.0));
        assert_eq!(s[1].color, Color(0xff00_00ff));
        assert!(box_shadows("none").unwrap().is_empty());
        assert!(box_shadows("1px").is_none());
    }

    #[test]
    fn gradient_syntax() {
        let g = gradient("linear-gradient(to right, #f00, rgb(0 0 255) 80%)").unwrap();
        assert_eq!(g.kind, GradientKind::Linear { angle: deg(90.0) });
        assert_eq!(g.stops[1].position, Some(percentage(0.8).into()));
        let g = gradient("linear-gradient(45deg in oklab, red, blue)").unwrap();
        assert_eq!(g.kind, GradientKind::Linear { angle: deg(45.0) });
        let g = gradient("radial-gradient(circle at 25% 75%, red, blue)").unwrap();
        assert_eq!(
            g.kind,
            GradientKind::Radial {
                circle: true,
                size: GradientSize::FarthestCorner,
                x: percentage(0.25),
                y: percentage(0.75)
            }
        );
        let g = gradient("conic-gradient(from 90deg, red, blue, red)").unwrap();
        assert_eq!(g.stops.len(), 3);
        assert!(gradient("linear-gradient(red)").unwrap().stops.len() == 2);
    }

    #[test]
    fn gradient_stop_positions() {
        let at = |g: &Gradient| g.stops.iter().map(|s| s.position).collect::<Vec<_>>();
        let fraction = |f: f32| DefiniteLength::from(percentage(f));
        let points = |p: f32| DefiniteLength::from(px(p));
        let g = gradient("repeating-linear-gradient(90deg, red 0px, blue 10px)").unwrap();
        assert!(g.repeating);
        assert_eq!(at(&g), [Some(fraction(0.0)), Some(points(10.0))]);
        // Lengths in every unit, mixed with percentages; two positions
        // make a band.
        let g = gradient("linear-gradient(red 0 1rem, blue 50%, green 2pt)").unwrap();
        assert_eq!(
            at(&g),
            [
                Some(fraction(0.0)),
                Some(points(16.0)),
                Some(fraction(0.5)),
                Some(points(2.0))
            ]
        );
        let g = gradient("radial-gradient(circle, red 4px, blue 20px)").unwrap();
        assert_eq!(at(&g), [Some(points(4.0)), Some(points(20.0))]);
        // Conic stops are angles (as fractions of a turn) or percentages.
        let g = gradient("conic-gradient(red 0deg 90deg, blue 0.5turn, green 75%)").unwrap();
        assert_eq!(
            at(&g),
            [
                Some(fraction(0.0)),
                Some(fraction(0.25)),
                Some(fraction(0.5)),
                Some(fraction(0.75))
            ]
        );
        assert!(gradient("conic-gradient(red 10px, blue)").is_none());
        assert!(gradient("linear-gradient(red 90deg, blue)").is_none());
        assert!(gradient("linear-gradient(red 0 10px 20px, blue)").is_none());
    }

    #[test]
    fn filter_lists() {
        let f = filters("blur(4px) brightness(120%) hue-rotate(90deg) drop-shadow(0 1px 2px #000)")
            .unwrap();
        assert_eq!(f[0], Filter::Blur(px(4.0)));
        assert_eq!(f[1], Filter::Brightness(1.2));
        assert_eq!(f[2], Filter::HueRotate(deg(90.0)));
        assert!(matches!(
            f[3],
            Filter::DropShadow {
                blur: crate::geometry::Points(2.0),
                ..
            }
        ));
    }

    #[test]
    fn units() {
        assert_eq!(length("0.5rem"), Some(8.0));
        assert_eq!(angle("0.25turn"), Some(90.0));
        assert_eq!(words("a rgb(0 0 0) b"), ["a", "rgb(0 0 0)", "b"]);
        assert_eq!(split_top("a(1,2), b", ','), ["a(1,2)", "b"]);
    }
}