art_lib/
lib.rs

1//! # Art
2//!
3//! A library for modeling artistic concepts.
4
5pub mod kinds {
6    /// The primary colors according to the RYB color model.
7    #[derive(Debug, PartialEq)]
8    pub enum PrimaryColor {
9        Red,
10        Yellow,
11        Blue,
12    }
13
14    /// The secondary colors according to the RYB color model.
15    #[derive(Debug, PartialEq)]
16    pub enum SecondaryColor {
17        Orange,
18        Green,
19        Purple,
20    }
21}
22
23pub mod utils {
24    use crate::kinds::*;
25
26    /// Combines two primary colors in equal amounts to create
27    /// a secondary color.
28    ///
29    /// # Example
30    /// ```
31    /// use art_lib::{SecondaryColor, PrimaryColor, mix};
32    /// assert_eq!(SecondaryColor::Purple, mix(PrimaryColor::Red, PrimaryColor::Blue));
33    /// assert_eq!(SecondaryColor::Purple, mix(PrimaryColor::Blue, PrimaryColor::Red));
34    /// assert_eq!(SecondaryColor::Orange, mix(PrimaryColor::Red, PrimaryColor::Yellow));
35    /// assert_eq!(SecondaryColor::Orange, mix(PrimaryColor::Yellow, PrimaryColor::Red));
36    /// assert_eq!(SecondaryColor::Green, mix(PrimaryColor::Yellow, PrimaryColor::Blue));
37    /// assert_eq!(SecondaryColor::Green, mix(PrimaryColor::Blue, PrimaryColor::Yellow));
38    /// ```
39    ///
40    /// # Panic
41    /// You can't mix the same colors
42    pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor {
43        match c1 {
44            PrimaryColor::Blue => match c2 {
45                PrimaryColor::Yellow => SecondaryColor::Green,
46                PrimaryColor::Red => SecondaryColor::Purple,
47                PrimaryColor::Blue => panic!("You can't mix the same colors"),
48            },
49            PrimaryColor::Yellow => match c2 {
50                PrimaryColor::Yellow => panic!("You can't mix the same colors"),
51                PrimaryColor::Red => SecondaryColor::Orange,
52                PrimaryColor::Blue => SecondaryColor::Green,
53            },
54            PrimaryColor::Red => match c2 {
55                PrimaryColor::Yellow => SecondaryColor::Orange,
56                PrimaryColor::Red => panic!("You can't mix the same colors"),
57                PrimaryColor::Blue => SecondaryColor::Purple,
58            },
59        }
60    }
61}
62
63pub use kinds::PrimaryColor;
64pub use kinds::SecondaryColor;
65pub use utils::mix;
66
67#[cfg(test)]
68mod test {
69    use super::*;
70
71    #[test]
72    #[should_panic(expected = "You can't mix the same colors")]
73    fn test_yellow_yellow() {
74        mix(PrimaryColor::Yellow, PrimaryColor::Yellow);
75    }
76
77    #[test]
78    #[should_panic(expected = "You can't mix the same colors")]
79    fn test_red_red() {
80        mix(PrimaryColor::Red, PrimaryColor::Red);
81    }
82
83    #[test]
84    #[should_panic(expected = "You can't mix the same colors")]
85    fn test_blue_blue() {
86        mix(PrimaryColor::Blue, PrimaryColor::Blue);
87    }
88}