use crate::colorscheme::schema; use color_thief::{get_palette, ColorFormat}; use colorsys::{ColorTransform, Rgb}; use image::io::Reader as ImageReader; use std::path::Path; pub fn generate(path: &Path, light: bool) -> schema::Colorscheme { let image = ImageReader::open(path).unwrap().decode().unwrap(); // FIXME: pick best quality value (do benchmarks), 17? let palette = get_palette(&image.to_rgba8(), ColorFormat::Rgba, 10, 17).unwrap(); assert!( palette.len() == 16, "ColorThief couldn't generate a suitable palette" ); let colors: Vec = palette .iter() .map(|color| Rgb::new(color.r.into(), color.g.into(), color.b.into(), None)) .collect(); // TODO: sort by rgb_to_yiq let adjusted_colors = adjust_colors(&colors, light); let hex_colors: Vec = adjusted_colors.iter().map(Rgb::to_hex_string).collect(); schema::Colorscheme::from_vec_16(&hex_colors) } #[allow(clippy::indexing_slicing)] fn adjust_colors(colors: &[Rgb], light: bool) -> Vec { let mut raw_colors = colors.to_vec(); if light { raw_colors[0] = colors[0].clone(); raw_colors[0].lighten(90.0); raw_colors[7] = colors[0].clone(); raw_colors[7].lighten(-75.0); } else { // for color in &mut raw_colors { // color.lighten(40.0); // } raw_colors[0] = colors[0].clone(); raw_colors[0].lighten(-80.0); raw_colors[7] = colors[0].clone(); raw_colors[7].lighten(60.0); } raw_colors[8] = colors[0].clone(); raw_colors[8].lighten(20.0); raw_colors[15] = raw_colors[7].clone(); raw_colors }