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
/// Stores colorspace information
pub trait Color: Sync + Send {
    /// The name of a colorspace, for example: "rgb"
    fn name() -> &'static str;

    /// The number of channels
    fn channels() -> usize;

    /// Determines if the last channel should be used as an alpha channel
    fn has_alpha() -> bool;
}

macro_rules! make_color {
    ($name:ident, $name_s:expr, $channels:expr, $alpha:expr) => {
        #[cfg_attr(
            feature = "ser",
            derive(serde_derive::Serialize, serde_derive::Deserialize)
        )]
        #[derive(Debug, Clone, Copy, PartialEq)]
        pub struct $name;

        impl Color for $name {
            fn channels() -> usize {
                $channels
            }
            fn has_alpha() -> bool {
                $alpha
            }
            fn name() -> &'static str {
                $name_s
            }
        }
    };
}

/// Grayscale (1 channel, no alpha)
make_color!(Gray, "gray", 1, false);

/// RGB (3 channels, no alpha)
make_color!(Rgb, "rgb", 3, false);

/// BGR (3 channels, no alpha)
make_color!(Bgr, "bgr", 3, false);

/// RGB (packed, 1 channel, no alpha)
make_color!(RgbPacked, "rgb_packed", 1, false);

/// RGBA (4 channels)
make_color!(Rgba, "rgba", 4, true);

/// BGRA (4 channels)
make_color!(Bgra, "bgra", 4, true);

/// RGBA (packed, 1 channel, no alpha)
make_color!(RgbaPacked, "rgba_packed", 1, false);

/// CMYK (4 channels)
make_color!(Cmyk, "cmyk", 4, false);

/// YUV (3 channels)
make_color!(Yuv, "yuv", 3, false);