use crate::color::Rgb;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Swatch {
pub color: Rgb,
pub population: u32,
}
impl Swatch {
pub fn new(color: Rgb, population: u32) -> Self {
Self { color, population }
}
pub fn hex(&self) -> String {
self.color.to_hex()
}
pub fn complement(&self) -> Swatch {
let rgb = self.color.to_hsl().complement().to_rgb();
Swatch::new(rgb, self.population)
}
}
pub fn complementary_swatches(swatches: &[Swatch]) -> Vec<Swatch> {
swatches.iter().map(|s| s.complement()).collect()
}
pub fn swatches_to_json(swatches: &[Swatch]) -> String {
let entries: Vec<String> = swatches
.iter()
.map(|s| {
format!(
r#"{{"hex":"{}","r":{},"g":{},"b":{},"population":{}}}"#,
s.hex(),
s.color.r,
s.color.g,
s.color.b,
s.population
)
})
.collect();
format!("[{}]", entries.join(","))
}