culors 1.6.0

Rust port of the culori color library. Color spaces, CSS Color Module 4 parsing, interpolation, gamut mapping, ΔE, blending, filters.
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
//! Ported tests for the CSS Color Module 4 parser.
//!
//! Each expected value was produced by running culori 4.0.2's `parse()`
//! on the same input string (see `node -e` snippets in the Phase E
//! report). Channel-mode mapping: culori's modes `rgb`, `hsl`, `hwb`,
//! `lab`, `lch`, `oklab`, `oklch`, `lrgb`, `xyz65`, `xyz50` correspond
//! to the matching Rust struct in `culors::spaces`.

use culors::parse;
use culors::spaces::{Hsl, Hwb, Lab, Lch, LinearRgb, Oklab, Oklch, Rgb, Xyz50, Xyz65};
use culors::Color;

#[path = "common/mod.rs"]
mod common;

const EPS: f64 = 1e-12;

fn rgb(c: Color) -> Rgb {
    let Color::Rgb(c) = c else {
        panic!("expected Rgb, got {c:?}")
    };
    c
}
fn hsl(c: Color) -> Hsl {
    let Color::Hsl(c) = c else {
        panic!("expected Hsl, got {c:?}")
    };
    c
}
fn hwb(c: Color) -> Hwb {
    let Color::Hwb(c) = c else {
        panic!("expected Hwb, got {c:?}")
    };
    c
}
fn lab(c: Color) -> Lab {
    let Color::Lab(c) = c else {
        panic!("expected Lab, got {c:?}")
    };
    c
}
fn lch(c: Color) -> Lch {
    let Color::Lch(c) = c else {
        panic!("expected Lch, got {c:?}")
    };
    c
}
fn oklab(c: Color) -> Oklab {
    let Color::Oklab(c) = c else {
        panic!("expected Oklab, got {c:?}")
    };
    c
}
fn oklch(c: Color) -> Oklch {
    let Color::Oklch(c) = c else {
        panic!("expected Oklch, got {c:?}")
    };
    c
}
fn lrgb(c: Color) -> LinearRgb {
    let Color::LinearRgb(c) = c else {
        panic!("expected LinearRgb, got {c:?}")
    };
    c
}
fn xyz65(c: Color) -> Xyz65 {
    let Color::Xyz65(c) = c else {
        panic!("expected Xyz65, got {c:?}")
    };
    c
}
fn xyz50(c: Color) -> Xyz50 {
    let Color::Xyz50(c) = c else {
        panic!("expected Xyz50, got {c:?}")
    };
    c
}

#[test]
fn named_red() {
    let c = rgb(parse("red").unwrap());
    common::assert_close(c.r, 1.0, EPS);
    common::assert_close(c.g, 0.0, EPS);
    common::assert_close(c.b, 0.0, EPS);
    assert_eq!(c.alpha, None);
}

#[test]
fn named_cornflowerblue_case_insensitive() {
    // culori: Cornflowerblue -> r=100/255, g=149/255, b=237/255
    let c = rgb(parse("Cornflowerblue").unwrap());
    common::assert_close(c.r, 100.0 / 255.0, EPS);
    common::assert_close(c.g, 149.0 / 255.0, EPS);
    common::assert_close(c.b, 237.0 / 255.0, EPS);
}

#[test]
fn named_uppercase() {
    assert_eq!(parse("RED"), parse("red"));
}

#[test]
fn named_with_surrounding_whitespace_fails() {
    // culori does not trim before parseNamed; matches the strict
    // behavior we mirror here.
    assert!(parse("   red   ").is_none());
    assert!(parse(" red").is_none());
}

#[test]
fn transparent_keyword() {
    let c = rgb(parse("transparent").unwrap());
    assert_eq!(c.r, 0.0);
    assert_eq!(c.g, 0.0);
    assert_eq!(c.b, 0.0);
    assert_eq!(c.alpha, Some(0.0));
}

#[test]
fn currentcolor_unknown() {
    // Per culori, `currentcolor` is not recognized.
    assert!(parse("currentcolor").is_none());
}

#[test]
fn hex_three_digit() {
    assert_eq!(parse("#f00"), parse("red"));
}

#[test]
fn hex_six_digit() {
    assert_eq!(parse("#ff0000"), parse("#f00"));
}

#[test]
fn hex_eight_digit_with_alpha() {
    let c = rgb(parse("#ff0000ff").unwrap());
    common::assert_close(c.r, 1.0, EPS);
    assert_eq!(c.alpha, Some(1.0));
}

#[test]
fn hex_four_digit_zero_alpha() {
    let c = rgb(parse("#0000").unwrap());
    assert_eq!(c.r, 0.0);
    assert_eq!(c.alpha, Some(0.0));
}

#[test]
fn hex_with_whitespace_fails() {
    assert!(parse(" #f00 ").is_none());
    assert!(parse("# f00").is_none());
}

#[test]
fn rgb_modern() {
    let c = rgb(parse("rgb(255 0 0)").unwrap());
    common::assert_close(c.r, 1.0, EPS);
    common::assert_close(c.g, 0.0, EPS);
    common::assert_close(c.b, 0.0, EPS);
    assert_eq!(c.alpha, None);
}

#[test]
fn rgb_modern_with_surrounding_whitespace() {
    // Outer whitespace is trimmed before dispatch, matching culori.
    let c = rgb(parse(" rgb(255 0 0) ").unwrap());
    common::assert_close(c.r, 1.0, EPS);
    common::assert_close(c.g, 0.0, EPS);
    common::assert_close(c.b, 0.0, EPS);
    assert_eq!(c.alpha, None);
}

#[test]
fn rgb_modern_with_alpha() {
    let c = rgb(parse("rgb(255 0 0 / 0.5)").unwrap());
    assert_eq!(c.alpha, Some(0.5));
}

#[test]
fn rgb_modern_with_pct_alpha() {
    let c = rgb(parse("rgb(255 0 0 / 50%)").unwrap());
    assert_eq!(c.alpha, Some(0.5));
}

#[test]
fn rgb_legacy_numbers() {
    let c = rgb(parse("rgb(255, 0, 0)").unwrap());
    common::assert_close(c.r, 1.0, EPS);
    assert_eq!(c.alpha, None);
}

#[test]
fn rgb_legacy_percentages() {
    let c = rgb(parse("rgb(100%, 0%, 0%)").unwrap());
    common::assert_close(c.r, 1.0, EPS);
    common::assert_close(c.g, 0.0, EPS);
    common::assert_close(c.b, 0.0, EPS);
}

#[test]
fn rgba_legacy() {
    let c = rgb(parse("rgba(255, 0, 0, 0.5)").unwrap());
    common::assert_close(c.r, 1.0, EPS);
    assert_eq!(c.alpha, Some(0.5));
}

#[test]
fn rgb_function_case_sensitive() {
    // Per culori: function names are not lowercased before tokenizing.
    assert!(parse("RGB(255 0 0)").is_none());
    assert!(parse("Rgb(255 0 0)").is_none());
}

#[test]
fn rgb_none_channel_is_nan() {
    let c = rgb(parse("rgb(none 0 0)").unwrap());
    assert!(c.r.is_nan());
    assert_eq!(c.g, 0.0);
    assert_eq!(c.b, 0.0);
    assert_eq!(c.alpha, None);
}

#[test]
fn rgb_none_alpha_is_none() {
    let c = rgb(parse("rgb(255 0 0 / none)").unwrap());
    assert_eq!(c.r, 1.0);
    assert_eq!(c.alpha, None);
}

#[test]
fn rgb_oor_passes_through() {
    // Out-of-range values are not clamped during parsing.
    let c = rgb(parse("rgb(300 0 0)").unwrap());
    common::assert_close(c.r, 300.0 / 255.0, EPS);
}

#[test]
fn rgb_too_few_args() {
    assert!(parse("rgb(255 0)").is_none());
    assert!(parse("rgb(not enough)").is_none());
}

#[test]
fn rgb_round_trip_named_vs_function() {
    // Named -> alpha=None; rgb() with three args -> alpha=None.
    assert_eq!(parse("red"), parse("rgb(255 0 0)"));
    assert_eq!(parse("red"), parse("#ff0000"));
}

#[test]
fn hsl_modern() {
    let c = hsl(parse("hsl(120deg 50% 50%)").unwrap());
    assert_eq!(c.h, 120.0);
    assert_eq!(c.s, 0.5);
    assert_eq!(c.l, 0.5);
}

#[test]
fn hsl_legacy() {
    let c = hsl(parse("hsl(120, 50%, 50%)").unwrap());
    assert_eq!(c.h, 120.0);
    assert_eq!(c.s, 0.5);
    assert_eq!(c.l, 0.5);
}

#[test]
fn hsl_turn_unit_converts_to_degrees() {
    let c = hsl(parse("hsl(0.5turn 100% 50%)").unwrap());
    assert_eq!(c.h, 180.0);
    assert_eq!(c.s, 1.0);
    assert_eq!(c.l, 0.5);
}

#[test]
fn hsl_with_alpha() {
    let c = hsl(parse("hsl(120 50% 50% / 0.5)").unwrap());
    assert_eq!(c.alpha, Some(0.5));
}

#[test]
fn hwb_modern() {
    let c = hwb(parse("hwb(120 30% 30%)").unwrap());
    assert_eq!(c.h, 120.0);
    assert_eq!(c.w, 0.3);
    assert_eq!(c.b, 0.3);
}

#[test]
fn hwb_legacy_form_unsupported() {
    // hwb has no legacy comma form; commas are an error.
    assert!(parse("hwb(120, 30%, 30%)").is_none());
}

#[test]
fn lab_pct_l() {
    let c = lab(parse("lab(50% 40 -30)").unwrap());
    assert_eq!(c.l, 50.0);
    assert_eq!(c.a, 40.0);
    assert_eq!(c.b, -30.0);
}

#[test]
fn lab_number_l() {
    let c = lab(parse("lab(50 40 -30)").unwrap());
    assert_eq!(c.l, 50.0);
}

#[test]
fn lab_pct_ab_scales_to_125() {
    let c = lab(parse("lab(50% 50% -50% / 50%)").unwrap());
    assert_eq!(c.a, 62.5);
    assert_eq!(c.b, -62.5);
    assert_eq!(c.alpha, Some(0.5));
}

#[test]
fn lch_basic() {
    let c = lch(parse("lch(50% 40 30deg)").unwrap());
    assert_eq!(c.l, 50.0);
    assert_eq!(c.c, 40.0);
    assert_eq!(c.h, 30.0);
}

#[test]
fn oklab_number_l() {
    let c = oklab(parse("oklab(0.5 0.1 -0.1)").unwrap());
    assert_eq!(c.l, 0.5);
    assert_eq!(c.a, 0.1);
    assert_eq!(c.b, -0.1);
}

#[test]
fn oklab_pct_l_maps_to_unit() {
    let c = oklab(parse("oklab(50% 0.1 -0.1)").unwrap());
    assert_eq!(c.l, 0.5);
}

#[test]
fn oklch_basic() {
    let c = oklch(parse("oklch(70% 0.15 30deg)").unwrap());
    assert_eq!(c.l, 0.7);
    assert_eq!(c.c, 0.15);
    assert_eq!(c.h, 30.0);
}

#[test]
fn color_srgb() {
    let c = rgb(parse("color(srgb 1 0 0)").unwrap());
    assert_eq!(c.r, 1.0);
    assert_eq!(c.g, 0.0);
    assert_eq!(c.b, 0.0);
}

