diff --git a/whiskers/src/filters.rs b/whiskers/src/filters.rs index 4dc8ba2..025220f 100644 --- a/whiskers/src/filters.rs +++ b/whiskers/src/filters.rs @@ -132,3 +132,39 @@ pub fn trunc( )?; Ok(tera::to_value(format!("{value:.places$}"))?) } + +pub fn css_rgb( + value: &tera::Value, + _args: &HashMap, +) -> Result { + let color: Color = tera::from_value(value.clone())?; + let color: css_colors::RGB = (&color).into(); + Ok(tera::to_value(color.to_string())?) +} + +pub fn css_rgba( + value: &tera::Value, + _args: &HashMap, +) -> Result { + let color: Color = tera::from_value(value.clone())?; + let color: css_colors::RGBA = (&color).into(); + Ok(tera::to_value(color.to_string())?) +} + +pub fn css_hsl( + value: &tera::Value, + _args: &HashMap, +) -> Result { + let color: Color = tera::from_value(value.clone())?; + let color: css_colors::HSL = (&color).into(); + Ok(tera::to_value(color.to_string())?) +} + +pub fn css_hsla( + value: &tera::Value, + _args: &HashMap, +) -> Result { + let color: Color = tera::from_value(value.clone())?; + let color: css_colors::HSLA = (&color).into(); + Ok(tera::to_value(color.to_string())?) +} diff --git a/whiskers/src/templating.rs b/whiskers/src/templating.rs index 418bafa..6e1df6a 100644 --- a/whiskers/src/templating.rs +++ b/whiskers/src/templating.rs @@ -50,6 +50,10 @@ pub fn make_engine() -> tera::Tera { tera.register_filter("urlencode_lzma", filters::urlencode_lzma); tera.register_filter("trunc", filters::trunc); tera.register_filter("mix", filters::mix); + tera.register_filter("css_rgb", filters::css_rgb); + tera.register_filter("css_rgba", filters::css_rgba); + tera.register_filter("css_hsl", filters::css_hsl); + tera.register_filter("css_hsla", filters::css_hsla); tera.register_function("if", functions::if_fn); tera.register_function("object", functions::object); tera.register_function("css_rgb", functions::css_rgb); @@ -148,6 +152,26 @@ pub fn all_filters() -> Vec { description: "Truncate a number to a certain number of places".to_string(), examples: vec![filter_example!(1.123456 | trunc(places=3) => "1.123")], }, + Filter { + name: "css_rgb".to_string(), + description: "Convert a color to an RGB CSS string".to_string(), + examples: vec![filter_example!(red | css_rgb => "rgb(210, 15, 57)")], + }, + Filter { + name: "css_rgba".to_string(), + description: "Convert a color to an RGBA CSS string".to_string(), + examples: vec![filter_example!(red | css_rgba => "rgba(210, 15, 57, 1.00)")], + }, + Filter { + name: "css_hsl".to_string(), + description: "Convert a color to an HSL CSS string".to_string(), + examples: vec![filter_example!(red | css_hsl => "hsl(347, 87%, 44%)")], + }, + Filter { + name: "css_hsla".to_string(), + description: "Convert a color to an HSLA CSS string".to_string(), + examples: vec![filter_example!(red | css_hsla => "hsla(347, 87%, 44%, 1.00)")], + }, ] }