Skip to content

Commit

Permalink
feat(filter): add auto_lightness filter
Browse files Browse the repository at this point in the history
auto_lightness filter increases lightness if under 50.0 and decreases
lightness if above 50.0
  • Loading branch information
TanX-009 committed Oct 20, 2024
1 parent 771cd3f commit d3f625a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
67 changes: 67 additions & 0 deletions src/filters/lightness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> {
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<String, String> {
let string = check_string_value(value).unwrap();

Expand Down
3 changes: 2 additions & 1 deletion src/template_util/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d3f625a

Please sign in to comment.