#[test]
fn color_srgb_linear() {
    let c = lrgb(parse("color(srgb-linear 1 0 0)").unwrap());
    assert_eq!(c.r, 1.0);
    assert_eq!(c.g, 0.0);
    assert_eq!(c.b, 0.0);
}

#[test]
fn color_xyz_aliases_to_d65() {
    let c = xyz65(parse("color(xyz 0.5 0.5 0.5)").unwrap());
    assert_eq!(c.x, 0.5);
    let c2 = xyz65(parse("color(xyz-d65 0.5 0.5 0.5)").unwrap());
    assert_eq!(c2.x, 0.5);
}

#[test]
fn color_xyz_d50() {
    let c = xyz50(parse("color(xyz-d50 0.5 0.5 0.5)").unwrap());
    assert_eq!(c.x, 0.5);
}

#[test]
fn color_wide_gamut_profiles() {
    let Color::P3(c) = parse("color(display-p3 1 0 0)").unwrap() else {
        panic!("expected P3");
    };
    assert_eq!(c.r, 1.0);
    assert_eq!(c.g, 0.0);
    assert_eq!(c.b, 0.0);
    let Color::Rec2020(_) = parse("color(rec2020 0.25 0.5 0.75)").unwrap() else {
        panic!("expected Rec2020");
    };
    let Color::ProphotoRgb(_) = parse("color(prophoto-rgb 1 1 1)").unwrap() else {
        panic!("expected ProphotoRgb");
    };
    let Color::A98(c) = parse("color(a98-rgb 0.5 0.5 0.5 / 0.4)").unwrap() else {
        panic!("expected A98");
    };
    assert_eq!(c.alpha, Some(0.4));
}

#[test]
fn unknown_input_returns_none() {
    assert!(parse("not a color").is_none());
    assert!(parse("").is_none());
}

#[test]
fn lab_none_channels_become_nan() {
    let c = lab(parse("lab(none none none / 0.5)").unwrap());
    assert!(c.l.is_nan());
    assert!(c.a.is_nan());
    assert!(c.b.is_nan());
    assert_eq!(c.alpha, Some(0.5));
}

// Discovered while building the parse fixture against culori's
// `parse()` (Phase E.2). The legacy comma-form parser used to stripe
// out commas without verifying the input had legal legacy structure —
// it accepted modern-style 4-positional forms (`rgb(255 0 0 0)`),
// trailing/leading commas, and mixed comma/space separators.
#[test]
fn rgb_modern_four_positional_no_slash_rejected() {
    assert!(parse("rgb(255 0 0 0)").is_none());
    assert!(parse("rgb(255 0 0 0.5)").is_none());
    assert!(parse("hsl(180 50% 50% 0.5)").is_none());
}

#[test]
fn rgb_legacy_trailing_or_misplaced_commas_rejected() {
    assert!(parse("rgb(255, 0, 0,)").is_none());
    assert!(parse("rgb(255 0 0,)").is_none());
}

#[test]
fn rgb_legacy_mixed_separators_rejected() {
    assert!(parse("hsl(180 50%, 50%)").is_none());
    assert!(parse("hsl(180, 50% 50%)").is_none());
}

// Culori's `parseRgbLegacy.js` uses two regexes (`rgb_num_old` and
// `rgb_per_old`) so legacy form requires all three RGB channels to
// share the same type. Mixed forms like `rgb(50%, 50, 0%)` are
// rejected.
#[test]
fn rgb_legacy_requires_uniform_channel_types() {
    assert!(parse("rgb(50%, 50, 0%)").is_none());
    assert!(parse("rgb(50%, 50%, 0)").is_none());
    assert!(parse("rgb(50, 50%, 0)").is_none());
    // All-num and all-per stay valid.
    assert_eq!(rgb(parse("rgb(255, 0, 0)").unwrap()).r, 1.0);
    assert_eq!(rgb(parse("rgb(100%, 0%, 0%)").unwrap()).r, 1.0);
}

