chapter14_colorpractice/
lib.rs

1//! # Color
2//!
3//! A library for modeling artistic concepts.
4
5pub use self::kinds::PrimaryColor;
6pub use self::kinds::SecondaryColor;
7pub use self::utils::mix;
8
9pub mod kinds {
10    /// The primary lights according to the RGB light mode.
11    #[derive(std::fmt::Debug, std::cmp::PartialEq)]
12    pub enum PrimaryColor {
13        Cyan,
14        Magenta,
15        Yellow,
16    }
17
18    /// The secondary lights according to the RGB light model.
19    #[derive(std::fmt::Debug, std::cmp::PartialEq)]
20    pub enum SecondaryColor {
21        Undefined,
22        Red,
23        Blue,
24        Green,
25    }
26}
27
28pub mod utils {
29    use crate::kinds::*;
30
31    /// Combines two primary lights in equal amounts to create
32    /// a secondary light.
33    pub fn mix(l1: PrimaryColor, l2: PrimaryColor) -> SecondaryColor {
34        match (l1, l2) {
35            (PrimaryColor::Cyan, PrimaryColor::Yellow) | (PrimaryColor::Yellow, PrimaryColor::Cyan) => SecondaryColor::Green,
36            (PrimaryColor::Cyan, PrimaryColor::Magenta) | (PrimaryColor::Magenta, PrimaryColor::Cyan) => SecondaryColor::Blue,
37            (PrimaryColor::Yellow, PrimaryColor::Magenta) | (PrimaryColor::Magenta, PrimaryColor::Yellow) => SecondaryColor::Red,
38            (_, _) => SecondaryColor::Undefined,
39        }
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn it_works() {
49        assert_eq!(SecondaryColor::Red, mix(PrimaryColor::Yellow, PrimaryColor::Magenta));
50    }
51}