Skip to content

Commit

Permalink
refactor: add float parameter for format_hsla and format_rgba (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
InioX committed Aug 13, 2024
1 parent f22dd21 commit 26b2f92
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
52 changes: 36 additions & 16 deletions src/util/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@ pub fn format_rgb(color: &Rgb) -> String {
)
}

pub fn format_rgba(color: &Rgb) -> String {
format!(
"rgba({:?}, {:?}, {:?}, {:?})",
color.red() as u8,
color.green() as u8,
color.blue() as u8,
color.alpha() as u8
)
pub fn format_rgba(color: &Rgb, float: bool) -> String {
if float {
format!(
"rgba({:?}, {:?}, {:?}, {:?})",
color.red() as u8,
color.green() as u8,
color.blue() as u8,
color.alpha()
)
} else {
format!(
"rgba({:?}, {:?}, {:?}, {:?})",
color.red() as u8,
color.green() as u8,
color.blue() as u8,
color.alpha() as u8
)
}
}

pub fn format_rgba_float(color: &Rgb) -> String {
Expand All @@ -83,14 +93,24 @@ pub fn format_hsl(color: &Hsl) -> String {
)
}

pub fn format_hsla(color: &Hsl) -> String {
format!(
"hsla({:?}, {:?}%, {:?}%, {:?})",
color.hue() as u8,
color.saturation() as u8,
color.lightness() as u8,
color.alpha() as u8
)
pub fn format_hsla(color: &Hsl, float: bool) -> String {
if float {
format!(
"hsla({:?}, {:?}%, {:?}%, {:?})",
color.hue() as u8,
color.saturation() as u8,
color.lightness() as u8,
color.alpha()
)
} else {
format!(
"hsla({:?}, {:?}%, {:?}%, {:?})",
color.hue() as u8,
color.saturation() as u8,
color.lightness() as u8,
color.alpha() as u8
)
}
}

pub fn format_hsla_float(color: &Hsl) -> String {
Expand Down
8 changes: 4 additions & 4 deletions src/util/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn set_lightness(value: &Value, amount: f64) -> Result<String, String> {

color.lighten(amount);

Ok(format_rgba(&color))
Ok(format_rgba(&color, true))
}
"hsl" => {
let mut color = Hsl::from_str(string).unwrap();
Expand All @@ -64,7 +64,7 @@ pub fn set_lightness(value: &Value, amount: f64) -> Result<String, String> {

color.lighten(amount);

Ok(format_hsla(&color))
Ok(format_hsla(&color, true))
}
v => Ok(v.to_string()),
}
Expand Down Expand Up @@ -92,13 +92,13 @@ pub fn set_alpha(value: &Value, amount: f64) -> Result<String, String> {
"rgba" => {
let mut color = Rgb::from_str(string).unwrap();
color.set_alpha(amount);
Ok(format_rgba_float(&color))
Ok(format_rgba(&color, true))
}
"hsl" => Err("cannot set alpha on hsl color, use hsla".to_string()),
"hsla" => {
let mut color = Hsl::from_str(string).unwrap();
color.set_alpha(amount);
Ok(format_hsla_float(&color))
Ok(format_hsla(&color, true))
}
v => Ok(v.to_string()),
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ fn generate_color_strings(color: Argb) -> Colora {
hex: format_hex(&base_color),
hex_stripped: format_hex_stripped(&base_color),
rgb: format_rgb(&base_color),
rgba: format_rgba(&base_color),
rgba: format_rgba(&base_color, true),
hsl: format_hsl(&hsl_color),
hsla: format_hsla(&hsl_color),
hsla: format_hsla(&hsl_color, true),
red: format!("{:?}", base_color.red() as u8),
green: format!("{:?}", base_color.green() as u8),
blue: format!("{:?}", base_color.blue() as u8),
Expand Down

0 comments on commit 26b2f92

Please sign in to comment.