From d3f625a3e7f0b2232454cc0d559a36cc67b04f92 Mon Sep 17 00:00:00 2001 From: TanX-009 Date: Sun, 20 Oct 2024 18:48:20 +0530 Subject: [PATCH] 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);