keyset_color/
rgb.rs

1use super::Color;
2
3use ::rgb::{RGB16, RGB8};
4type RGBf32 = ::rgb::RGB<f32>;
5
6impl From<RGB16> for Color {
7    fn from(value: RGB16) -> Self {
8        Self::from_rgb16(value.into())
9    }
10}
11
12impl From<Color> for RGB16 {
13    fn from(value: Color) -> Self {
14        value.as_rgb16().into()
15    }
16}
17
18impl From<RGB8> for Color {
19    fn from(value: RGB8) -> Self {
20        Self::from_rgb8(value.into())
21    }
22}
23
24impl From<Color> for RGB8 {
25    fn from(value: Color) -> Self {
26        value.as_rgb8().into()
27    }
28}
29
30impl From<RGBf32> for Color {
31    fn from(value: RGBf32) -> Self {
32        let (r, g, b) = value.into();
33        (r, g, b).into()
34    }
35}
36
37impl From<Color> for RGBf32 {
38    fn from(value: Color) -> Self {
39        let (r, g, b) = value.into();
40        (r, g, b).into()
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use assert_approx_eq::assert_approx_eq;
47
48    use super::*;
49
50    #[test]
51    fn from_rgb16() {
52        let rgb = RGB16::new(0x3333, 0x6666, 0x9999);
53        let color = Color::from(rgb);
54
55        assert_approx_eq!(color.0[0], 0.2);
56        assert_approx_eq!(color.0[1], 0.4);
57        assert_approx_eq!(color.0[2], 0.6);
58    }
59
60    #[test]
61    fn into_rgb16() {
62        let color = Color::new(0.2, 0.4, 0.6);
63        let rgb: RGB16 = color.into();
64
65        assert_eq!(rgb.r, 0x3333);
66        assert_eq!(rgb.g, 0x6666);
67        assert_eq!(rgb.b, 0x9999);
68    }
69
70    #[test]
71    fn from_rgb8() {
72        let rgb = RGB8::new(0x33, 0x66, 0x99);
73        let color = Color::from(rgb);
74
75        assert_approx_eq!(color.0[0], 0.2);
76        assert_approx_eq!(color.0[1], 0.4);
77        assert_approx_eq!(color.0[2], 0.6);
78    }
79
80    #[test]
81    fn into_rgb8() {
82        let color = Color::new(0.2, 0.4, 0.6);
83        let rgb: RGB8 = color.into();
84
85        assert_eq!(rgb.r, 0x33);
86        assert_eq!(rgb.g, 0x66);
87        assert_eq!(rgb.b, 0x99);
88    }
89
90    #[test]
91    fn from_rgbf32() {
92        let rgb = RGBf32::new(0.2, 0.4, 0.6);
93        let color = Color::from(rgb);
94
95        assert_eq!(color.0[0], 0.2);
96        assert_eq!(color.0[1], 0.4);
97        assert_eq!(color.0[2], 0.6);
98    }
99
100    #[test]
101    fn into_rgbf32() {
102        let color = Color::new(0.2, 0.4, 0.6);
103        let rgb: RGBf32 = color.into();
104
105        assert_eq!(rgb.r, 0.2);
106        assert_eq!(rgb.g, 0.4);
107        assert_eq!(rgb.b, 0.6);
108    }
109}