martin_art_lib 0.1.0

An art library, just for fun
Documentation
//! # Art
//!
//! A library for modeling artistic concepts.

pub use self::kinds::PrimaryColor;
pub use self::kinds::SecondaryColor;
pub use self::utils::mix;

pub mod kinds {
    /// The primary colors according to the RYB color model.
    #[derive(Debug)]
    #[derive(PartialEq)]
    pub enum PrimaryColor {
        Red,
        Yellow,
        Blue,
    }

    /// The secondary colors according to the RYB color model.
    #[derive(Debug)]
    #[derive(PartialEq)]
    pub enum SecondaryColor {
        Red,
        Yellow,
        Blue,
        Orange,
        Green,
        Purple,
    }
}

pub mod utils {
    use crate::kinds::*;

    /// Combines two primary colors in equal amounts to create
    /// a secondary color.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use martin_art_lib::PrimaryColor;
    /// use martin_art_lib::SecondaryColor;
    /// use martin_art_lib::mix;
    /// 
    /// assert_eq!(SecondaryColor::Orange, mix(PrimaryColor::Yellow, PrimaryColor::Red));
    /// ```
    /// 
    /// ```
    /// use martin_art_lib::utils::mix;
    /// use martin_art_lib::kinds::SecondaryColor;
    /// use martin_art_lib::kinds::PrimaryColor;
    /// 
    /// assert_eq!(SecondaryColor::Green, mix(PrimaryColor::Blue, PrimaryColor::Yellow));
    /// ```
    pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor {
        match c1 {
            PrimaryColor::Red => {
                match c2 {
                    PrimaryColor::Red => SecondaryColor::Red,
                    PrimaryColor::Yellow => SecondaryColor::Orange,
                    PrimaryColor::Blue => SecondaryColor::Purple,
                }
            },
            PrimaryColor::Yellow => {
                match c2 {
                    PrimaryColor::Red => SecondaryColor::Orange,
                    PrimaryColor::Yellow => SecondaryColor::Yellow,
                    PrimaryColor::Blue => SecondaryColor::Green,
                }
            },
            PrimaryColor::Blue => {
                match c2 {
                    PrimaryColor::Red => SecondaryColor::Purple,
                    PrimaryColor::Yellow => SecondaryColor::Green,
                    PrimaryColor::Blue => SecondaryColor::Blue,
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use kinds::SecondaryColor;
    use kinds::PrimaryColor;
    use utils::mix;

    #[test]
    fn mix_a_few_primary_colors() {
        assert_eq!(SecondaryColor::Green, mix(PrimaryColor::Blue, PrimaryColor::Yellow));
        assert_eq!(SecondaryColor::Orange, mix(PrimaryColor::Yellow, PrimaryColor::Red));
        assert_eq!(SecondaryColor::Purple, mix(PrimaryColor::Red, PrimaryColor::Blue));
    }
}