// Culori's `parseHslLegacy.js` clamps S and L to [0, 1] (legacy form
// only) and requires both to be percentages. Modern syntax preserves
// out-of-range values and accepts bare numbers.
#[test]
fn hsl_legacy_clamps_saturation_lightness() {
    let neg = hsl(parse("hsl(180, -50%, 50%)").unwrap());
    assert_eq!(neg.s, 0.0);
    let big = hsl(parse("hsl(180, 150%, 50%)").unwrap());
    assert_eq!(big.s, 1.0);
    // Modern form does NOT clamp.
    let neg_modern = hsl(parse("hsl(180 -50% 50%)").unwrap());
    assert_eq!(neg_modern.s, -0.5);
    let big_modern = hsl(parse("hsl(180 150% 50%)").unwrap());
    assert_eq!(big_modern.s, 1.5);
}

#[test]
fn hsl_legacy_requires_percentage_sl() {
    // Bare numbers in legacy form are rejected.
    assert!(parse("hsl(180, 50, 50)").is_none());
    assert!(parse("hsl(180, 0.5, 0.5)").is_none());
    // Modern still accepts bare numbers (treated as percentages).
    let bare = hsl(parse("hsl(180 50 50)").unwrap());
    assert_eq!(bare.s, 0.5);
    assert_eq!(bare.l, 0.5);
}

// Edge cases for the `color()` functional notation, ported from culori
// 4.0.2's `test/color-syntax.test.js`. Each expectation comes from running
// `node -e "import('culori').then(c => console.log(JSON.stringify(c.parse(<input>))))"`.

#[test]
fn color_syntax_too_few_values_is_none() {
    // culori: every entry returns `undefined`.
    assert!(parse("color(srgb)").is_none());
    assert!(parse("color(srgb )").is_none());
    assert!(parse("color(srgb/)").is_none());
    assert!(parse("color(srgb /0.5)").is_none());
    assert!(parse("color(srgb 0.25)").is_none());
    assert!(parse("color(srgb 0.25 50%)").is_none());
    assert!(parse("color( srgb 25% .5 / 0.2)").is_none());
    // Two channels, no alpha-divider.
    assert!(parse("color(srgb 1 0)").is_none());
}

#[test]
fn color_syntax_too_many_values_is_none() {
    // culori: returns `undefined` for any extra channel before the slash.
    assert!(parse("color(srgb 25% .5 75% 0.33 0.66)").is_none());
    assert!(parse("color(srgb 25% .5 75% 0.33 0.66 / 70% )").is_none());
    assert!(parse("color(srgb 25% .5 75% 0.33 / 0.7)").is_none());
    // Four channels with no slash before alpha-like value.
    assert!(parse("color(srgb 1 0 0 0)").is_none());
}

#[test]
fn color_syntax_hue_units_in_rgb_channels_rejected() {
    // culori: `color(srgb 0.5 0.5 0deg)` -> `undefined` (rgb has no hue).
    assert!(parse("color(srgb 0.5 0.5 0deg)").is_none());
}

#[test]
fn color_syntax_clamps_negative_alpha() {
    // culori reference:
    //   parse('color(srgb 1 0 0 / -0.5)')
    //   -> { mode: 'rgb', r: 1, g: 0, b: 0, alpha: 0 }
    let parsed = parse("color(srgb 1 0 0 / -0.5)").expect("parses");
    let r = rgb(parsed);
    assert_eq!(r.alpha, Some(0.0));
    assert_eq!(r.r, 1.0);
    assert_eq!(r.g, 0.0);
    assert_eq!(r.b, 0.0);
}

