1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use super::*;
pub trait ToHexString {
fn to_hex_string(&self) -> String;
}
impl<C: Into<RgbColor> + Clone> ToHexString for C {
fn to_hex_string(&self) -> String {
let RgbColor { red, green, blue } = self.clone().into();
format!("#{:0>2x}{:0>2x}{:0>2x}", red, green, blue)
}
}
#[cfg(test)]
mod test {
use super::*;
lazy_static! {
static ref TEST_DATA: Vec<(Color, &'static str)> = {
vec!(
(Color::RGB(56, 217, 169), "#38d9a9"),
(Color::RGB(178, 242, 187), "#b2f2bb"),
(Color::RGB(230, 252, 245), "#e6fcf5"),
(Color::RGB(18, 184, 134), "#12b886"),
)
};
}
#[test]
fn from_rgb() {
for (color, hex_str) in TEST_DATA.iter() {
assert_eq!(color.to_hex_string(), String::from(*hex_str));
}
}
}