diff --git a/src/lib.rs b/src/lib.rs index 130e470..9ed7116 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub mod color; pub mod exec; pub mod filters; pub mod scheme; +pub mod template_util; extern crate pretty_env_logger; #[macro_use] diff --git a/src/main.rs b/src/main.rs index cbc8fb5..5772d11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate paris_log; mod helpers; mod reload; -mod template; +pub mod template; mod util; mod wallpaper; diff --git a/src/template.rs b/src/template.rs index 7f4bd04..1dd21ff 100644 --- a/src/template.rs +++ b/src/template.rs @@ -3,17 +3,15 @@ use color_eyre::eyre::WrapErr; use color_eyre::Help; use color_eyre::{eyre::Result, Report}; -use colorsys::{ColorAlpha, Hsl}; use material_colors::color::Argb; -use matugen::filters::grayscale::grayscale; -use matugen::filters::hue::set_hue; -use matugen::filters::invert::invert; +use matugen::template_util::template::add_engine_filters; +use matugen::template_util::template::get_render_data; +use matugen::template_util::template::render_template; use serde::{Deserialize, Serialize}; use upon::Value; use matugen::exec::hook::format_hook; -use matugen::filters::{alpha::set_alpha, lightness::set_lightness}; use std::path::Path; use std::str; @@ -25,10 +23,6 @@ use std::fs::OpenOptions; use std::io::Write; use std::path::PathBuf; -use matugen::color::format::{ - format_hex, format_hex_stripped, format_hsl, format_hsla, format_rgb, format_rgba, -}; - use matugen::color::color::Source; use resolve_path::PathResolveExt; @@ -47,32 +41,6 @@ pub struct Template { pub post_hook: Option, } -#[derive(Serialize, Deserialize, Debug)] -struct Colora { - hex: String, - hex_stripped: String, - rgb: String, - rgba: String, - hsl: String, - hsla: String, - red: String, - green: String, - blue: String, - alpha: String, - hue: String, - saturation: String, - lightness: String, -} - -#[derive(Serialize, Deserialize, Debug)] -struct ColorVariants { - pub light: Colora, - pub dark: Colora, - pub default: Colora, -} - -use matugen::color::format::rgb_from_argb; - #[allow(clippy::manual_strip)] pub trait StripCanonicalization where @@ -122,19 +90,7 @@ impl Template { Source::Color { .. } => None, }; - let colors = generate_colors(schemes, source_color, default_scheme)?; - - let mut custom: HashMap = Default::default(); - - for entry in custom_keywords.iter() { - for (name, value) in entry { - custom.insert(name.to_string(), value.to_string()); - } - } - - let mut render_data = upon::value! { - colors: &colors, image: image, custom: &custom, - }; + let mut render_data = get_render_data(schemes, source_color, default_scheme, custom_keywords, image)?; for (i, (name, template)) in templates.iter().enumerate() { let (input_path_absolute, output_path_absolute) = @@ -257,20 +213,7 @@ fn export_template( i: usize, templates: &HashMap, ) -> Result<(), Report> { - let data = engine - .template(name) - .render(render_data) - .to_string() - .map_err(|error| { - let message = format!( - "[{} - {}]\n{:#}", - name, - input_path_absolute.display(), - &error - ); - - Report::new(error).wrap_err(message) - })?; + let data = render_template(engine, name, render_data, input_path_absolute.to_str())?; let out = if path_prefix.is_some() && !cfg!(windows) { let mut prefix_path = PathBuf::from(path_prefix.as_ref().unwrap()); @@ -313,86 +256,4 @@ fn export_template( ); Ok(()) -} - -fn add_engine_filters(engine: &mut Engine) { - // Color manipulation - engine.add_filter("set_lightness", set_lightness); - engine.add_filter("set_alpha", set_alpha); - engine.add_filter("set_hue", set_hue); - engine.add_filter("grayscale", grayscale); - engine.add_filter("invert", invert); - - // String manipulation - 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) - }); -} - -fn generate_colors( - schemes: &Schemes, - source_color: &Argb, - default_scheme: &SchemesEnum, -) -> Result, Report> { - let mut hashmap: HashMap = Default::default(); - for ((field, color_light), (_, color_dark)) in std::iter::zip(&schemes.light, &schemes.dark) { - hashmap.insert( - field.to_string(), - generate_single_color(field, source_color, default_scheme, *color_light, *color_dark)?, - ); - } - hashmap.insert( - String::from("source_color"), - generate_single_color("source_color", source_color, default_scheme, *source_color, *source_color)?, - ); - Ok(hashmap) -} - -fn generate_single_color( - field: &str, - source_color: &Argb, - default_scheme: &SchemesEnum, - color_light: Argb, - color_dark: Argb, -) -> Result { - let default_scheme_color = match default_scheme { - SchemesEnum::Light => color_light, - SchemesEnum::Dark => color_dark, - }; - - if field == "source_color" { - return Ok(ColorVariants { - default: generate_color_strings(*source_color), - light: generate_color_strings(*source_color), - dark: generate_color_strings(*source_color), - }); - } - - Ok(ColorVariants { - default: generate_color_strings(default_scheme_color), - light: generate_color_strings(color_light), - dark: generate_color_strings(color_dark), - }) -} - -fn generate_color_strings(color: Argb) -> Colora { - let base_color = rgb_from_argb(color); - let hsl_color = Hsl::from(&base_color); - Colora { - hex: format_hex(&base_color), - hex_stripped: format_hex_stripped(&base_color), - rgb: format_rgb(&base_color), - rgba: format_rgba(&base_color, true), - hsl: format_hsl(&hsl_color), - hsla: format_hsla(&hsl_color, true), - 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()), - } -} +} \ No newline at end of file diff --git a/src/template_util/mod.rs b/src/template_util/mod.rs new file mode 100644 index 0000000..01806cb --- /dev/null +++ b/src/template_util/mod.rs @@ -0,0 +1 @@ +pub mod template; \ No newline at end of file diff --git a/src/template_util/template.rs b/src/template_util/template.rs new file mode 100644 index 0000000..49baded --- /dev/null +++ b/src/template_util/template.rs @@ -0,0 +1,151 @@ +use std::collections::HashMap; + +use color_eyre::{eyre::Result, Report}; +use colorsys::{ColorAlpha, Hsl}; +use material_colors::color::Argb; +use upon::{Engine, Value}; + +use crate::filters::alpha::set_alpha; +use crate::filters::grayscale::grayscale; +use crate::filters::hue::set_hue; +use crate::filters::invert::invert; +use crate::filters::lightness::set_lightness; +use crate::scheme::{Schemes, SchemesEnum}; +use crate::color::format::{format_hex, format_hex_stripped, format_hsl, format_hsla, format_rgb, format_rgba, rgb_from_argb}; + +#[derive(serde::Serialize, serde::Deserialize, Debug)] +pub struct Color { + hex: String, + hex_stripped: String, + rgb: String, + rgba: String, + hsl: String, + hsla: String, + red: String, + green: String, + blue: String, + alpha: String, + hue: String, + saturation: String, + lightness: String, +} + +#[derive(serde::Serialize, serde::Deserialize, Debug)] +pub struct ColorVariants { + pub light: Color, + pub dark: Color, + pub default: Color, +} + +pub fn add_engine_filters(engine: &mut Engine) { + // Color manipulation + engine.add_filter("set_lightness", set_lightness); + engine.add_filter("set_alpha", set_alpha); + engine.add_filter("set_hue", set_hue); + engine.add_filter("grayscale", grayscale); + engine.add_filter("invert", invert); + + // String manipulation + 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) + }); +} + +pub fn render_template(engine: &Engine, name: &String, render_data: &Value, path: Option<&str>) -> Result { + let data = engine + .template(name) + .render(render_data) + .to_string() + .map_err(|error| { + let message = format!( + "[{} - {}]\n{:#}", + name, + path.unwrap_or(&"".to_string()), + &error + ); + + Report::new(error).wrap_err(message) + })?; + Ok(data) +} + +pub fn get_render_data(schemes: &Schemes, source_color: &Argb, default_scheme: &SchemesEnum, custom_keywords: &Option>, image: Option<&String>) -> Result { + let colors = generate_colors(schemes, source_color, default_scheme)?; + let mut custom: HashMap = Default::default(); + for entry in custom_keywords.iter() { + for (name, value) in entry { + custom.insert(name.to_string(), value.to_string()); + } + } + Ok(upon::value! { + colors: &colors, image: image, custom: &custom, + }) +} + +pub fn generate_colors( + schemes: &Schemes, + source_color: &Argb, + default_scheme: &SchemesEnum, +) -> Result, Report> { + let mut hashmap: HashMap = Default::default(); + for ((field, color_light), (_, color_dark)) in std::iter::zip(&schemes.light, &schemes.dark) { + hashmap.insert( + field.to_string(), + generate_single_color(field, source_color, default_scheme, *color_light, *color_dark)?, + ); + } + hashmap.insert( + String::from("source_color"), + generate_single_color("source_color", source_color, default_scheme, *source_color, *source_color)?, + ); + Ok(hashmap) +} + +pub fn generate_single_color( + field: &str, + source_color: &Argb, + default_scheme: &SchemesEnum, + color_light: Argb, + color_dark: Argb, +) -> Result { + let default_scheme_color = match default_scheme { + SchemesEnum::Light => color_light, + SchemesEnum::Dark => color_dark, + }; + + if field == "source_color" { + return Ok(ColorVariants { + default: generate_color_strings(*source_color), + light: generate_color_strings(*source_color), + dark: generate_color_strings(*source_color), + }); + } + + Ok(ColorVariants { + default: generate_color_strings(default_scheme_color), + light: generate_color_strings(color_light), + dark: generate_color_strings(color_dark), + }) +} + +fn generate_color_strings(color: Argb) -> Color { + let base_color = rgb_from_argb(color); + let hsl_color = Hsl::from(&base_color); + Color { + hex: format_hex(&base_color), + hex_stripped: format_hex_stripped(&base_color), + rgb: format_rgb(&base_color), + rgba: format_rgba(&base_color, true), + hsl: format_hsl(&hsl_color), + hsla: format_hsla(&hsl_color, true), + 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()), + } +} \ No newline at end of file