off_rs/parser/
color_format.rs1#[derive(Copy, Clone, PartialEq, Debug)]
3pub enum ColorFormat {
4 RGBFloat,
6 RGBAFloat,
8 RGBInteger,
10 RGBAInteger,
12}
13
14impl ColorFormat {
15 #[must_use]
17 pub fn is_float(&self) -> bool {
18 matches!(self, ColorFormat::RGBFloat | ColorFormat::RGBAFloat)
19 }
20
21 #[must_use]
23 pub fn is_integer(&self) -> bool {
24 !self.is_float()
25 }
26
27 #[must_use]
29 pub fn has_alpha(&self) -> bool {
30 matches!(self, ColorFormat::RGBAFloat | ColorFormat::RGBAInteger)
31 }
32
33 #[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 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}