color 0.3.2

A library for representing and manipulating colors
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
// Copyright 2024 the Color Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Parse CSS4 color

use core::error::Error;
use core::f64;
use core::fmt;
use core::str;
use core::str::FromStr;

use crate::{
    AlphaColor, ColorSpace, ColorSpaceTag, DynamicColor, Flags, Missing, OpaqueColor, PremulColor,
    Srgb,
};

// TODO: maybe include string offset
/// Error type for parse errors.
///
/// Discussion question: should it also contain a string offset?
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ParseError {
    /// Unclosed comment
    UnclosedComment,
    /// Unknown angle dimension
    UnknownAngleDimension,
    /// Unknown angle
    UnknownAngle,
    /// Unknown color component
    UnknownColorComponent,
    /// Unknown color identifier
    UnknownColorIdentifier,
    /// Unknown color space
    UnknownColorSpace,
    /// Unknown color syntax
    UnknownColorSyntax,
    /// Expected arguments
    ExpectedArguments,
    /// Expected closing parenthesis
    ExpectedClosingParenthesis,
    /// Expected color space identifier
    ExpectedColorSpaceIdentifier,
    /// Expected comma
    ExpectedComma,
    /// Expected end of string
    ExpectedEndOfString,
    /// Wrong number of hex digits
    WrongNumberOfHexDigits,
}

impl Error for ParseError {}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let msg = match *self {
            Self::UnclosedComment => "unclosed comment",
            Self::UnknownAngleDimension => "unknown angle dimension",
            Self::UnknownAngle => "unknown angle",
            Self::UnknownColorComponent => "unknown color component",
            Self::UnknownColorIdentifier => "unknown color identifier",
            Self::UnknownColorSpace => "unknown color space",
            Self::UnknownColorSyntax => "unknown color syntax",
            Self::ExpectedArguments => "expected arguments",
            Self::ExpectedClosingParenthesis => "expected closing parenthesis",
            Self::ExpectedColorSpaceIdentifier => "expected color space identifier",
            Self::ExpectedComma => "expected comma",
            Self::ExpectedEndOfString => "expected end of string",
            Self::WrongNumberOfHexDigits => "wrong number of hex digits",
        };
        f.write_str(msg)
    }
}

#[derive(Default)]
struct Parser<'a> {
    s: &'a str,
    ix: usize,
}

/// A parsed value.
#[derive(Debug, Clone)]
enum Value<'a> {
    Symbol(&'a str),
    Number(f64),
    Percent(f64),
    Dimension(f64, &'a str),
}

/// Whether or not we are parsing modern or legacy mode syntax.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Mode {
    Legacy,
    Modern,
}

impl Mode {
    fn alpha_separator(self) -> u8 {
        match self {
            Self::Legacy => b',',
            Self::Modern => b'/',
        }
    }
}

#[expect(
    clippy::cast_possible_truncation,
    reason = "deliberate choice of f32 for colors"
)]
fn color_from_components(components: [Option<f64>; 4], cs: ColorSpaceTag) -> DynamicColor {
    let mut missing = Missing::default();
    for (i, component) in components.iter().enumerate() {
        if component.is_none() {
            missing.insert(i);
        }
    }
    DynamicColor {
        cs,
        flags: Flags::from_missing(missing),
        components: components.map(|x| x.unwrap_or(0.0) as f32),
    }
}

impl<'a> Parser<'a> {
    fn new(s: &'a str) -> Self {
        let ix = 0;
        Parser { s, ix }
    }

    // This will be called at the start of most tokens.
    fn consume_comments(&mut self) -> Result<(), ParseError> {
        while self.s[self.ix..].starts_with("/*") {
            if let Some(i) = self.s[self.ix + 2..].find("*/") {
                self.ix += i + 4;
            } else {
                return Err(ParseError::UnclosedComment);
            }
        }
        Ok(())
    }

