auto-palette 0.8.1

🎨 A Rust library that extracts prominent color palettes from images automatically.
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
mod ansi16;
mod ansi256;
mod cmyk;
mod error;
mod hsl;
mod hsv;
mod hue;
mod lab;
mod lchab;
mod lchuv;
mod luv;
mod oklab;
mod oklch;
mod rgb;
mod white_point;
mod xyz;

use std::{
    fmt,
    fmt::{Display, Formatter},
    marker::PhantomData,
    str::FromStr,
};

pub use ansi16::Ansi16;
pub use ansi256::Ansi256;
pub use cmyk::CMYK;
pub use hsl::HSL;
pub use hsv::HSV;
pub use hue::Hue;
pub(crate) use lab::xyz_to_lab;
pub use lab::Lab;
pub use lchab::LCHab;
pub use lchuv::LCHuv;
pub use luv::Luv;
pub use oklab::Oklab;
pub use oklch::Oklch;
pub use rgb::RGB;
pub use white_point::*;
pub(crate) use xyz::rgb_to_xyz;
pub use xyz::XYZ;

use crate::{color::error::ColorError, math::FloatNumber};

/// The color representation.
///
/// # Type Parameters
/// * `T` - The floating point type.
/// * `W` - The white point type.
///
/// # Examples
/// ```
/// use std::str::FromStr;
///
/// use auto_palette::color::Color;
///
/// let color: Color<f32> = Color::from_str("#2c7de7").unwrap();
/// assert!(color.is_light());
/// assert!(color.lightness() - 52.917 < 1e-3);
/// assert!(color.chroma() - 61.981 < 1e-3);
/// assert!(color.hue().to_degrees() - 282.662 < 1e-3);
///
/// let rgb = color.to_rgb();
/// assert_eq!(format!("{}", rgb), "RGB(44, 125, 231)");
///
/// let hsl = color.to_hsl();
/// assert_eq!(format!("{}", hsl), "HSL(214.01, 0.80, 0.54)");
///
/// let lab = color.to_lab();
/// assert_eq!(format!("{}", lab), "Lab(52.92, 13.59, -60.47)");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Color<T, W = D65>
where
    T: FloatNumber,
{
    pub(super) l: T,
    pub(super) a: T,
    pub(super) b: T,
    _marker: PhantomData<W>,
}

impl<T, W> Color<T, W>
where
    T: FloatNumber,
    W: WhitePoint,
{
    /// Creates a new `Color` instance.
    ///
    /// # Arguments
    /// * `l` - The value of l.
    /// * `a` - The value of a.
    /// * `b` - The value of b.
    ///
    /// # Returns
    /// A new `Color` instance.
    #[must_use]
    pub(crate) fn new(l: T, a: T, b: T) -> Self {
        Self {
            l,
            a,
            b,
            _marker: PhantomData,
        }
    }

    /// Returns whether this color is light.
    ///
    /// # Returns
    /// `true` if the color is light, otherwise `false`.
    #[must_use]
    pub fn is_light(&self) -> bool {
        self.l > T::from_f32(50.0)
    }

    /// Returns whether this color is dark.
    ///
    /// # Returns
    /// `true` if the color is dark, otherwise `false`.
    #[must_use]
    pub fn is_dark(&self) -> bool {
        !self.is_light()
    }

    /// Returns the lightness of this color.
    ///
    /// # Returns
    /// The lightness of this color.
    #[must_use]
    pub fn lightness(&self) -> T {
        self.l
    }

    /// Returns the chroma of this color.
    ///
    /// # Returns
    /// The chroma of this color.
    #[must_use]
    pub fn chroma(&self) -> T {
        (self.a.powi(2) + self.b.powi(2)).sqrt()
    }

    /// Returns the hue of this color. The hue is the angle of the vector in the a*b* plane.
    ///
    /// # Returns
    /// The hue of this color.
    #[must_use]
    pub fn hue(&self) -> Hue<T> {
        let degrees = self.b.atan2(self.a).to_degrees();
        Hue::from_degrees(degrees)
    }

    /// Converts this color to a hexadecimal string.
    ///
    /// # Returns
    /// The hexadecimal string representation of this color.
    #[must_use]
    pub fn to_hex_string(&self) -> String {
        let RGB { r, g, b } = self.to_rgb();
        format!("#{:02X}{:02X}{:02X}", r, g, b)
    }

    /// Converts this color to the RGB color space.
    ///
    /// # Returns
    /// The converted `RGB` color.
    #[must_use]
    pub fn to_rgb(&self) -> RGB {
        RGB::from(&self.to_xyz())
    }

    /// Converts this color to the CMYK color space.
    ///
    /// # Returns
    /// The converted `CMYK` color.
    #[must_use]
    pub fn to_cmyk(&self) -> CMYK<T> {
        CMYK::from(&self.to_rgb())
    }

    /// Converts this color to the HSL color space.
    ///
    /// # Returns
    /// The converted `HSL` color.
    #[must_use]
    pub fn to_hsl(&self) -> HSL<T> {
        HSL::from(&self.to_rgb())
    }

    /// Converts this color to the HSV color space.
    ///
    /// # Returns
    /// The converted `HSV` color.
    #[must_use]
    pub fn to_hsv(&self) -> HSV<T> {
        HSV::from(&self.to_rgb())
    }

    /// Converts this color to the CIE XYZ color space.
    ///
    /// # Returns
    /// The converted `XYZ` color.
    #[must_use]
    pub fn to_xyz(&self) -> XYZ<T> {
        XYZ::from(&self.to_lab())
    }

    /// Converts this color to the CIE L*u*v* color space.
    ///
    /// # Returns
    /// The converted `Luv` color.
    #[must_use]
    pub fn to_luv(&self) -> Luv<T, W> {
        Luv::<T, W>::from(&self.to_xyz())
    }

    /// Converts this color to the CIE LCH(uv) color space.
    ///
    /// # Returns
    /// The converted `LCHuv` color.
    #[must_use]
    pub fn to_lchuv(&self) -> LCHuv<T, W> {
        LCHuv::<T, W>::from(&self.to_luv())
    }

    /// Converts this color to the CIE L*a*b* color space.
    ///
    /// # Returns
    /// The converted `Lab` color.
    #[must_use]
    pub fn to_lab(&self) -> Lab<T, W> {
        Lab::<T, W>::new(self.l, self.a, self.b)
    }

    /// Converts this color to the CIE LCH(ab) color space.
    ///
    /// # Returns
    /// The converted `LCHab` color.
    #[must_use]
    pub fn to_lchab(&self) -> LCHab<T, W> {
        LCHab::<T, W>::from(&self.to_lab())
    }

    /// Converts this color to the CIE Oklab color space.
    ///
    /// # Returns
    /// The converted `Oklab` color.
    #[must_use]
    pub fn to_oklab(&self) -> Oklab<T> {
        Oklab::from(&self.to_xyz())
    }

    /// Converts this color to the CIE Oklch color space.
    ///
    /// # Returns
    /// The converted `Oklch` color.
    #[must_use]
    pub fn to_oklch(&self) -> Oklch<T> {
        Oklch::from(&self.to_oklab())
    }

    /// Converts this color to the 4-bit ANSI 16 color space.
    ///
    /// # Returns
    /// The converted `Ansi16` color.
    #[must_use]
    pub fn to_ansi16(&self) -> Ansi16 {
        Ansi16::from(&self.to_rgb())
    }

    /// Converts this color to the 8-bit ANSI 256 color space.
    ///
    /// # Returns
    /// The converted `Ansi256` color.
    #[must_use]
    pub fn to_ansi256(&self) -> Ansi256 {
        Ansi256::from(&self.to_rgb())
    }

    /// Converts this color to a 32-bit integer representation in RGB.
    ///
    /// # Returns
    /// The converted RGB integer representation.
    #[must_use]
    pub fn to_rgb_int(&self) -> u32 {
        let rgb = self.to_rgb();
        let r = rgb.r as u32;
        let g = rgb.g as u32;
        let b = rgb.b as u32;
        (r << 16) | (g << 8) | b
    }

    /// Converts this color to a 32-bit integer representation in RGBA.
    ///
    /// # Arguments
    /// * `alpha` - The alpha value (0-255).
    ///
    /// # Returns
    /// The converted RGBA integer representation.
    #[must_use]
    pub fn to_rgba_int(&self, alpha: u8) -> u32 {
        let rgb = self.to_rgb();
        let r = rgb.r as u32;
        let g = rgb.g as u32;
        let b = rgb.b as u32;
        (r << 24) | (g << 16) | (b << 8) | alpha as u32
    }
}

