-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(filters): add
invert
, grayscale and
set_hue`
- Loading branch information
Showing
6 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters