From 87d00b535853dd5c05770962d009f93ca42168f3 Mon Sep 17 00:00:00 2001 From: InioX Date: Mon, 26 Feb 2024 18:06:03 +0100 Subject: [PATCH 1/6] feat(config): change `custom_keywords` configuration syntax --- example/config.toml | 3 +++ src/main.rs | 20 +++++++++++++++----- src/util/config.rs | 8 +------- src/util/template.rs | 7 +++---- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/example/config.toml b/example/config.toml index 938ff63..38b88c5 100644 --- a/example/config.toml +++ b/example/config.toml @@ -7,6 +7,9 @@ swww_options = [ "center", ] +[config.custom_keywords] +test = "aaaa" + [templates.name1] input_path = "example/colors.whatever-extension" output_path = "example/a/colors-generated.whatever-extension" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 126567c..86f6f29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -170,7 +170,9 @@ fn generate_scheme( ) -> Scheme { match scheme_type.unwrap() { SchemeTypes::SchemeContent => { - return Scheme::from(SchemeContent::new(Hct::new(source_color), is_dark, contrast_level).scheme) + return Scheme::from( + SchemeContent::new(Hct::new(source_color), is_dark, contrast_level).scheme, + ) } SchemeTypes::SchemeExpressive => { return Scheme::from( @@ -178,7 +180,9 @@ fn generate_scheme( ) } SchemeTypes::SchemeFidelity => { - return Scheme::from(SchemeFidelity::new(Hct::new(source_color), is_dark, contrast_level).scheme) + return Scheme::from( + SchemeFidelity::new(Hct::new(source_color), is_dark, contrast_level).scheme, + ) } SchemeTypes::SchemeFruitSalad => { return Scheme::from( @@ -191,13 +195,19 @@ fn generate_scheme( ) } SchemeTypes::SchemeNeutral => { - return Scheme::from(SchemeNeutral::new(Hct::new(source_color), is_dark, contrast_level).scheme) + return Scheme::from( + SchemeNeutral::new(Hct::new(source_color), is_dark, contrast_level).scheme, + ) } SchemeTypes::SchemeRainbow => { - return Scheme::from(SchemeRainbow::new(Hct::new(source_color), is_dark, contrast_level).scheme) + return Scheme::from( + SchemeRainbow::new(Hct::new(source_color), is_dark, contrast_level).scheme, + ) } SchemeTypes::SchemeTonalSpot => { - return Scheme::from(SchemeTonalSpot::new(Hct::new(source_color), is_dark, contrast_level).scheme) + return Scheme::from( + SchemeTonalSpot::new(Hct::new(source_color), is_dark, contrast_level).scheme, + ) } } } diff --git a/src/util/config.rs b/src/util/config.rs index 483f4d2..71e16f1 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -28,13 +28,7 @@ pub struct Config { pub swww_options: Option>, pub feh_options: Option>, pub prefix: Option, - pub custom_keywords: Option>, -} - -#[derive(Deserialize, Serialize, Debug)] -pub struct CustomKeyword { - pub find: String, - pub replace: String, + pub custom_keywords: Option>, } #[derive(Deserialize, Serialize, Debug)] diff --git a/src/util/template.rs b/src/util/template.rs index ea8ad4f..c971b54 100644 --- a/src/util/template.rs +++ b/src/util/template.rs @@ -16,7 +16,6 @@ use std::io::Write; use std::path::PathBuf; use crate::util::arguments::Source; -use crate::util::config::CustomKeyword; use resolve_path::PathResolveExt; use crate::{Schemes, SchemesEnum}; @@ -64,7 +63,7 @@ impl Template { prefix: &Option, source_color: &[u8; 4], default_scheme: &SchemesEnum, - custom_keywords: &Option>, + custom_keywords: &Option>, ) -> Result<(), Report> { let default_prefix = "@".to_string(); @@ -89,8 +88,8 @@ impl Template { let mut custom: HashMap = Default::default(); for entry in custom_keywords.iter() { - for (_name, values) in entry { - custom.insert(values.find.to_string(), values.replace.to_string()); + for (name, value) in entry { + custom.insert(name.to_string(), value.to_string()); } } From 86eea867bd0ec054c915c01e18309f28b95ea28a Mon Sep 17 00:00:00 2001 From: InioX Date: Sat, 2 Mar 2024 17:33:22 +0100 Subject: [PATCH 2/6] feat: add `colors_to_harmonize` --- example/colors.whatever-extension | 8 ++++++ example/config.toml | 7 ++++- src/main.rs | 9 +++++- src/util/color.rs | 48 +++++++++++++++++++++++++------ src/util/config.rs | 1 + src/util/template.rs | 26 ++++++++++++++++- 6 files changed, 88 insertions(+), 11 deletions(-) diff --git a/example/colors.whatever-extension b/example/colors.whatever-extension index 5bcb73d..60a8771 100644 --- a/example/colors.whatever-extension +++ b/example/colors.whatever-extension @@ -1,3 +1,11 @@ +red #FF0000; +green #00FF00; +blue #0000FF; + +red {{harmonized_colors.red.hex}}; +green {{harmonized_colors.green.hex}}; +blue {{harmonized_colors.blue.hex}}; + source_color {{colors.source_color.default.hex}}; primary {{colors.primary.default.hex}}; diff --git a/example/config.toml b/example/config.toml index 38b88c5..784edee 100644 --- a/example/config.toml +++ b/example/config.toml @@ -12,4 +12,9 @@ test = "aaaa" [templates.name1] input_path = "example/colors.whatever-extension" -output_path = "example/a/colors-generated.whatever-extension" \ No newline at end of file +output_path = "example/a/colors-generated.whatever-extension" + +[config.colors_to_harmonize] +green = "#00FF00" +red = "#FF0000" +blue = "#0000FF" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 86f6f29..2b67868 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize}; use std::io::Write; use update_informer::{registry, Check}; -use util::arguments::SchemeTypes; +use util::{arguments::SchemeTypes, color::harmonize_colors}; use material_colors::{ SchemeContent, SchemeExpressive, SchemeFidelity, SchemeFruitSalad, SchemeMonochrome, @@ -76,6 +76,12 @@ fn main() -> Result<(), Report> { light: IndexMap::from_iter(scheme_light), }; + let mut harmonized_colors = None; + + if let Some(colors) = &config.config.colors_to_harmonize { + harmonized_colors = Some(harmonize_colors(&source_color, colors)); + }; + if args.show_colors == Some(true) { show_color(&schemes, &source_color); } @@ -93,6 +99,7 @@ fn main() -> Result<(), Report> { &source_color, &default_scheme, &config.config.custom_keywords, + harmonized_colors, )?; if config.config.reload_apps == Some(true) { diff --git a/src/util/color.rs b/src/util/color.rs index 3eab237..a6f7e07 100644 --- a/src/util/color.rs +++ b/src/util/color.rs @@ -6,8 +6,8 @@ use crate::Schemes; use crate::util::image::fetch_image; -use image::io::Reader as ImageReader; use image::imageops::{resize, FilterType}; +use image::io::Reader as ImageReader; use super::arguments::{ColorFormat, Format, Source}; use super::image::source_color_from_image; @@ -17,6 +17,8 @@ use serde_json::json; use std::collections::HashMap; use std::str::FromStr; +use material_colors::blend::harmonize; + pub fn rgb_from_argb(color: [u8; 4]) -> Rgb { Rgb::from([ color[1] as f64, @@ -26,6 +28,30 @@ pub fn rgb_from_argb(color: [u8; 4]) -> Rgb { ]) } +pub fn harmonize_colors( + source_color: &[u8; 4], + colors: &HashMap, +) -> HashMap { + debug!("colors_to_harmonize: {:#?}", &colors); + let mut harmonized_colors: HashMap = HashMap::default(); + + for (name, color) in colors { + let rgb = Rgb::from_hex_str(&color) + .expect("Invalid hex color string provided for `harmonized_colors`"); + + let argb: [u8; 4] = [ + rgb.alpha() as u8, + rgb.red() as u8, + rgb.green() as u8, + rgb.blue() as u8, + ]; + harmonized_colors.insert(name.to_string(), harmonize(argb, *source_color)); + } + + debug!("harmonized_colors: {:#?}", &harmonized_colors); + harmonized_colors +} + pub fn show_color(schemes: &Schemes, source_color: &[u8; 4]) { let mut table: Table = generate_table_format(); @@ -36,7 +62,12 @@ pub fn show_color(schemes: &Schemes, source_color: &[u8; 4]) { generate_table_rows(&mut table, field, color_light, color_dark); } - generate_table_rows(&mut table, "source_color", rgb_from_argb(*source_color), rgb_from_argb(*source_color)); + generate_table_rows( + &mut table, + "source_color", + rgb_from_argb(*source_color), + rgb_from_argb(*source_color), + ); table.printstd(); } @@ -161,12 +192,13 @@ pub fn get_source_color(source: &Source) -> Result<[u8; 4], Report> { Source::Image { path } => { // test info!("Opening image in {}", path); - let img = ImageReader::open(path).expect("failed to open image") - .with_guessed_format() - .expect("failed to guess format") - .decode() - .expect("failed to decode image") - .into_rgba8(); + let img = ImageReader::open(path) + .expect("failed to open image") + .with_guessed_format() + .expect("failed to guess format") + .decode() + .expect("failed to decode image") + .into_rgba8(); let img = resize(&img, 128, 128, FilterType::Gaussian); source_color_from_image(img)? diff --git a/src/util/config.rs b/src/util/config.rs index 71e16f1..474b740 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -29,6 +29,7 @@ pub struct Config { pub feh_options: Option>, pub prefix: Option, pub custom_keywords: Option>, + pub colors_to_harmonize: Option>, } #[derive(Deserialize, Serialize, Debug)] diff --git a/src/util/template.rs b/src/util/template.rs index c971b54..a3a616a 100644 --- a/src/util/template.rs +++ b/src/util/template.rs @@ -64,6 +64,7 @@ impl Template { source_color: &[u8; 4], default_scheme: &SchemesEnum, custom_keywords: &Option>, + harmonized_colors: Option>, ) -> Result<(), Report> { let default_prefix = "@".to_string(); @@ -85,6 +86,8 @@ impl Template { let colors = generate_colors(schemes, source_color, default_scheme)?; + let harmonized = generate_harmonized_colors(source_color, harmonized_colors)?; + let mut custom: HashMap = Default::default(); for entry in custom_keywords.iter() { @@ -93,6 +96,12 @@ impl Template { } } + let render_data = upon::value! { + colors: &colors, image: image, custom: &custom, harmonized_colors: harmonized, + }; + + // debug!("render_data: {:#?}", &render_data); + for (i, (name, template)) in templates.iter().enumerate() { let input_path_absolute = template.input_path.try_resolve()?; let output_path_absolute = template.output_path.try_resolve()?; @@ -142,7 +151,7 @@ impl Template { let data = engine .template(name) - .render(upon::value! { colors: &colors, image: image, custom: &custom, }) + .render(&render_data) .to_string() .map_err(|error| { let message = format!( @@ -226,6 +235,21 @@ fn generate_colors( Ok(hashmap) } +fn generate_harmonized_colors( + source_color: &[u8; 4], + harmonized_colors: Option>, +) -> Result>, Report> { + if let Some(colors) = harmonized_colors { + let mut map: HashMap = Default::default(); + for (name, color) in colors { + map.insert(name, generate_color_strings(color)); + } + Ok(Some(map)) + } else { + return Ok(None); + } +} + fn generate_single_color( field: &str, schemes: &Schemes, From a7a514a6923fc372716451b65465aa82935d7a99 Mon Sep 17 00:00:00 2001 From: InioX Date: Sun, 3 Mar 2024 12:50:46 +0100 Subject: [PATCH 3/6] feat: add `set_lightness` filter --- Cargo.toml | 2 +- example/colors.whatever-extension | 18 ++++++-- src/util/color.rs | 46 ++++++++++++++++++++ src/util/filters.rs | 69 ++++++++++++++++++++++++++++++ src/util/mod.rs | 1 + src/util/template.rs | 71 ++++++++++++++++++++----------- 6 files changed, 177 insertions(+), 30 deletions(-) create mode 100644 src/util/filters.rs diff --git a/Cargo.toml b/Cargo.toml index 8ca580f..918dcc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,4 +41,4 @@ upon = "0.8.0" reqwest = { version = "0.11.23", default_features=false, features = ["blocking", "rustls-tls"] } material-colors = "0.1.4" ahash = "0.8.7" -indexmap = "2.2.2" +indexmap = "2.2.2" \ No newline at end of file diff --git a/example/colors.whatever-extension b/example/colors.whatever-extension index 60a8771..f5cd5c9 100644 --- a/example/colors.whatever-extension +++ b/example/colors.whatever-extension @@ -2,9 +2,21 @@ red #FF0000; green #00FF00; blue #0000FF; -red {{harmonized_colors.red.hex}}; -green {{harmonized_colors.green.hex}}; -blue {{harmonized_colors.blue.hex}}; +test {{ colors.source_color.default.rgb | set_lightness: 20.0 }} +test {{ colors.source_color.default.rgb | set_lightness: -20.0 }} +test {{ colors.source_color.default.rgb }} + +{{ colors.source_color.default.hex_stripped | set_lightness: 20.0 }} + +{{ colors.source_color.default.hex | set_lightness: 20.0 }} + +Should not change at all +{{ colors.source_color.default.red | set_lightness: 5.0 }} + +red {{harmonized_colors.red.rgb}}; +red {{harmonized_colors.red.rgba}}; +green {{harmonized_colors.green.hsl}}; +blue {{harmonized_colors.blue.hsla}}; source_color {{colors.source_color.default.hex}}; diff --git a/src/util/color.rs b/src/util/color.rs index a6f7e07..ddd09bc 100644 --- a/src/util/color.rs +++ b/src/util/color.rs @@ -28,6 +28,52 @@ pub fn rgb_from_argb(color: [u8; 4]) -> Rgb { ]) } +pub fn format_hex(color: &Rgb) -> String { + color.to_hex_string() +} + +pub fn format_hex_stripped(color: &Rgb) -> String { + color.to_hex_string()[1..].to_string() +} + +pub fn format_rgb(color: &Rgb) -> String { + format!( + "rgb({:?}, {:?}, {:?})", + color.red() as u8, + color.green() as u8, + color.blue() as u8, + ) +} + +pub fn format_rgba(color: &Rgb) -> String { + format!( + "rgba({:?}, {:?}, {:?}, {:?})", + color.red() as u8, + color.green() as u8, + color.blue() as u8, + color.alpha() as u8 + ) +} + +pub fn format_hsl(color: &Hsl) -> String { + format!( + "hsl({:?}, {:?}%, {:?}%)", + color.hue() as u8, + color.saturation() as u8, + color.lightness() as u8, + ) +} + +pub fn format_hsla(color: &Hsl) -> String { + format!( + "hsla({:?}, {:?}%, {:?}%, {:?})", + color.hue() as u8, + color.saturation() as u8, + color.lightness() as u8, + color.alpha() as u8 + ) +} + pub fn harmonize_colors( source_color: &[u8; 4], colors: &HashMap, diff --git a/src/util/filters.rs b/src/util/filters.rs new file mode 100644 index 0000000..d454ce4 --- /dev/null +++ b/src/util/filters.rs @@ -0,0 +1,69 @@ +use colorsys::ColorTransform; +use colorsys::Rgb; +use colorsys::{ColorAlpha, Hsl}; +use std::str::FromStr; +use upon::Value; + +use crate::util::template::{check_string_value, parse_color}; + +use crate::util::color::{ + format_hex, format_hex_stripped, format_hsl, format_hsla, format_rgb, format_rgba, +}; + +pub fn set_lightness(value: &Value, amount: f64) -> Result { + let string = check_string_value(value).unwrap(); + + let format = parse_color(string); + + debug!("Setting lightness on string {} by {}", string, amount); + + if format.is_none() { + return Ok(string.to_string()); + } + + match format.unwrap() { + "hex" => { + let mut color = Rgb::from_hex_str(string).unwrap(); + + color.lighten(amount); + + Ok(format_hex(&color)) + } + "hex_stripped" => { + let mut color = Rgb::from_hex_str(string).unwrap(); + + color.lighten(amount); + + Ok(format_hex_stripped(&color)) + } + "rgb" => { + let mut color = Rgb::from_str(string).unwrap(); + + color.lighten(amount); + + Ok(format_rgb(&color)) + } + "rgba" => { + let mut color = Rgb::from_str(string).unwrap(); + + color.lighten(amount); + + Ok(format_rgba(&color)) + } + "hsl" => { + let mut color = Hsl::from_str(string).unwrap(); + + color.lighten(amount); + + Ok(format_hsl(&color)) + } + "hsla" => { + let mut color = Hsl::from_str(string).unwrap(); + + color.lighten(amount); + + Ok(format_hsla(&color)) + } + v => Ok(v.to_string()), + } +} diff --git a/src/util/mod.rs b/src/util/mod.rs index 7b19360..2932f98 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,5 +1,6 @@ pub mod arguments; pub mod color; pub mod config; +pub mod filters; pub mod image; pub mod template; diff --git a/src/util/template.rs b/src/util/template.rs index a3a616a..9847cc3 100644 --- a/src/util/template.rs +++ b/src/util/template.rs @@ -3,8 +3,14 @@ use color_eyre::eyre::WrapErr; use color_eyre::Help; use color_eyre::{eyre::Result, Report}; +use colorsys::ColorTransform; +use colorsys::Rgb; use colorsys::{ColorAlpha, Hsl}; use serde::{Deserialize, Serialize}; +use std::str::FromStr; +use upon::Value; + +use crate::util::filters::set_lightness; use std::str; @@ -15,6 +21,10 @@ use std::fs::OpenOptions; use std::io::Write; use std::path::PathBuf; +use crate::util::color::{ + format_hex, format_hex_stripped, format_hsl, format_hsla, format_rgb, format_rgba, +}; + use crate::util::arguments::Source; use resolve_path::PathResolveExt; @@ -55,6 +65,29 @@ struct ColorVariants { use super::color::rgb_from_argb; +pub fn check_string_value(value: &Value) -> Option<&String> { + match value { + Value::String(v) => return Some(v), + v => return None, + } +} + +pub fn parse_color(string: &String) -> Option<&str> { + if let Some(s) = string.strip_prefix('#') { + return Some("hex"); + } + + if let (Some(i), Some(s)) = (string.find('('), string.strip_suffix(')')) { + let fname = s[..i].trim_end(); + return Some(fname); + } else if string.len() == 6 { + // Does not matter if it is actually a stripped hex or not, we handle it somewhere else. + return Some("hex_stripped"); + } else { + None + } +} + impl Template { pub fn generate( schemes: &Schemes, @@ -78,6 +111,8 @@ impl Template { let syntax = Syntax::builder().expr("{{", "}}").block("<*", "*>").build(); let mut engine = Engine::with_syntax(syntax); + engine.add_filter("set_lightness", set_lightness); + let image = match &source { Source::Image { path } => Some(path), Source::WebImage { .. } => None, @@ -280,32 +315,16 @@ fn generate_color_strings(color: [u8; 4]) -> Colora { let base_color = rgb_from_argb(color); let hsl_color = Hsl::from(&base_color); Colora { - hex: base_color.to_hex_string(), - hex_stripped: base_color.to_hex_string()[1..].to_string(), - rgb: format!( - "rgb({:?}, {:?}, {:?})", - base_color.red(), - base_color.green(), - base_color.blue() - ), - rgba: format!( - "rgba({:?}, {:?}, {:?}, {:?})", - base_color.red(), - base_color.green(), - base_color.blue(), - base_color.alpha() - ), - hsl: format!( - "hsl({:?}, {:?}, {:?})", - hsl_color.hue(), - hsl_color.saturation(), - hsl_color.lightness(), - ), - hsla: hsl_color.to_css_string(), - red: format!("{:?}", base_color.red()), - green: format!("{:?}", base_color.green()), - blue: format!("{:?}", base_color.blue()), - alpha: format!("{:?}", base_color.alpha()), + hex: format_hex(&base_color), + hex_stripped: format_hex_stripped(&base_color), + rgb: format_rgb(&base_color), + rgba: format_rgba(&base_color), + hsl: format_hsl(&hsl_color), + hsla: format_hsla(&hsl_color), + red: format!("{:?}", base_color.red() as u8), + green: format!("{:?}", base_color.green() as u8), + blue: format!("{:?}", base_color.blue() as u8), + alpha: format!("{:?}", base_color.alpha() as u8), hue: format!("{:?}", &hsl_color.hue()), lightness: format!("{:?}", &hsl_color.lightness()), saturation: format!("{:?}", &hsl_color.saturation()), From 59c3e8c5f048a911379871d218a836ead258c4fa Mon Sep 17 00:00:00 2001 From: InioX Date: Sun, 3 Mar 2024 13:38:25 +0100 Subject: [PATCH 4/6] feat: add `replace` filter --- example/colors.whatever-extension | 51 ++----------------------------- src/util/template.rs | 3 ++ 2 files changed, 6 insertions(+), 48 deletions(-) diff --git a/example/colors.whatever-extension b/example/colors.whatever-extension index f5cd5c9..609b616 100644 --- a/example/colors.whatever-extension +++ b/example/colors.whatever-extension @@ -20,51 +20,6 @@ blue {{harmonized_colors.blue.hsla}}; source_color {{colors.source_color.default.hex}}; -primary {{colors.primary.default.hex}}; -on_primary {{colors.on_primary.default.hex}}; -primary_container {{colors.primary_container.default.hex}}; -on_primary_container {{colors.on_primary_container.default.hex}}; -inverse_primary {{colors.inverse_primary.default.hex}}; -primary_fixed {{colors.primary_fixed.default.hex}}; -primary_fixed_dim {{colors.primary_fixed_dim.default.hex}}; -on_primary_fixed {{colors.on_primary_fixed.default.hex}}; -on_primary_fixed_variant {{colors.on_primary_fixed_variant.default.hex}}; -secondary {{colors.secondary.default.hex}}; -on_secondary {{colors.on_secondary.default.hex}}; -secondary_container {{colors.secondary_container.default.hex}}; -on_secondary_container {{colors.on_secondary_container.default.hex}}; -secondary_fixed {{colors.secondary_fixed.default.hex}}; -secondary_fixed_dim {{colors.secondary_fixed_dim.default.hex}}; -on_secondary_fixed {{colors.on_secondary_fixed.default.hex}}; -on_secondary_fixed_variant {{colors.on_secondary_fixed_variant.default.hex}}; -tertiary {{colors.tertiary.default.hex}}; -on_tertiary {{colors.on_tertiary.default.hex}}; -tertiary_container {{colors.tertiary_container.default.hex}}; -on_tertiary_container {{colors.on_tertiary_container.default.hex}}; -tertiary_fixed {{colors.tertiary_fixed.default.hex}}; -tertiary_fixed_dim {{colors.tertiary_fixed_dim.default.hex}}; -on_tertiary_fixed {{colors.on_tertiary_fixed.default.hex}}; -on_tertiary_fixed_variant {{colors.on_tertiary_fixed_variant.default.hex}}; -error {{colors.error.default.hex}}; -on_error {{colors.on_error.default.hex}}; -error_container {{colors.error_container.default.hex}}; -on_error_container {{colors.on_error_container.default.hex}}; -surface_dim {{colors.surface_dim.default.hex}}; -surface {{colors.surface.default.hex}}; -surface_bright {{colors.surface_bright.default.hex}}; -surface_container_lowest {{colors.surface_container_lowest.default.hex}}; -surface_container_low {{colors.surface_container_low.default.hex}}; -surface_container {{colors.surface_container.default.hex}}; -surface_container_high {{colors.surface_container_high.default.hex}}; -surface_container_highest {{colors.surface_container_highest.default.hex}}; -on_surface {{colors.on_surface.default.hex}}; -on_surface_variant {{colors.on_surface_variant.default.hex}}; -outline {{colors.outline.default.hex}}; -outline_variant {{colors.outline_variant.default.hex}}; -inverse_surface {{colors.inverse_surface.default.hex}}; -inverse_on_surface {{colors.inverse_on_surface.default.hex}}; -surface_variant {{colors.surface_variant.default.hex}}; -background {{colors.background.default.hex}}; -on_background {{colors.on_background.default.hex}}; -shadow {{colors.shadow.default.hex}}; -scrim {{colors.scrim.default.hex}}; \ No newline at end of file +<* for name, value in colors *> + {{name | replace: "_", "-" }} {{value.default.hex}}; +<* endfor *> \ No newline at end of file diff --git a/src/util/template.rs b/src/util/template.rs index 9847cc3..46aedbf 100644 --- a/src/util/template.rs +++ b/src/util/template.rs @@ -112,6 +112,9 @@ impl Template { let mut engine = Engine::with_syntax(syntax); engine.add_filter("set_lightness", set_lightness); + engine.add_filter("replace", |s: String, from: String, to: String| { + s.replace(&from, &to) + }); let image = match &source { Source::Image { path } => Some(path), From 5ee2c74ea64671772b794c29b69a1382cffe76c7 Mon Sep 17 00:00:00 2001 From: InioX Date: Sun, 3 Mar 2024 13:40:49 +0100 Subject: [PATCH 5/6] chore: run `clippy fix` --- src/main.rs | 16 ++++++++-------- src/util/color.rs | 14 +++++++------- src/util/filters.rs | 2 +- src/util/template.rs | 22 +++++++++++----------- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2b67868..f85e6c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -177,42 +177,42 @@ fn generate_scheme( ) -> Scheme { match scheme_type.unwrap() { SchemeTypes::SchemeContent => { - return Scheme::from( + Scheme::from( SchemeContent::new(Hct::new(source_color), is_dark, contrast_level).scheme, ) } SchemeTypes::SchemeExpressive => { - return Scheme::from( + Scheme::from( SchemeExpressive::new(Hct::new(source_color), is_dark, contrast_level).scheme, ) } SchemeTypes::SchemeFidelity => { - return Scheme::from( + Scheme::from( SchemeFidelity::new(Hct::new(source_color), is_dark, contrast_level).scheme, ) } SchemeTypes::SchemeFruitSalad => { - return Scheme::from( + Scheme::from( SchemeFruitSalad::new(Hct::new(source_color), is_dark, contrast_level).scheme, ) } SchemeTypes::SchemeMonochrome => { - return Scheme::from( + Scheme::from( SchemeMonochrome::new(Hct::new(source_color), is_dark, contrast_level).scheme, ) } SchemeTypes::SchemeNeutral => { - return Scheme::from( + Scheme::from( SchemeNeutral::new(Hct::new(source_color), is_dark, contrast_level).scheme, ) } SchemeTypes::SchemeRainbow => { - return Scheme::from( + Scheme::from( SchemeRainbow::new(Hct::new(source_color), is_dark, contrast_level).scheme, ) } SchemeTypes::SchemeTonalSpot => { - return Scheme::from( + Scheme::from( SchemeTonalSpot::new(Hct::new(source_color), is_dark, contrast_level).scheme, ) } diff --git a/src/util/color.rs b/src/util/color.rs index ddd09bc..0d76125 100644 --- a/src/util/color.rs +++ b/src/util/color.rs @@ -82,7 +82,7 @@ pub fn harmonize_colors( let mut harmonized_colors: HashMap = HashMap::default(); for (name, color) in colors { - let rgb = Rgb::from_hex_str(&color) + let rgb = Rgb::from_hex_str(color) .expect("Invalid hex color string provided for `harmonized_colors`"); let argb: [u8; 4] = [ @@ -132,19 +132,19 @@ pub fn dump_json(schemes: &Schemes, source_color: &[u8; 4], format: &Format) { ) }, F::Hsl => { - |c: Rgb| Hsl::from((c.red() as f64, c.green() as f64, c.blue() as f64)).to_css_string() + |c: Rgb| Hsl::from((c.red(), c.green(), c.blue())).to_css_string() } F::Hsla => |c: Rgb| { Hsl::from(( - c.red() as f64, - c.green() as f64, - c.blue() as f64, - c.alpha() as f64, + c.red(), + c.green(), + c.blue(), + c.alpha(), )) .to_css_string() }, F::Hex => |c: Rgb| c.to_hex_string(), - F::Strip => |c: Rgb| c.to_hex_string().replace("#", ""), + F::Strip => |c: Rgb| c.to_hex_string().replace('#', ""), }; let mut colors_normal_light: HashMap<&str, String> = HashMap::new(); diff --git a/src/util/filters.rs b/src/util/filters.rs index d454ce4..66794cd 100644 --- a/src/util/filters.rs +++ b/src/util/filters.rs @@ -1,6 +1,6 @@ use colorsys::ColorTransform; use colorsys::Rgb; -use colorsys::{ColorAlpha, Hsl}; +use colorsys::{Hsl}; use std::str::FromStr; use upon::Value; diff --git a/src/util/template.rs b/src/util/template.rs index 46aedbf..9cb45bf 100644 --- a/src/util/template.rs +++ b/src/util/template.rs @@ -3,11 +3,11 @@ use color_eyre::eyre::WrapErr; use color_eyre::Help; use color_eyre::{eyre::Result, Report}; -use colorsys::ColorTransform; -use colorsys::Rgb; + + use colorsys::{ColorAlpha, Hsl}; use serde::{Deserialize, Serialize}; -use std::str::FromStr; + use upon::Value; use crate::util::filters::set_lightness; @@ -67,19 +67,19 @@ use super::color::rgb_from_argb; pub fn check_string_value(value: &Value) -> Option<&String> { match value { - Value::String(v) => return Some(v), - v => return None, + Value::String(v) => Some(v), + _v => None, } } pub fn parse_color(string: &String) -> Option<&str> { - if let Some(s) = string.strip_prefix('#') { + if let Some(_s) = string.strip_prefix('#') { return Some("hex"); } if let (Some(i), Some(s)) = (string.find('('), string.strip_suffix(')')) { let fname = s[..i].trim_end(); - return Some(fname); + Some(fname) } else if string.len() == 6 { // Does not matter if it is actually a stripped hex or not, we handle it somewhere else. return Some("hex_stripped"); @@ -263,18 +263,18 @@ fn generate_colors( for (field, _color) in &schemes.dark { hashmap.insert( field.to_string(), - generate_single_color(field, &schemes, source_color, default_scheme)?, + generate_single_color(field, schemes, source_color, default_scheme)?, ); } hashmap.insert( String::from("source_color"), - generate_single_color("source_color", &schemes, source_color, default_scheme)?, + generate_single_color("source_color", schemes, source_color, default_scheme)?, ); Ok(hashmap) } fn generate_harmonized_colors( - source_color: &[u8; 4], + _source_color: &[u8; 4], harmonized_colors: Option>, ) -> Result>, Report> { if let Some(colors) = harmonized_colors { @@ -284,7 +284,7 @@ fn generate_harmonized_colors( } Ok(Some(map)) } else { - return Ok(None); + Ok(None) } } From 59042ef46005969f518c9bfe31acf864f8b42f7f Mon Sep 17 00:00:00 2001 From: InioX Date: Sun, 3 Mar 2024 16:50:09 +0100 Subject: [PATCH 6/6] feat: add `to_upper` and `to_lower` filters --- src/util/template.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/template.rs b/src/util/template.rs index 9cb45bf..9c50d4b 100644 --- a/src/util/template.rs +++ b/src/util/template.rs @@ -3,8 +3,6 @@ use color_eyre::eyre::WrapErr; use color_eyre::Help; use color_eyre::{eyre::Result, Report}; - - use colorsys::{ColorAlpha, Hsl}; use serde::{Deserialize, Serialize}; @@ -112,6 +110,8 @@ impl Template { let mut engine = Engine::with_syntax(syntax); engine.add_filter("set_lightness", set_lightness); + engine.add_filter("to_upper", str::to_uppercase); + engine.add_filter("to_lower", str::to_lowercase); engine.add_filter("replace", |s: String, from: String, to: String| { s.replace(&from, &to) });