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
use super::Color;

use ::rgb::{RGB16, RGB8};
type RGBf32 = ::rgb::RGB<f32>;

impl From<RGB16> for Color {
    fn from(value: RGB16) -> Self {
        Self::from_rgb16(value.into())
    }
}

impl From<Color> for RGB16 {
    fn from(value: Color) -> Self {
        value.as_rgb16().into()
    }
}

impl From<RGB8> for Color {
    fn from(value: RGB8) -> Self {
        Self::from_rgb8(value.into())
    }
}

impl From<Color> for RGB8 {
    fn from(value: Color) -> Self {
        value.as_rgb8().into()
    }
}

impl From<RGBf32> for Color {
    fn from(value: RGBf32) -> Self {
        let (r, g, b) = value.into();
        (r, g, b).into()
    }
}

impl From<Color> for RGBf32 {
    fn from(value: Color) -> Self {
        let (r, g, b) = value.into();
        (r, g, b).into()
    }
}

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

    use super::*;

    #[test]
    fn from_rgb16() {
        let rgb = RGB16::new(0x3333, 0x6666, 0x9999);
        let color = Color::from(rgb);

        assert_approx_eq!(color.0[0], 0.2);
        assert_approx_eq!(color.0[1], 0.4);
        assert_approx_eq!(color.0[2], 0.6);
    }

    #[test]
    fn into_rgb16() {
        let color = Color::new(0.2, 0.4, 0.6);
        let rgb: RGB16 = color.into();

        assert_eq!(rgb.r, 0x3333);
        assert_eq!(rgb.g, 0x6666);
        assert_eq!(rgb.b, 0x9999);
    }

    #[test]
    fn from_rgb8() {
        let rgb = RGB8::new(0x33, 0x66, 0x99);
        let color = Color::from(rgb);

        assert_approx_eq!(color.0[0], 0.2);
        assert_approx_eq!(color.0[1], 0.4);
        assert_approx_eq!(color.0[2], 0.6);
    }

    #[test]
    fn into_rgb8() {
        let color = Color::new(0.2, 0.4, 0.6);
        let rgb: RGB8 = color.into();

        assert_eq!(rgb.r, 0x33);
        assert_eq!(rgb.g, 0x66);
        assert_eq!(rgb.b, 0x99);
    }

    #[test]
    fn from_rgbf32() {
        let rgb = RGBf32::new(0.2, 0.4, 0.6);
        let color = Color::from(rgb);

        assert_eq!(color.0[0], 0.2);
        assert_eq!(color.0[1], 0.4);
        assert_eq!(color.0[2], 0.6);
    }

    #[test]
    fn into_rgbf32() {
        let color = Color::new(0.2, 0.4, 0.6);
        let rgb: RGBf32 = color.into();

        assert_eq!(rgb.r, 0.2);
        assert_eq!(rgb.g, 0.4);
        assert_eq!(rgb.b, 0.6);
    }
}