#[test]
fn color_syntax_clamps_alpha_above_one() {
    // culori reference:
    //   parse('color(srgb 1 0 0 / 1.5)')
    //   -> { mode: 'rgb', r: 1, g: 0, b: 0, alpha: 1 }
    let parsed = parse("color(srgb 1 0 0 / 1.5)").expect("parses");
    let r = rgb(parsed);
    assert_eq!(r.alpha, Some(1.0));
}

#[test]
fn color_syntax_alpha_clamp_preserves_out_of_range_channels() {
    // culori reference:
    //   parse('color(srgb 1.5 -0.4 0.2 / -0.5)')
    //   -> { mode: 'rgb', r: 1.5, g: -0.4, b: 0.2, alpha: 0 }
    //   parse('color(srgb 1.5 -0.4 0.2 / 1.5)')
    //   -> { mode: 'rgb', r: 1.5, g: -0.4, b: 0.2, alpha: 1 }
    let neg = rgb(parse("color(srgb 1.5 -0.4 0.2 / -0.5)").expect("parses"));
    assert_eq!(neg.r, 1.5);
    assert_eq!(neg.g, -0.4);
    assert_eq!(neg.b, 0.2);
    assert_eq!(neg.alpha, Some(0.0));

    let big = rgb(parse("color(srgb 1.5 -0.4 0.2 / 1.5)").expect("parses"));
    assert_eq!(big.r, 1.5);
    assert_eq!(big.g, -0.4);
    assert_eq!(big.b, 0.2);
    assert_eq!(big.alpha, Some(1.0));
}

// --- v1.6: parse edge cases flagged in the v1.5 audit -------------------
//
// Each expected value comes from running culori 4.0.2's `parse()` against
// the same input string. The audit identified that culors covers the
// underlying behaviour (alpha clamping was tested for `color()` only,
// hex no-hash was implicitly accepted, bare-numeric `hsl` was implicitly
// rejected in legacy form) but did not have direct test pins.

#[test]
fn transparent_keyword_uppercase_rejected() {
    // culori treats `transparent` like a named color but does NOT
    // lowercase the input before lookup; only the exact spelling
    // `transparent` is recognized.
    assert!(parse("TRANSPARENT").is_none());
    assert!(parse("Transparent").is_none());
    assert!(parse(" transparent").is_none());
}

#[test]
fn no_hash_hex_three_six_eight_digit() {
    // culori's `parseHex` accepts strings of length 3, 4, 6, 8 with no
    // leading `#`. Each maps to the same value as the `#`-prefixed form.
    //
    // 3-digit:  parse('369') === { r: 0.2, g: 0.4, b: 0.6 }
    let three = rgb(parse("369").expect("parses"));
    common::assert_close(three.r, 0.2, EPS);
    common::assert_close(three.g, 0.4, EPS);
    common::assert_close(three.b, 0.6, EPS);
    assert_eq!(three.alpha, None);
    // Six-digit (no hash) round-trips through the hash form.
    assert_eq!(parse("ffffff"), parse("#ffffff"));
    assert_eq!(parse("369abc"), parse("#369abc"));
    assert_eq!(parse("369ABC"), parse("#369abc"));
}

#[test]
fn no_hash_hex_four_and_eight_digit_with_alpha() {
    // 4-digit no-hash: alpha is the fourth nibble.
    //   parse('1234') === { r: 1/15, g: 2/15, b: 3/15, alpha: 4/15 }
    let four = rgb(parse("1234").expect("parses"));
    common::assert_close(four.r, 1.0 / 15.0, EPS);
    common::assert_close(four.g, 2.0 / 15.0, EPS);
    common::assert_close(four.b, 3.0 / 15.0, EPS);
    assert_eq!(four.alpha, Some(4.0 / 15.0));
    // 8-digit: same as the `#`-prefixed form.
    assert_eq!(parse("12345678"), parse("#12345678"));
}

