From 771cd3f793da3899027c7e9ba58f3c7f1aab69a9 Mon Sep 17 00:00:00 2001 From: TanX-009 Date: Sun, 20 Oct 2024 18:43:31 +0530 Subject: [PATCH 1/2] feat(filter): add camel_case filter --- src/filters/camel.rs | 27 +++++++++++++++++++++++++++ src/filters/mod.rs | 1 + src/template_util/template.rs | 2 ++ 3 files changed, 30 insertions(+) create mode 100644 src/filters/camel.rs diff --git a/src/filters/camel.rs b/src/filters/camel.rs new file mode 100644 index 0000000..00a72c6 --- /dev/null +++ b/src/filters/camel.rs @@ -0,0 +1,27 @@ +use upon::Value; + +use crate::color::parse::check_string_value; + +pub fn camel_case(value: &Value) -> Result { + let string = check_string_value(value).unwrap(); + + let mut result = String::new(); + let mut capitalize_next = false; + + for c in string.chars() { + if c == '_' { + capitalize_next = true; + } else { + if capitalize_next { + result.push(c.to_uppercase().next().unwrap()); + capitalize_next = false; + } else { + result.push(c); + } + } + } + + debug!("Converting to camelCase: {} to {}", string, result); + + Ok(result) +} diff --git a/src/filters/mod.rs b/src/filters/mod.rs index f43bcaf..56b665d 100644 --- a/src/filters/mod.rs +++ b/src/filters/mod.rs @@ -1,4 +1,5 @@ pub mod alpha; +pub mod camel; pub mod grayscale; pub mod hue; pub mod invert; diff --git a/src/template_util/template.rs b/src/template_util/template.rs index 65cb39f..1e68667 100644 --- a/src/template_util/template.rs +++ b/src/template_util/template.rs @@ -10,6 +10,7 @@ use crate::color::format::{ rgb_from_argb, }; use crate::filters::alpha::set_alpha; +use crate::filters::camel::camel_case; use crate::filters::grayscale::grayscale; use crate::filters::hue::set_hue; use crate::filters::invert::invert; @@ -54,6 +55,7 @@ pub fn add_engine_filters(engine: &mut Engine) { engine.add_filter("replace", |s: String, from: String, to: String| { s.replace(&from, &to) }); + engine.add_filter("camel_case", camel_case); } pub fn render_template( From d3f625a3e7f0b2232454cc0d559a36cc67b04f92 Mon Sep 17 00:00:00 2001 From: TanX-009 Date: Sun, 20 Oct 2024 18:48:20 +0530 Subject: [PATCH 2/2] feat(filter): add auto_lightness filter auto_lightness filter increases lightness if under 50.0 and decreases lightness if above 50.0 --- src/filters/lightness.rs | 67 +++++++++++++++++++++++++++++++++++ src/template_util/template.rs | 3 +- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/filters/lightness.rs b/src/filters/lightness.rs index 2fd8ffd..6b2809e 100644 --- a/src/filters/lightness.rs +++ b/src/filters/lightness.rs @@ -8,6 +8,73 @@ use crate::color::{ parse::{check_string_value, parse_color}, }; +fn adjust_rgb_lightness(color: &mut Rgb, amount: f64, threshold: f64) { + let hsl = Hsl::from(color.clone()); // Convert RGB to HSL + + // Adjust lightness based on the threshold + if hsl.lightness() < threshold { + color.lighten(amount); // Increase lightness + } else { + color.lighten(-amount); // Decrease lightness + } +} + +fn adjust_hsl_lightness(color: &mut Hsl, amount: f64, threshold: f64) { + // Adjust lightness based on the threshold + if color.lightness() < threshold { + color.lighten(amount); // Increase lightness + } else { + color.lighten(-amount); // Decrease lightness + } +} + +pub fn auto_lightness(value: &Value, amount: f64) -> Result { + let string = check_string_value(value).unwrap(); + let threshold = 50.0; + + 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(); + adjust_rgb_lightness(&mut color, amount, threshold); + Ok(format_hex(&color)) + } + "hex_stripped" => { + let mut color = Rgb::from_hex_str(string).unwrap(); + adjust_rgb_lightness(&mut color, amount, threshold); + Ok(format_hex_stripped(&color)) + } + "rgb" => { + let mut color = Rgb::from_str(string).unwrap(); + adjust_rgb_lightness(&mut color, amount, threshold); + Ok(format_rgb(&color)) + } + "rgba" => { + let mut color = Rgb::from_str(string).unwrap(); + adjust_rgb_lightness(&mut color, amount, threshold); + Ok(format_rgba(&color, true)) + } + "hsl" => { + let mut color = Hsl::from_str(string).unwrap(); + adjust_hsl_lightness(&mut color, amount, threshold); + Ok(format_hsl(&color)) + } + "hsla" => { + let mut color = Hsl::from_str(string).unwrap(); + adjust_hsl_lightness(&mut color, amount, threshold); + Ok(format_hsla(&color, true)) + } + v => Ok(v.to_string()), + } +} + pub fn set_lightness(value: &Value, amount: f64) -> Result { let string = check_string_value(value).unwrap(); diff --git a/src/template_util/template.rs b/src/template_util/template.rs index 1e68667..9b37c80 100644 --- a/src/template_util/template.rs +++ b/src/template_util/template.rs @@ -14,7 +14,7 @@ use crate::filters::camel::camel_case; use crate::filters::grayscale::grayscale; use crate::filters::hue::set_hue; use crate::filters::invert::invert; -use crate::filters::lightness::set_lightness; +use crate::filters::lightness::{auto_lightness, set_lightness}; use crate::scheme::{Schemes, SchemesEnum}; #[derive(serde::Serialize, serde::Deserialize, Debug)] @@ -44,6 +44,7 @@ pub struct ColorVariants { pub fn add_engine_filters(engine: &mut Engine) { // Color manipulation engine.add_filter("set_lightness", set_lightness); + engine.add_filter("auto_lightness", auto_lightness); engine.add_filter("set_alpha", set_alpha); engine.add_filter("set_hue", set_hue); engine.add_filter("grayscale", grayscale);