asdf_pixel_sort/
options.rs

1use once_cell::sync::Lazy;
2
3use crate::PColor;
4
5/// Options to configure behaviours.
6#[derive(Clone, Debug, Default, Eq, PartialEq)]
7pub struct Options {
8    /// Sorting mode.
9    pub mode: Mode,
10
11    /// Sorting direction.
12    pub direction: Direction,
13}
14
15/// Default value of [`Mode::Black`].
16pub static DEFAULT_BLACK: Lazy<PColor> = Lazy::new(|| PColor::new(11, 220, 0));
17
18/// Default value of [`Mode::Brightness`].
19pub static DEFAULT_BRIGHTNESS: u8 = 60;
20
21/// Default value of [`Mode::White`].
22pub static DEFAULT_WHITE: Lazy<PColor> = Lazy::new(|| PColor::new(57, 162, 192));
23
24/// Sorting modes.
25#[derive(Clone, Debug, Eq, PartialEq)]
26pub enum Mode {
27    /// Black mode with a threshold color.
28    Black(PColor),
29
30    /// Brightness mode with a threshold value `0` to `255`.
31    Brightness(u8),
32
33    /// White mode with a threshold color.
34    White(PColor),
35}
36
37impl Default for Mode {
38    fn default() -> Self {
39        Self::brightness()
40    }
41}
42
43impl Mode {
44    /// Black mode with a default threshold.
45    pub fn black() -> Self {
46        Self::Black(DEFAULT_BLACK.clone())
47    }
48
49    /// Brightness mode with a default threshold.
50    pub fn brightness() -> Self {
51        Self::Brightness(DEFAULT_BRIGHTNESS)
52    }
53
54    /// White mode with a default threshold.
55    pub fn white() -> Self {
56        Self::White(DEFAULT_WHITE.clone())
57    }
58}
59
60/// Sorting direction.
61#[derive(Clone, Debug, Eq, PartialEq)]
62pub enum Direction {
63    /// Both column and row.
64    Both,
65
66    /// Only column.
67    Column,
68
69    /// Only row.
70    Row,
71}
72
73impl Default for Direction {
74    fn default() -> Self {
75        Direction::Both
76    }
77}
78
79impl Direction {
80    /// Checks if this direction has [`Direction::Column`].
81    pub fn has_column(&self) -> bool {
82        match self {
83            Self::Both | Self::Column => true,
84            _ => false,
85        }
86    }
87
88    /// Checks if this direction has [`Direction::Row`].
89    pub fn has_row(&self) -> bool {
90        match self {
91            Self::Both | Self::Row => true,
92            _ => false,
93        }
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn test_options_default() {
103        let expected = Options {
104            mode: Mode::Brightness(60),
105            direction: Direction::Both,
106        };
107        assert_eq!(expected, Options::default());
108    }
109
110    #[test]
111    fn test_mode_default() {
112        let expected = Mode::Brightness(60);
113        assert_eq!(expected, Mode::default());
114    }
115
116    #[test]
117    fn test_mode_black() {
118        let color = PColor::new(11, 220, 0);
119        let expected = Mode::Black(color);
120        assert_eq!(expected, Mode::black());
121    }
122
123    #[test]
124    fn test_mode_brightness() {
125        let value = 60;
126        let expected = Mode::Brightness(value);
127        assert_eq!(expected, Mode::brightness());
128    }
129
130    #[test]
131    fn test_mode_white() {
132        let color = PColor::new(57, 162, 192);
133        let expected = Mode::White(color);
134        assert_eq!(expected, Mode::white());
135    }
136
137    #[test]
138    fn test_direction_default() {
139        let expected = Direction::Both;
140        assert_eq!(expected, Direction::default());
141    }
142
143    #[test]
144    fn test_direction_has_column() {
145        assert!(Direction::Both.has_column());
146        assert!(Direction::Column.has_column());
147        assert!(!Direction::Row.has_column());
148    }
149
150    #[test]
151    fn test_direction_has_row() {
152        assert!(Direction::Both.has_row());
153        assert!(!Direction::Column.has_row());
154        assert!(Direction::Row.has_row());
155    }
156}