#[test]
fn invalid_hex_lengths_rejected() {
    // Lengths other than 3, 4, 6, 8 are not legal hex.
    assert!(parse("12345").is_none());
    assert!(parse("1234567").is_none());
    assert!(parse("12").is_none());
    assert!(parse("1").is_none());
}

#[test]
fn hsl_modern_accepts_bare_numbers_legacy_rejects() {
    // Modern hsl interprets bare numbers as percentages divided by 100;
    // legacy form requires the `%` suffix on saturation and lightness.
    let modern = hsl(parse("hsl(180 50 50)").expect("parses"));
    assert_eq!(modern.h, 180.0);
    assert_eq!(modern.s, 0.5);
    assert_eq!(modern.l, 0.5);

    // culori reference: parse('hsl(180 0.5 0.5)') === { s: 0.005, l: 0.005 }
    // (bare number / 100 -> 0.005, *not* the literal 0.5).
    let small = hsl(parse("hsl(180 0.5 0.5)").expect("parses"));
    assert_eq!(small.s, 0.005);
    assert_eq!(small.l, 0.005);

    // Legacy comma form rejects bare numbers (already covered by
    // `hsl_legacy_requires_percentage_sl`, retained here for the audit
    // checklist).
    assert!(parse("hsl(180, 50, 50)").is_none());
}

#[test]
fn rgb_modern_alpha_clamps_above_one_and_below_zero() {
    // culori reference:
    //   parse('rgb(255 0 0 / 1.5)') -> alpha: 1
    //   parse('rgb(255 0 0 / -1)')  -> alpha: 0
    let big = rgb(parse("rgb(255 0 0 / 1.5)").expect("parses"));
    assert_eq!(big.alpha, Some(1.0));
    let neg = rgb(parse("rgb(255 0 0 / -1)").expect("parses"));
    assert_eq!(neg.alpha, Some(0.0));
}

#[test]
fn hsl_modern_alpha_clamps_above_one() {
    let big = hsl(parse("hsl(180 50% 50% / 1.5)").expect("parses"));
    assert_eq!(big.alpha, Some(1.0));
}

#[test]
fn hwb_alpha_clamps_below_zero() {
    let neg = hwb(parse("hwb(180 30% 30% / -0.3)").expect("parses"));
    assert_eq!(neg.alpha, Some(0.0));
}

#[test]
fn lab_alpha_clamps_in_modern_syntax() {
    // culori reference:
    //   parse('lab(50 0 0 / 1.5)') -> alpha: 1
    //   parse('lab(50 0 0 / -0.5)') -> alpha: 0
    let big = lab(parse("lab(50 0 0 / 1.5)").expect("parses"));
    assert_eq!(big.alpha, Some(1.0));
    let neg = lab(parse("lab(50 0 0 / -0.5)").expect("parses"));
    assert_eq!(neg.alpha, Some(0.0));
}

#[test]
fn lch_alpha_clamps_in_modern_syntax() {
    let big = lch(parse("lch(50% 30 60 / 1.5)").expect("parses"));
    assert_eq!(big.alpha, Some(1.0));
}

#[test]
fn oklab_alpha_clamps_in_modern_syntax() {
    let neg = oklab(parse("oklab(0.5 0 0 / -0.3)").expect("parses"));
    assert_eq!(neg.alpha, Some(0.0));
}

#[test]
fn oklch_alpha_clamps_in_modern_syntax() {
    let big = oklch(parse("oklch(0.7 0.1 30 / 2)").expect("parses"));
    assert_eq!(big.alpha, Some(1.0));
}

#[test]
fn xyz_color_alpha_clamps_above_one() {
    // culori reference:
    //   parse('color(xyz 0.5 0.5 0.5 / 1.5)') -> alpha: 1
    let big = xyz65(parse("color(xyz 0.5 0.5 0.5 / 1.5)").expect("parses"));
    assert_eq!(big.alpha, Some(1.0));
}