primitives/foundation/colorspace/
to_hex_string.rs1use super::*;
2
3pub trait ToHexString {
5 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)> = {vec!(
24 (Color::rgb(56, 217, 169), "#38d9a9"),(Color::rgb(178, 242, 187), "#b2f2bb"),(Color::rgb(230, 252, 245), "#e6fcf5"),(Color::rgb(18, 184, 134), "#12b886"),)
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}