off_rs/parser/
color_format.rs

1/// The different color formats that can be parsed.
2#[derive(Copy, Clone, PartialEq, Debug)]
3pub enum ColorFormat {
4    /// Parses the red, green and blue values as floating point values ranging from (0.0, 0.0, 0.0) to (1.0, 1.0, 1.0)
5    RGBFloat,
6    /// Parses the red, green, blue and alpha values as floating point values ranging from (0.0, 0.0, 0.0, 0.0) to (1.0, 1.0, 1.0, 1.0)
7    RGBAFloat,
8    /// Parses the red, green and blue values as integers ranging from (0, 0, 0) to (255, 255, 255)
9    RGBInteger,
10    /// Parses the red, green, blue and alpha values as integers ranging from (0, 0, 0, 0) to (255, 255, 255, 255)
11    RGBAInteger,
12}
13
14impl ColorFormat {
15    /// Returns whether the color format is a floating point format.
16    #[must_use]
17    pub fn is_float(&self) -> bool {
18        matches!(self, ColorFormat::RGBFloat | ColorFormat::RGBAFloat)
19    }
20
21    /// Returns whether the color format is an integer format.
22    #[must_use]
23    pub fn is_integer(&self) -> bool {
24        !self.is_float()
25    }
26
27    /// Returns whether the color format contains an alpha channel.
28    #[must_use]
29    pub fn has_alpha(&self) -> bool {
30        matches!(self, ColorFormat::RGBAFloat | ColorFormat::RGBAInteger)
31    }
32
33    /// Returns the number of channels in the color format.
34    #[must_use]
35    pub fn channel_count(&self) -> usize {
36        if self.has_alpha() {
37            4
38        } else {
39            3
40        }
41    }
42}
43
44impl Default for ColorFormat {
45    /// The default color format is [`RGBFloat`].
46    // Because this format is specified in the implementation of the Princeton Shape Benchmark.
47    fn default() -> Self {
48        ColorFormat::RGBAFloat
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn color_format() {
58        assert!(ColorFormat::RGBFloat.is_float());
59        assert!(ColorFormat::RGBAFloat.is_float());
60        assert!(!ColorFormat::RGBInteger.is_float());
61        assert!(!ColorFormat::RGBAInteger.is_float());
62
63        assert!(!ColorFormat::RGBFloat.is_integer());
64        assert!(!ColorFormat::RGBAFloat.is_integer());
65        assert!(ColorFormat::RGBInteger.is_integer());
66        assert!(ColorFormat::RGBAInteger.is_integer());
67
68        assert!(!ColorFormat::RGBFloat.has_alpha());
69        assert!(ColorFormat::RGBAFloat.has_alpha());
70        assert!(!ColorFormat::RGBInteger.has_alpha());
71        assert!(ColorFormat::RGBAInteger.has_alpha());
72
73        assert_eq!(ColorFormat::RGBFloat.channel_count(), 3);
74        assert_eq!(ColorFormat::RGBAFloat.channel_count(), 4);
75        assert_eq!(ColorFormat::RGBInteger.channel_count(), 3);
76        assert_eq!(ColorFormat::RGBAInteger.channel_count(), 4);
77    }
78}