    fn number(&mut self) -> Option<f64> {
        self.consume_comments().ok()?;
        let tail = &self.s[self.ix..];
        let mut i = 0;
        let mut valid = false;
        if matches!(tail.as_bytes().first(), Some(b'+' | b'-')) {
            i += 1;
        }
        while let Some(c) = tail.as_bytes().get(i) {
            if c.is_ascii_digit() {
                valid = true;
                i += 1;
            } else {
                break;
            }
        }
        if let Some(b'.') = tail.as_bytes().get(i) {
            if let Some(c) = tail.as_bytes().get(i + 1) {
                if c.is_ascii_digit() {
                    valid = true;
                    i += 2;
                    while let Some(c2) = tail.as_bytes().get(i) {
                        if c2.is_ascii_digit() {
                            i += 1;
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        if matches!(tail.as_bytes().get(i), Some(b'e' | b'E')) {
            let mut j = i + 1;
            if matches!(tail.as_bytes().get(j), Some(b'+' | b'-')) {
                j += 1;
            }
            if let Some(c) = tail.as_bytes().get(j) {
                if c.is_ascii_digit() {
                    i = j + 1;
                    while let Some(c2) = tail.as_bytes().get(i) {
                        if c2.is_ascii_digit() {
                            i += 1;
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        if valid {
            // For this parse to fail would be strange, but we'll be careful.
            if let Ok(value) = tail[..i].parse() {
                self.ix += i;
                return Some(value);
            }
        }
        None
    }

    // Complies with ident-token production with three exceptions:
    // Escapes are not supported.
    // Non-ASCII characters are not supported.
    // Result is case sensitive.
    fn ident(&mut self) -> Option<&'a str> {
        // This does *not* strip initial whitespace.
        let tail = &self.s[self.ix..];
        let i_init = 0; // This exists as a vestige for syntax like :ident
        let mut i = i_init;
        while i < tail.len() {
            let b = tail.as_bytes()[i];
            if b.is_ascii_alphabetic()
                || b == b'_'
                || b == b'-'
                || ((i >= 2 || i == 1 && tail.as_bytes()[i_init] != b'-') && b.is_ascii_digit())
            {
                i += 1;
            } else {
                break;
            }
        }
        // Reject '', '-', and anything starting with '--'
        let mut j = i_init;
        while j < i.min(i_init + 2) {
            if tail.as_bytes()[j] == b'-' {
                j += 1;
            } else {
                self.ix += i;
                return Some(&tail[..i]);
            }
        }
        None
    }

    fn ch(&mut self, ch: u8) -> bool {
        if self.consume_comments().is_err() {
            return false;
        }
        self.raw_ch(ch)
    }

    /// Attempt to read the exact ASCII character given, returning whether that character was read.
    ///
    /// The parser proceeds to the next character if the character was successfully read.
    fn raw_ch(&mut self, ch: u8) -> bool {
        debug_assert!(ch.is_ascii(), "`ch` must be an ASCII character");
        if self.s.as_bytes().get(self.ix) == Some(&ch) {
            self.ix += 1;
            true
        } else {
            false
        }
    }

    fn ws_one(&mut self) -> bool {
        if self.consume_comments().is_err() {
            return false;
        }
        let tail = &self.s[self.ix..];
        let mut i = 0;
        while let Some(&b) = tail.as_bytes().get(i) {
            if !(b == b' ' || b == b'\t' || b == b'\r' || b == b'\n') {
                break;
            }
            i += 1;
        }
        self.ix += i;
        i > 0
    }

    fn ws(&mut self) -> bool {
        if !self.ws_one() {
            return false;
        }
        while self.consume_comments().is_ok() {
            if !self.ws_one() {
                break;
            }
        }
        true
    }

    fn value(&mut self) -> Option<Value<'a>> {
        if let Some(number) = self.number() {
            if self.raw_ch(b'%') {
                Some(Value::Percent(number))
            } else if let Some(unit) = self.ident() {
                Some(Value::Dimension(number, unit))
            } else {
                Some(Value::Number(number))
            }
        } else {
            self.ident().map(Value::Symbol)
        }
    }

    /// Parse a color component.
    fn scaled_component(&mut self, scale: f64, pct_scale: f64) -> Result<Option<f64>, ParseError> {
        self.ws();
        let value = self.value();
        match value {
            Some(Value::Number(n)) => Ok(Some(n * scale)),
            Some(Value::Percent(n)) => Ok(Some(n * pct_scale)),
            Some(Value::Symbol(s)) if s.eq_ignore_ascii_case("none") => Ok(None),
            _ => Err(ParseError::UnknownColorComponent),
        }
    }

    fn angle(&mut self) -> Result<Option<f64>, ParseError> {
        self.ws();
        let value = self.value();
        match value {
            Some(Value::Number(n)) => Ok(Some(n)),
            Some(Value::Symbol(s)) if s.eq_ignore_ascii_case("none") => Ok(None),
            Some(Value::Dimension(n, dim)) => {
                let mut buf = [0; LOWERCASE_BUF_SIZE];
                let dim_lc = make_lowercase(dim, &mut buf);
                let scale = match dim_lc {
                    "deg" => 1.0,
                    "rad" => {
                        // TODO: to make doubly sure this is computed at compile-time, this can be
                        // wrapped in a `const` block when our MSRV is 1.83 or greater.
                        1_f64.to_degrees()
                    }
                    "grad" => 0.9,
                    "turn" => 360.0,
                    _ => return Err(ParseError::UnknownAngleDimension),
                };
                Ok(Some(n * scale))
            }
            _ => Err(ParseError::UnknownAngle),
        }
    }

    fn optional_comma(&mut self, comma: bool) -> Result<(), ParseError> {
        self.ws();
        if comma && !self.ch(b',') {
            Err(ParseError::ExpectedComma)
        } else {
            Ok(())
        }
    }

    fn rgb(&mut self) -> Result<DynamicColor, ParseError> {
        if !self.raw_ch(b'(') {
            return Err(ParseError::ExpectedArguments);
        }
        // TODO: in legacy mode, be stricter about not mixing numbers
        // and percentages, and disallowing "none"
        let r = self
            .scaled_component(1. / 255., 0.01)?
            .map(|x| x.clamp(0., 1.));
        self.ws();
        let comma = self.ch(b',');
        let mode = if comma { Mode::Legacy } else { Mode::Modern };
        let g = self
            .scaled_component(1. / 255., 0.01)?
            .map(|x| x.clamp(0., 1.));
        self.optional_comma(comma)?;
        let b = self
            .scaled_component(1. / 255., 0.01)?
            .map(|x| x.clamp(0., 1.));
        let alpha = self.alpha(mode)?;
        self.ws();
        if !self.ch(b')') {
            return Err(ParseError::ExpectedClosingParenthesis);
        }
        Ok(color_from_components([r, g, b, alpha], ColorSpaceTag::Srgb))
    }

    /// Read a slash separator and an alpha value.
    ///
    /// The value may be either number or a percentage.
    ///
    /// The alpha value defaults to `1.0` if not present. The value will be clamped
    /// to the range [0, 1].
    ///
    /// If the value is `"none"`, then `Ok(None)` will be returned.
    ///
    /// The separator will be a `'/'` in modern mode and a `','` in legacy mode.
    /// If no separator is present, then the default value will be returned.
    ///
    /// Reference: § 4.2 of CSS Color 4 spec.
    fn alpha(&mut self, mode: Mode) -> Result<Option<f64>, ParseError> {
        self.ws();
        if self.ch(mode.alpha_separator()) {
            Ok(self.scaled_component(1., 0.01)?.map(|a| a.clamp(0., 1.)))
        } else {
            Ok(Some(1.0))
        }
    }

    fn lab(&mut self, lmax: f64, c: f64, tag: ColorSpaceTag) -> Result<DynamicColor, ParseError> {
        if !self.raw_ch(b'(') {
            return Err(ParseError::ExpectedArguments);
        }
        let l = self
            .scaled_component(1., 0.01 * lmax)?
            .map(|x| x.clamp(0., lmax));
        let a = self.scaled_component(1., c)?;
        let b = self.scaled_component(1., c)?;
        let alpha = self.alpha(Mode::Modern)?;
        self.ws();
        if !self.ch(b')') {
            return Err(ParseError::ExpectedClosingParenthesis);
        }
        Ok(color_from_components([l, a, b, alpha], tag))
    }

    fn lch(&mut self, lmax: f64, c: f64, tag: ColorSpaceTag) -> Result<DynamicColor, ParseError> {
        if !self.raw_ch(b'(') {
            return Err(ParseError::ExpectedArguments);
        }
        let l = self
            .scaled_component(1., 0.01 * lmax)?
            .map(|x| x.clamp(0., lmax));
        let c = self.scaled_component(1., c)?.map(|x| x.max(0.));
        let h = self.angle()?;
        let alpha = self.alpha(Mode::Modern)?;
        self.ws();
        if !self.ch(b')') {
            return Err(ParseError::ExpectedClosingParenthesis);
        }
        Ok(color_from_components([l, c, h, alpha], tag))
    }

    fn hsl(&mut self) -> Result<DynamicColor, ParseError> {
        if !self.raw_ch(b'(') {
            return Err(ParseError::ExpectedArguments);
        }
        let h = self.angle()?;
        let comma = self.ch(b',');
        let mode = if comma { Mode::Legacy } else { Mode::Modern };
        let s = self.scaled_component(1., 1.)?.map(|x| x.max(0.));
        self.optional_comma(comma)?;
        let l = self.scaled_component(1., 1.)?;
        let alpha = self.alpha(mode)?;
        self.ws();
        if !self.ch(b')') {
            return Err(ParseError::ExpectedClosingParenthesis);
        }
        Ok(color_from_components([h, s, l, alpha], ColorSpaceTag::Hsl))
    }

    fn hwb(&mut self) -> Result<DynamicColor, ParseError> {
        if !self.raw_ch(b'(') {
            return Err(ParseError::ExpectedArguments);
        }
        let h = self.angle()?;
        let w = self.scaled_component(1., 1.)?;
        let b = self.scaled_component(1., 1.)?;
        let alpha = self.alpha(Mode::Modern)?;
        self.ws();
        if !self.ch(b')') {
            return Err(ParseError::ExpectedClosingParenthesis);
        }
        Ok(color_from_components([h, w, b, alpha], ColorSpaceTag::Hwb))
    }

    fn color(&mut self) -> Result<DynamicColor, ParseError> {
        if !self.raw_ch(b'(') {
            return Err(ParseError::ExpectedArguments);
        }
        self.ws();
        let Some(id) = self.ident() else {
            return Err(ParseError::ExpectedColorSpaceIdentifier);
        };
        let mut buf = [0; LOWERCASE_BUF_SIZE];
        let id_lc = make_lowercase(id, &mut buf);
        let cs = match id_lc {
            "srgb" => ColorSpaceTag::Srgb,
            "srgb-linear" => ColorSpaceTag::LinearSrgb,
            "display-p3" => ColorSpaceTag::DisplayP3,
            "a98-rgb" => ColorSpaceTag::A98Rgb,
            "prophoto-rgb" => ColorSpaceTag::ProphotoRgb,
            "rec2020" => ColorSpaceTag::Rec2020,
            "xyz-d50" => ColorSpaceTag::XyzD50,
            "xyz" | "xyz-d65" => ColorSpaceTag::XyzD65,
            _ => return Err(ParseError::UnknownColorSpace),
        };
        let r = self.scaled_component(1., 0.01)?;
        let g = self.scaled_component(1., 0.01)?;
        let b = self.scaled_component(1., 0.01)?;
        let alpha = self.alpha(Mode::Modern)?;
        self.ws();
        if !self.ch(b')') {
            return Err(ParseError::ExpectedClosingParenthesis);
        }
        Ok(color_from_components([r, g, b, alpha], cs))
    }
}

/// Parse a color string prefix in CSS syntax into a color.
///
/// Returns the byte offset of the unparsed remainder of the string and the parsed color. See also
/// [`parse_color`].
///
/// # Errors
///
/// Tries to return a suitable error for any invalid string, but may be
/// a little lax on some details.
pub fn parse_color_prefix(s: &str) -> Result<(usize, DynamicColor), ParseError> {
    #[inline]
    fn set_from_named_color_space(mut color: DynamicColor) -> DynamicColor {
        color.flags.set_named_color_space();
        color
    }

    if let Some(stripped) = s.strip_prefix('#') {
        let (ix, channels) = get_4bit_hex_channels(stripped)?;
        let color = color_from_4bit_hex(channels);
        // Hex colors are seen as if they are generated from the named `rgb()` color space
        // function.
        let mut color = DynamicColor::from_alpha_color(color);
        color.flags.set_named_color_space();
        return Ok((ix + 1, color));
    }
    let mut parser = Parser::new(s);
    if let Some(id) = parser.ident() {
        let mut buf = [0; LOWERCASE_BUF_SIZE];
        let id_lc = make_lowercase(id, &mut buf);
        let color = match id_lc {
            "rgb" | "rgba" => parser.rgb().map(set_from_named_color_space),
            "lab" => parser
                .lab(100.0, 1.25, ColorSpaceTag::Lab)
                .map(set_from_named_color_space),
            "lch" => parser
                .lch(100.0, 1.25, ColorSpaceTag::Lch)
                .map(set_from_named_color_space),
            "oklab" => parser
                .lab(1.0, 0.004, ColorSpaceTag::Oklab)
                .map(set_from_named_color_space),
            "oklch" => parser
                .lch(1.0, 0.004, ColorSpaceTag::Oklch)
                .map(set_from_named_color_space),
            "hsl" | "hsla" => parser.hsl().map(set_from_named_color_space),
            "hwb" => parser.hwb().map(set_from_named_color_space),
            "color" => parser.color(),
            _ => {
                if let Some(ix) = crate::x11_colors::lookup_palette_index(id_lc) {
                    let [r, g, b, a] = crate::x11_colors::COLORS[ix];
                    let mut color =
                        DynamicColor::from_alpha_color(AlphaColor::from_rgba8(r, g, b, a));
                    color.flags.set_named_color(ix);
                    Ok(color)
                } else {
                    Err(ParseError::UnknownColorIdentifier)
                }
            }
        }?;

        Ok((parser.ix, color))
    } else {
        Err(ParseError::UnknownColorSyntax)
    }
}

/// Parse a color string in CSS syntax into a color.
///
/// This parses the entire string; trailing characters cause an
/// [`ExpectedEndOfString`](ParseError::ExpectedEndOfString) parse error. Leading and trailing
/// whitespace are ignored. See also [`parse_color_prefix`].
///
/// # Errors
///
/// Tries to return a suitable error for any invalid string, but may be
/// a little lax on some details.
pub fn parse_color(s: &str) -> Result<DynamicColor, ParseError> {
    let s = s.trim();
    let (ix, color) = parse_color_prefix(s)?;

    if ix == s.len() {
        Ok(color)
    } else {
        Err(ParseError::ExpectedEndOfString)
    }
}

impl FromStr for DynamicColor {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_color(s)
    }
}

impl<CS: ColorSpace> FromStr for AlphaColor<CS> {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_color(s).map(DynamicColor::to_alpha_color)
    }
}

impl<CS: ColorSpace> FromStr for OpaqueColor<CS> {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_color(s)
            .map(DynamicColor::to_alpha_color)
            .map(AlphaColor::discard_alpha)
    }
}

impl<CS: ColorSpace> FromStr for PremulColor<CS> {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_color(s)
            .map(DynamicColor::to_alpha_color)
            .map(AlphaColor::premultiply)
    }
}

/// Parse 4-bit color channels from a hex-encoded string.
///
/// Returns the parsed channels and the byte offset to the remainder of the string (i.e., the
/// number of hex characters parsed).
const fn get_4bit_hex_channels(hex_str: &str) -> Result<(usize, [u8; 8]), ParseError> {
    let mut hex = [0; 8];

    let mut i = 0;
    while i < 8 && i < hex_str.len() {
        if let Ok(h) = hex_from_ascii_byte(hex_str.as_bytes()[i]) {
            hex[i] = h;
            i += 1;
        } else {
            break;
        }
    }

    let four_bit_channels = match i {
        3 => [hex[0], hex[0], hex[1], hex[1], hex[2], hex[2], 15, 15],
        4 => [
            hex[0], hex[0], hex[1], hex[1], hex[2], hex[2], hex[3], hex[3],
        ],
        6 => [hex[0], hex[1], hex[2], hex[3], hex[4], hex[5], 15, 15],
        8 => hex,
        _ => return Err(ParseError::WrongNumberOfHexDigits),
    };

    Ok((i, four_bit_channels))
}

const fn hex_from_ascii_byte(b: u8) -> Result<u8, ()> {
    match b {
        b'0'..=b'9' => Ok(b - b'0'),
        b'A'..=b'F' => Ok(b - b'A' + 10),
        b'a'..=b'f' => Ok(b - b'a' + 10),
        _ => Err(()),
    }
}

const fn color_from_4bit_hex(components: [u8; 8]) -> AlphaColor<Srgb> {
    let [r0, r1, g0, g1, b0, b1, a0, a1] = components;
    AlphaColor::from_rgba8(
        (r0 << 4) | r1,
        (g0 << 4) | g1,
        (b0 << 4) | b1,
        (a0 << 4) | a1,
    )
}

impl FromStr for ColorSpaceTag {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut buf = [0; LOWERCASE_BUF_SIZE];
        match make_lowercase(s, &mut buf) {
            "srgb" => Ok(Self::Srgb),
            "srgb-linear" => Ok(Self::LinearSrgb),
            "lab" => Ok(Self::Lab),
            "lch" => Ok(Self::Lch),
            "oklab" => Ok(Self::Oklab),
            "oklch" => Ok(Self::Oklch),
            "display-p3" => Ok(Self::DisplayP3),
            "a98-rgb" => Ok(Self::A98Rgb),
            "prophoto-rgb" => Ok(Self::ProphotoRgb),
            "xyz-d50" => Ok(Self::XyzD50),
            "xyz" | "xyz-d65" => Ok(Self::XyzD65),
            _ => Err(ParseError::UnknownColorSpace),
        }
    }
}

const LOWERCASE_BUF_SIZE: usize = 32;

/// If the string contains any uppercase characters, make a lowercase copy
/// in the provided buffer space.
///
/// If anything goes wrong (including the buffer size being exceeded), return
/// the original string.
fn make_lowercase<'a>(s: &'a str, buf: &'a mut [u8; LOWERCASE_BUF_SIZE]) -> &'a str {
    let len = s.len();
    if len <= LOWERCASE_BUF_SIZE && s.as_bytes().iter().any(|c| c.is_ascii_uppercase()) {
        buf[..len].copy_from_slice(s.as_bytes());
        if let Ok(s_copy) = str::from_utf8_mut(&mut buf[..len]) {
            s_copy.make_ascii_lowercase();
            s_copy
        } else {
            s
        }
    } else {
        s
    }
}

#[cfg(test)]
mod tests {
    use crate::DynamicColor;

    use super::{parse_color, parse_color_prefix, Mode, ParseError, Parser};

    fn assert_close_color(c1: DynamicColor, c2: DynamicColor) {
        const EPSILON: f32 = 1e-4;
        assert_eq!(c1.cs, c2.cs);
        for i in 0..4 {
            assert!((c1.components[i] - c2.components[i]).abs() < EPSILON);
        }
    }

    fn assert_err(c: &str, err: ParseError) {
        assert_eq!(parse_color(c).unwrap_err(), err);
    }

    #[test]
    fn x11_color_names() {
        let red = parse_color("red").unwrap();
        assert_close_color(red, parse_color("rgb(255, 0, 0)").unwrap());
        assert_close_color(red, parse_color("\n rgb(255, 0, 0)\t ").unwrap());
        let lgy = parse_color("lightgoldenrodyellow").unwrap();
        assert_close_color(lgy, parse_color("rgb(250, 250, 210)").unwrap());
        let transparent = parse_color("transparent").unwrap();
        assert_close_color(transparent, parse_color("rgba(0, 0, 0, 0)").unwrap());
    }

    #[test]
    fn hex() {
        let red = parse_color("red").unwrap();
        assert_close_color(red, parse_color("#f00").unwrap());
        assert_close_color(red, parse_color("#f00f").unwrap());
        assert_close_color(red, parse_color("#ff0000ff").unwrap());
        assert_eq!(
            parse_color("#f00fa").unwrap_err(),
            ParseError::WrongNumberOfHexDigits
        );
    }

    #[test]
    fn consume_string() {
        assert_eq!(
            parse_color("#ff0000ffa").unwrap_err(),
            ParseError::ExpectedEndOfString
        );
        assert_eq!(
            parse_color("rgba(255, 100, 0, 1)a").unwrap_err(),
            ParseError::ExpectedEndOfString
        );
    }

    #[test]
    fn prefix() {
        for (color, trailing) in [
            ("color(rec2020 0.2 0.3 0.4 / 0.85)trailing", "trailing"),
            ("color(rec2020 0.2 0.3 0.4 / 0.85) ", " "),
            ("color(rec2020 0.2 0.3 0.4 / 0.85)", ""),
            ("red\0", "\0"),
            ("#ffftrailing", "trailing"),
            ("#fffffftr", "tr"),
        ] {
            assert_eq!(&color[parse_color_prefix(color).unwrap().0..], trailing);
        }
    }

    #[test]
    fn consume_comments() {
        for (s, remaining) in [
            ("/* abc */ def", " def"),
            ("/* *//* */abc", "abc"),
            ("/* /* */abc", "abc"),
        ] {
            let mut parser = Parser::new(s);
            assert!(parser.consume_comments().is_ok());
            assert_eq!(&parser.s[parser.ix..], remaining);
        }
    }

    #[test]
    fn alpha() {
        for (alpha, expected, mode) in [
            (", 10%", Ok(Some(0.1)), Mode::Legacy),
            ("/ 0.25", Ok(Some(0.25)), Mode::Modern),
            ("/ -0.3", Ok(Some(0.)), Mode::Modern),
            ("/ 110%", Ok(Some(1.)), Mode::Modern),
            ("", Ok(Some(1.)), Mode::Legacy),
            ("/ none", Ok(None), Mode::Modern),
        ] {
            let mut parser = Parser::new(alpha);
            let result = parser.alpha(mode);
            assert_eq!(result, expected,
                "Failed parsing specified alpha `{alpha}`. Expected: `{expected:?}`. Got: `{result:?}`.");
        }
    }

    #[test]
    fn angles() {
        for (angle, expected) in [
            ("90deg", 90.),
            ("1.5707963rad", 90.),
            ("100grad", 90.),
            ("0.25turn", 90.),
        ] {
            let mut parser = Parser::new(angle);
            let result = parser.angle().unwrap().unwrap();
            assert!((result - expected).abs() < 1e-4,
                    "Failed parsing specified angle `{angle}`. Expected: `{expected:?}`. Got: `{result:?}`.");
        }

        {
            let mut parser = Parser::new("none");
            assert_eq!(parser.angle().unwrap(), None);
        }

        assert_err(
            "hwb(1turns 20% 30% / 50%)",
            ParseError::UnknownAngleDimension,
        );
    }

    #[test]
    fn case_insensitive() {
        for (c1, c2) in [
            ("red", "ReD"),
            ("lightgoldenrodyellow", "LightGoldenRodYellow"),
            ("rgb(102, 51, 153)", "RGB(102, 51, 153)"),
            (
                "color(rec2020 0.2 0.3 0.4 / 0.85)",
                "CoLoR(ReC2020 0.2 0.3 0.4 / 0.85)",
            ),
            ("hwb(120deg 30% 50%)", "HwB(120DeG 30% 50%)"),
            ("hsl(none none none)", "HSL(NONE NONE NONE)"),
        ] {
            assert_close_color(parse_color(c1).unwrap(), parse_color(c2).unwrap());
        }
    }
}