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
/// An sRGB color.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Srgb {
    /// Red (0 to 1).
    pub r: f32,
    /// Green (0 to 1).
    pub g: f32,
    /// Blue (0 to 1).
    pub b: f32,
}

impl From<crate::LinearRgb> for Srgb {
    fn from(linear: crate::LinearRgb) -> Self {
        let from_linear = |n: f32| {
            if n <= 0.0031308 {
                n * 12.92
            } else {
                n.powf(1.0 / 2.4) * 1.055 - 0.055
            }
        };

        Self {
            r: from_linear(linear.r),
            g: from_linear(linear.g),
            b: from_linear(linear.b),
        }
    }
}

impl crate::ColorSpace for Srgb {
    const BLACK: Self = Self {
        r: 0.0,
        g: 0.0,
        b: 0.0,
    };

    const WHITE: Self = Self {
        r: 1.0,
        g: 1.0,
        b: 1.0,
    };

    fn in_bounds(self) -> bool {
        crate::approx_in_range(self.r, 0.0..1.0)
            && crate::approx_in_range(self.g, 0.0..1.0)
            && crate::approx_in_range(self.b, 0.0..1.0)
    }
}

impl crate::Hex for Srgb {
    fn components(self) -> (f32, f32, f32) {
        (self.r, self.g, self.b)
    }
}

#[cfg(test)]
#[test]
fn hex() {
    use crate::Hex;

    let rgb = Srgb {
        r: 1.0,
        g: 0.25,
        b: 1.0,
    };

    assert_eq!(rgb.hex(), 0xff40ff);
}