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
use super::{AsBytes, Rgba};

/// A pixel that is three bytes long and is made of a red, green and blue
/// channel, in that order.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct Rgb<T = u8>(pub T, pub T, pub T);

/// A pixel that is three bytes long and is made of a blue, green and red
/// channel, in that order.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct Bgr<T = u8>(pub T, pub T, pub T);

impl<T> From<Rgba<T>> for Rgb<T> {
    fn from(Rgba(r, g, b, _): Rgba<T>) -> Self {
        Rgb(r, g, b)
    }
}

impl<T> From<Rgba<T>> for Bgr<T> {
    fn from(Rgba(r, g, b, _): Rgba<T>) -> Self {
        Bgr(b, g, r)
    }
}

impl From<Rgb> for Rgba {
    fn from(Rgb(r, g, b): Rgb) -> Self {
        Rgba(r, g, b, 255)
    }
}

impl From<Bgr> for Rgba {
    fn from(Bgr(b, g, r): Bgr) -> Self {
        Rgba(r, g, b, 255)
    }
}

macro_rules! three {
    ($x:ident) => {
        unsafe impl AsBytes for $x {
            type Bytes = [u8; 3];
            fn width() -> usize { 3 }
        }

        impl From<[u8; 3]> for $x {
            fn from(c: [u8; 3]) -> Self {
                $x(c[0], c[1], c[2])
            }
        }

        impl From<$x> for [u8; 3] {
            fn from(p: $x) -> Self {
                [p.0, p.1, p.2]
            }
        }
    }
}

three!(Rgb);
three!(Bgr);