primitives/foundation/colorspace/
to_hex_string.rs

1use super::*;
2
3/// Defines conversion to hex sting functionality
4pub trait ToHexString {
5    /// Represents a color as a hexadecimal string
6    fn to_hex_string(&self) -> String;
7}
8
9impl<C: Into<RgbColor> + Clone> ToHexString for C {
10    fn to_hex_string(&self) -> String {
11        let RgbColor { red, green, blue } = self.clone().into();
12        format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
13    }
14}
15
16#[cfg(test)]
17mod test {
18    use super::*;
19    use lazy_static::lazy_static;
20
21    lazy_static! {
22        static ref TEST_DATA: Vec<(Color, &'static str)> = {//, CmykColor, HslColor)> = {
23            vec!(
24                (Color::rgb(56, 217, 169), "#38d9a9"),//, CmykColor::new(74.0, 0.0, 22.0, 15.0), HslColor::new(162.0, 68.0, 54.0)),
25                (Color::rgb(178, 242, 187), "#b2f2bb"),//, CmykColor::new(26.0, 0.0, 23.0, 5.0), HslColor::new(128.0, 71.0, 82.0)),
26                (Color::rgb(230, 252, 245), "#e6fcf5"),//, CmykColor::new(9.0, 0.0, 3.0, 1.0), HslColor::new(161.0, 79.0, 95.0)),
27                (Color::rgb(18, 184, 134), "#12b886"),//, CmykColor::new(90.0, 0.0, 27.0, 28.0), HslColor::new(162.0, 82.0, 40.0)),
28                //(Color::RGB(___), "#______", CmykColor::new(___), HslColor::new(___)),
29            )
30        };
31    }
32
33    #[test]
34    fn from_rgb() {
35        for (color, hex_str) in TEST_DATA.iter() {
36            assert_eq!(color.to_hex_string(), String::from(*hex_str));
37        }
38    }
39}