impl<T> Display for Color<T>
where
    T: FloatNumber,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Color(l: {:.2}, a: {:.2}, b: {:.2})",
            self.l, self.a, self.b
        )
    }
}

impl<T> From<u32> for Color<T>
where
    T: FloatNumber,
{
    fn from(value: u32) -> Self {
        let r = (value >> 16) & 0xFF;
        let g = (value >> 8) & 0xFF;
        let b = value & 0xFF;
        let (x, y, z) = rgb_to_xyz::<T>(r as u8, g as u8, b as u8);
        let (l, a, b) = xyz_to_lab::<T, D65>(x, y, z);
        Self::new(l, a, b)
    }
}

impl<T> FromStr for Color<T>
where
    T: FloatNumber,
{
    type Err = ColorError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if !s.starts_with("#") {
            return Err(ColorError::InvalidHexValue(s.to_string()));
        }

        let (r, g, b) = match s.len() {
            // Handle 3-digit hex color #RGB
            4 => {
                let r = u8::from_str_radix(&s[1..2].repeat(2), 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                let g = u8::from_str_radix(&s[2..3].repeat(2), 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                let b = u8::from_str_radix(&s[3..4].repeat(2), 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                (r, g, b)
            }
            // Handle 4-digit hex color #RGBA
            5 => {
                let r = u8::from_str_radix(&s[1..2].repeat(2), 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                let g = u8::from_str_radix(&s[2..3].repeat(2), 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                let b = u8::from_str_radix(&s[3..4].repeat(2), 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                // Check whether the alpha value is valid
                let _ = u8::from_str_radix(&s[4..5].repeat(2), 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                (r, g, b)
            }
            // Handle 6-digit hex color #RRGGBB
            7 => {
                let r = u8::from_str_radix(&s[1..3], 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                let g = u8::from_str_radix(&s[3..5], 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                let b = u8::from_str_radix(&s[5..7], 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                (r, g, b)
            }
            // Handle 8-digit hex color #RRGGBBAA
            9 => {
                let r = u8::from_str_radix(&s[1..3], 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                let g = u8::from_str_radix(&s[3..5], 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                let b = u8::from_str_radix(&s[5..7], 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                // Check whether the alpha value is valid
                let _ = u8::from_str_radix(&s[7..9], 16)
                    .map_err(|_| ColorError::InvalidHexValue(s.to_string()))?;
                (r, g, b)
            }
            _ => return Err(ColorError::InvalidHexValue(s.to_string())),
        };

        let (x, y, z) = rgb_to_xyz::<T>(r, g, b);
        let (l, a, b) = xyz_to_lab::<T, D65>(x, y, z);
        Ok(Self::new(l, a, b))
    }
}

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

    use super::*;
    use crate::assert_approx_eq;

    #[test]
    fn test_new() {
        // Act
        let actual: Color<f32> = Color::new(80.0, 0.0, 0.0);

        // Assert
        assert_eq!(actual.l, 80.0);
        assert_eq!(actual.a, 0.0);
        assert_eq!(actual.b, 0.0);
    }

    #[rstest]
    #[case((0.0, 0.0, 0.0), false)]
    #[case((50.0, 0.0, 0.0), false)]
    #[case((50.1, 0.0, 0.0), true)]
    #[case((80.0, 0.0, 0.0), true)]
    fn test_color_is_light(#[case] input: (f32, f32, f32), #[case] expected: bool) {
        // Act
        let color: Color<f32> = Color::new(input.0, input.1, input.2);
        let actual = color.is_light();

        // Assert
        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case((0.0, 0.0, 0.0), true)]
    #[case((50.0, 0.0, 0.0), true)]
    #[case((50.1, 0.0, 0.0), false)]
    #[case((80.0, 0.0, 0.0), false)]
    fn test_color_is_dark(#[case] input: (f32, f32, f32), #[case] expected: bool) {
        // Act
        let color: Color<f32> = Color::new(input.0, input.1, input.2);
        let actual = color.is_dark();

        // Assert
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_lightness() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.lightness();

        // Assert
        assert_approx_eq!(actual, 91.1120);
    }

    #[test]
    fn test_chroma() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.chroma();

        // Assert
        assert_approx_eq!(actual, 50.120_117);
    }

    #[rstest]
    #[case::black((0.0, 0.0, 0.0), 0.0)]
    #[case::white((100.0, - 0.002_443, 0.011_384), 102.111_946)]
    #[case::red((53.237_144, 80.088_320, 67.199_460), 39.998_900)]
    #[case::green((87.735_535, - 86.183_550, 83.179_924), 136.016_020)]
    #[case::blue((32.300_800, 79.194_260, - 107.868_910), 306.28503)]
    #[case::cyan((91.114_750, - 48.080_950, - 14.142_858), 196.391_080,)]
    #[case::magenta((60.322_700, 98.235_580, - 60.842_370), 328.227_940,)]
    #[case::yellow((97.138_580, - 21.562_368, 94.476_760), 102.856_380)]
    fn test_hue(#[case] input: (f32, f32, f32), #[case] expected: f32) {
        // Act
        let color: Color<f32> = Color::new(input.0, input.1, input.2);
        let actual = color.hue();

        // Assert
        assert_approx_eq!(actual.to_degrees(), expected, 1e-3);
    }

    #[test]
    fn test_to_hex_string() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_hex_string();

        // Assert
        assert_eq!(actual, "#00FFFF");
    }

    #[test]
    fn test_to_rgb() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_rgb();

        // Assert
        assert_eq!(actual, RGB::new(0, 255, 255));
    }

    #[test]
    fn test_to_cmyk() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_cmyk();

        // Assert
        assert_eq!(actual, CMYK::new(1.0, 0.0, 0.0, 0.0));
    }

    #[test]
    fn test_to_hsl() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_hsl();

        // Assert
        assert_approx_eq!(actual.h.to_degrees(), 180.0, 1e-3);
        assert_approx_eq!(actual.s, 1.0);
        assert_approx_eq!(actual.l, 0.5);
    }

    #[test]
    fn test_to_hsv() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_hsv();

        // Assert
        assert_approx_eq!(actual.h.to_degrees(), 180.0, 1e-3);
        assert_approx_eq!(actual.s, 1.0);
        assert_approx_eq!(actual.v, 1.0);
    }

    #[test]
    fn test_to_xyz() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual: XYZ<f32> = color.to_xyz();

        // Assert
        assert_approx_eq!(actual.x, 0.5380, 1e-3);
        assert_approx_eq!(actual.y, 0.7873, 1e-3);
        assert_approx_eq!(actual.z, 1.0690, 1e-3);
    }

    #[test]
    fn test_to_luv() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_luv();

        // Assert
        assert_approx_eq!(actual.l, 91.1120);
        assert_approx_eq!(actual.u, -70.480, 1e-3);
        assert_approx_eq!(actual.v, -15.240, 1e-3);
    }

    #[test]
    fn test_to_lchuv() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_lchuv();

        // Assert
        assert_approx_eq!(actual.l, 91.1120);
        assert_approx_eq!(actual.c, 72.109, 1e-3);
        assert_approx_eq!(actual.h.to_degrees(), 192.202, 1e-3);
    }

    #[test]
    fn test_to_lab() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_lab();

        // Assert
        assert_approx_eq!(actual.l, 91.1120);
        assert_approx_eq!(actual.a, -48.0806);
        assert_approx_eq!(actual.b, -14.1521);
    }

    #[test]
    fn test_to_oklab() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_oklab();

        // Assert
        assert_approx_eq!(actual.l, 0.905, 1e-3);
        assert_approx_eq!(actual.a, -0.149, 1e-3);
        assert_approx_eq!(actual.b, -0.040, 1e-3);
    }

    #[test]
    fn test_to_oklch() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_oklch();

        // Assert
        assert_approx_eq!(actual.l, 0.905, 1e-3);
        assert_approx_eq!(actual.c, 0.155, 1e-3);
        assert_approx_eq!(actual.h.to_degrees(), 194.82, 1e-3);
    }

    #[test]
    fn test_to_lchab() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_lchab();

        // Assert
        assert_approx_eq!(actual.l, 91.1120, 1e-3);
        assert_approx_eq!(actual.c, 50.120, 1e-3);
        assert_approx_eq!(actual.h.to_degrees(), 196.401, 1e-3);
    }

    #[test]
    fn test_to_anis16() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_ansi16();

        // Assert
        assert_eq!(actual, Ansi16::bright_cyan());
    }

    #[test]
    fn test_to_ansi256() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_ansi256();

        // Assert
        assert_eq!(actual, Ansi256::new(51));
    }

    #[test]
    fn test_to_rgb_int() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_rgb_int();

        // Assert
        assert_eq!(actual, 0x00ffff);
    }

    #[test]
    fn test_to_rgba_int() {
        // Act
        let color: Color<f32> = Color::new(91.1120, -48.0806, -14.1521);
        let actual = color.to_rgba_int(128);

        // Assert
        assert_eq!(actual, 0x00ffff80);
    }

    #[test]
    fn test_fmt() {
        // Act & Assert
        let color: Color<f32> = Color::new(91.114_750, -48.080_950, -14.142_8581);
        assert_eq!(
            format!("{}", color),
            "Color(l: 91.11, a: -48.08, b: -14.14)"
        );
    }

    #[test]
    fn test_from_u32() {
        // Act
        let actual: Color<f32> = Color::from(0xff0080);

        // Assert
        assert_approx_eq!(actual.l, 54.8888, 1e-3);
        assert_approx_eq!(actual.a, 84.5321, 1e-3);
        assert_approx_eq!(actual.b, 4.0656, 1e-3);
    }

    #[rstest]
    #[case::black_rgb("#000", 0.0, 0.0, 0.0)]
    #[case::white_rgb("#fff", 100.0, - 0.002_443, 0.011_384)]
    #[case::red_rgb("#f00", 53.237_144, 80.088_320, 67.199_460)]
    #[case::green_rgb("#0f0", 87.735_535, - 86.183_550, 83.179_924)]
    #[case::blue_rgb("#00f", 32.300_800, 79.194_260, - 107.868_910)]
    #[case::cyan_rgb("#0ff", 91.114_750, - 48.080_950, - 14.142_858)]
    #[case::magenta_rgb("#f0f", 60.322_700, 98.235_580, - 60.842_370)]
    #[case::yellow_rgb("#ff0", 97.138_580, - 21.562_368, 94.476_760)]
    #[case::black_rgba("#0000", 0.0, 0.0, 0.0)]
    #[case::white_rgba("#ffff", 100.0, - 0.002_443, 0.011_384)]
    #[case::red_rgba("#f00f", 53.237_144, 80.088_320, 67.199_460)]
    #[case::green_rgba("#0f0f", 87.735_535, - 86.183_550, 83.179_924)]
    #[case::blue_rgba("#00ff", 32.300_800, 79.194_260, - 107.868_910)]
    #[case::cyan_rgba("#0fff", 91.114_750, - 48.080_950, - 14.142_858)]
    #[case::magenta_rgba("#f0ff", 60.322_700, 98.235_580, - 60.842_370)]
    #[case::yellow_rgba("#ff0f", 97.138_580, - 21.562_368, 94.476_760)]
    #[case::black_rrggbb("#000000", 0.0, 0.0, 0.0)]
    #[case::white_rrggbb("#ffffff", 100.0, - 0.002_443, 0.011_384)]
    #[case::red_rrggbb("#ff0000", 53.237_144, 80.088_320, 67.199_460)]
    #[case::green_rrggbb("#00ff00", 87.735_535, - 86.183_550, 83.179_924)]
    #[case::blue_rrggbb("#0000ff", 32.300_800, 79.194_260, - 107.868_910)]
    #[case::cyan_rrggbb("#00ffff", 91.114_750, - 48.080_950, - 14.142_858)]
    #[case::magenta_rrggbb("#ff00ff", 60.322_700, 98.235_580, - 60.842_370)]
    #[case::yellow_rrggbb("#ffff00", 97.138_580, - 21.562_368, 94.476_760)]
    #[case::black_rrggbbaa("#000000ff", 0.0, 0.0, 0.0)]
    #[case::white_rrggbbaa("#ffffffff", 100.0, - 0.002_443, 0.011_384)]
    #[case::red_rrggbbaa("#ff0000ff", 53.237_144, 80.088_320, 67.199_460)]
    #[case::green_rrggbbaa("#00ff00ff", 87.735_535, - 86.183_550, 83.179_924)]
    #[case::blue_rrggbbaa("#0000ffff", 32.300_800, 79.194_260, - 107.868_910)]
    #[case::cyan_rrggbbaa("#00ffffff", 91.114_750, - 48.080_950, - 14.142_858)]
    #[case::magenta_rrggbbaa("#ff00ffff", 60.322_700, 98.235_580, - 60.842_370)]
    #[case::yellow_rrggbbaa("#ffff00ff", 97.138_580, - 21.562_368, 94.476_760)]
    fn test_from_str(#[case] input: &str, #[case] l: f32, #[case] a: f32, #[case] b: f32) {
        // Act
        let actual: Color<f32> = Color::from_str(input).unwrap();

        // Assert
        assert_approx_eq!(actual.l, l, 1e-3);
        assert_approx_eq!(actual.a, a, 1e-3);
        assert_approx_eq!(actual.b, b, 1e-3);
    }

    #[rstest]
    #[case::empty("")]
    #[case::invalid("123456")]
    #[case::invalid_length("#12345")]
    #[case::invalid_prefix("123456")]
    #[case::invalid_rgb_r("#g00")]
    #[case::invalid_rgb_g("#0g0")]
    #[case::invalid_rgb_b("#00g")]
    #[case::invalid_rrggbb_r("#0g0000")]
    #[case::invalid_rrggbb_g("#000g00")]
    #[case::invalid_rrggbb_b("#00000g")]
    #[case::invalid_rrggbbaa_r("#0g000000")]
    #[case::invalid_rrggbbaa_g("#000g0000")]
    #[case::invalid_rrggbbaa_b("#00000g00")]
    #[case::invalid_rrggbbaa_a("#0000000g")]
    fn test_from_str_error(#[case] input: &str) {
        // Act
        let actual = Color::<f32>::from_str(input);

        // Assert
        assert!(actual.is_err());
        assert_eq!(
            actual.unwrap_err(),
            ColorError::InvalidHexValue(input.to_string())
        );
    }
}