Skip to content

Commit

Permalink
Merge pull request #114 from TanX-009/main
Browse files Browse the repository at this point in the history
feat(filter): add camel_case filter and auto_lightness filter
  • Loading branch information
InioX authored Oct 26, 2024
2 parents 6361ff9 + d3f625a commit d0761e5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/filters/camel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use upon::Value;

use crate::color::parse::check_string_value;

pub fn camel_case(value: &Value) -> Result<String, String> {
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)
}
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
1 change: 1 addition & 0 deletions src/filters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod alpha;
pub mod camel;
pub mod grayscale;
pub mod hue;
pub mod invert;
Expand Down
5 changes: 4 additions & 1 deletion src/template_util/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ 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;
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 @@ -43,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 All @@ -54,6 +56,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(
Expand Down

0 comments on commit d0761e5

Please sign in to comment.