Skip to content

Commit

Permalink
feat(filters): add invert, grayscale and set_hue`
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Aug 16, 2024
1 parent 2126147 commit 942efee
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 2 deletions.
14 changes: 13 additions & 1 deletion example/colors.whatever-extension
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ Only works with rgba and hsla

{{ colors.source_color.default.rgba | set_alpha: 0.5 }}

Output: rgba(r, g, b, 0.5)
- 180
{{ colors.source_color.default.rgba | set_hue: -180.0 }}

+ 90
{{ colors.source_color.default.rgba | set_hue: 90.0 }}

grayscale
{{ colors.source_color.default.rgba | grayscale }}

invert
{{ colors.source_color.default.rgba | invert }}

chain together {{ colors.source_color.default.rgba | set_alpha: 0.7 | set_hue: -180.0 | invert }}
53 changes: 53 additions & 0 deletions src/filters/grayscale.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use upon::Value;

use colorsys::{ColorTransform, Hsl, Rgb};
use std::str::FromStr;

use crate::color::{
parse::{parse_color, check_string_value},
format::{format_hex, format_hex_stripped, format_rgb, format_rgba, format_hsl, format_hsla},
};

pub fn grayscale(value: &Value) -> Result<String, String> {
let string = check_string_value(value).unwrap();

let format = parse_color(string);

if format.is_none() {
return Ok(string.to_string());
}

match format.unwrap() {
"hex" => {
let mut color = Rgb::from_hex_str(string).unwrap();
color.grayscale_simple();
Ok(format_hex(&color))
}
"hex_stripped" => {
let mut color = Rgb::from_hex_str(string).unwrap();
color.grayscale_simple();
Ok(format_hex_stripped(&color))
}
"rgb" => {
let mut color = Rgb::from_str(string).unwrap();
color.grayscale_simple();
Ok(format_rgb(&color))
}
"rgba" => {
let mut color = Rgb::from_str(string).unwrap();
color.grayscale_simple();
Ok(format_rgba(&color, false))
}
"hsl" => {
let mut color = Hsl::from_str(string).unwrap();
color.grayscale_simple();
Ok(format_hsl(&color))
}
"hsla" => {
let mut color = Hsl::from_str(string).unwrap();
color.grayscale_simple();
Ok(format_hsla(&color, false))
}
v => Ok(v.to_string()),
}
}
60 changes: 60 additions & 0 deletions src/filters/hue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use upon::Value;

use colorsys::{ColorTransform, Hsl, Rgb};
use std::str::FromStr;

use crate::color::{
parse::{parse_color, check_string_value},
format::{format_hex, format_hex_stripped, format_rgb, format_rgba, format_hsl, format_hsla},
};

pub fn set_hue(value: &Value, amount: f64) -> Result<String, String> {
let string = check_string_value(value).unwrap();

let format = parse_color(string);

debug!("Setting alpha on string {} by {}", string, amount);

if format.is_none() {
error!("Could not detect the format for string {:?}", string);
return Ok(string.to_string());
}

if !(-360.0..=360.0).contains(&amount) {
return Err("alpha must be in range [-360.0 to 360.0]".to_string());
}

match format.unwrap() {
"hex" => {
let mut color = Rgb::from_hex_str(string).unwrap();
color.adjust_hue(amount);
Ok(format_hex(&color))
}
"hex_stripped" => {
let mut color = Rgb::from_hex_str(string).unwrap();
color.adjust_hue(amount);
Ok(format_hex_stripped(&color))
}
"rgb" => {
let mut color = Rgb::from_str(string).unwrap();
color.adjust_hue(amount);
Ok(format_rgb(&color))
}
"rgba" => {
let mut color = Rgb::from_str(string).unwrap();
color.adjust_hue(amount);
Ok(format_rgba(&color, false))
}
"hsl" => {
let mut color = Hsl::from_str(string).unwrap();
color.adjust_hue(amount);
Ok(format_hsl(&color))
}
"hsla" => {
let mut color = Hsl::from_str(string).unwrap();
color.adjust_hue(amount);
Ok(format_hsla(&color, false))
}
v => Ok(v.to_string()),
}
}
53 changes: 53 additions & 0 deletions src/filters/invert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use upon::Value;

use colorsys::{ColorTransform, Hsl, Rgb};
use std::str::FromStr;

use crate::color::{
parse::{parse_color, check_string_value},
format::{format_hex, format_hex_stripped, format_rgb, format_rgba, format_hsl, format_hsla},
};

pub fn invert(value: &Value) -> Result<String, String> {
let string = check_string_value(value).unwrap();

let format = parse_color(string);

if format.is_none() {
return Ok(string.to_string());
}

match format.unwrap() {
"hex" => {
let mut color = Rgb::from_hex_str(string).unwrap();
color.invert();
Ok(format_hex(&color))
}
"hex_stripped" => {
let mut color = Rgb::from_hex_str(string).unwrap();
color.invert();
Ok(format_hex_stripped(&color))
}
"rgb" => {
let mut color = Rgb::from_str(string).unwrap();
color.invert();
Ok(format_rgb(&color))
}
"rgba" => {
let mut color = Rgb::from_str(string).unwrap();
color.invert();
Ok(format_rgba(&color, false))
}
"hsl" => {
let mut color = Hsl::from_str(string).unwrap();
color.invert();
Ok(format_hsl(&color))
}
"hsla" => {
let mut color = Hsl::from_str(string).unwrap();
color.invert();
Ok(format_hsla(&color, false))
}
v => Ok(v.to_string()),
}
}
5 changes: 4 additions & 1 deletion src/filters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod lightness;
pub mod alpha;
pub mod alpha;
pub mod hue;
pub mod grayscale;
pub mod invert;
9 changes: 9 additions & 0 deletions src/util/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use color_eyre::{eyre::Result, Report};

use colorsys::{ColorAlpha, Hsl};
use material_colors::color::Argb;
use matugen::filters::grayscale::grayscale;
use matugen::filters::hue::set_hue;
use matugen::filters::invert::invert;
use proper_path_tools::path::rebase;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -298,8 +301,14 @@ fn export_template(
}

fn add_engine_filters(engine: &mut Engine) {
// Color manipulation
engine.add_filter("set_lightness", set_lightness);
engine.add_filter("set_alpha", set_alpha);
engine.add_filter("set_hue", set_hue);
engine.add_filter("grayscale", grayscale);
engine.add_filter("invert", invert);

// String manipulation
engine.add_filter("to_upper", str::to_uppercase);
engine.add_filter("to_lower", str::to_lowercase);
engine.add_filter("replace", |s: String, from: String, to: String| {
Expand Down

0 comments on commit 942efee

Please sign in to comment.