From 3aa92b034291faed4d70329b8e9c54914f3737e6 Mon Sep 17 00:00:00 2001 From: erxclau Date: Mon, 2 Dec 2024 22:45:20 -0500 Subject: [PATCH 1/2] Round hues in hsl to rgb conversion --- src/color.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/color.rs b/src/color.rs index 06845a4..fe93498 100644 --- a/src/color.rs +++ b/src/color.rs @@ -273,9 +273,9 @@ fn hsl_to_rgb(hue: f32, saturation: f32, lightness: f32) -> Color { let green = hue_to_rgb(t1, t2, hue); let blue = hue_to_rgb(t1, t2, hue - 2.0); Color::new_rgb( - (red * 255.0) as u8, - (green * 255.0) as u8, - (blue * 255.0) as u8, + (red * 255.0).round() as u8, + (green * 255.0).round() as u8, + (blue * 255.0).round() as u8, ) } @@ -521,7 +521,7 @@ mod tests { test!( hsl_green, "hsl(120, 100%, 75%)", - Color::new_rgba(127, 255, 127, 255) + Color::new_rgba(128, 255, 128, 255) ); test!( @@ -545,13 +545,19 @@ mod tests { test!( hsla_green, "hsla(120, 100%, 75%, 0.5)", - Color::new_rgba(127, 255, 127, 127) + Color::new_rgba(128, 255, 128, 127) ); test!( hsl_with_alpha, "hsl(120, 100%, 75%, 0.5)", - Color::new_rgba(127, 255, 127, 127) + Color::new_rgba(128, 255, 128, 127) + ); + + test!( + hsl_to_rgb_red_round_up, + "hsl(230, 57%, 54%)", + Color::new_rgba(71, 93, 205, 255) ); test!( From 8aafe8faebc5c0ce188c4cfcfad403d21c930e5c Mon Sep 17 00:00:00 2001 From: erxclau Date: Tue, 3 Dec 2024 12:26:10 -0500 Subject: [PATCH 2/2] Fix tests --- src/color.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/color.rs b/src/color.rs index fe93498..c9d47ce 100644 --- a/src/color.rs +++ b/src/color.rs @@ -563,13 +563,13 @@ mod tests { test!( hsl_with_hue_float, "hsl(120.152, 100%, 75%)", - Color::new_rgba(127, 255, 127, 255) + Color::new_rgba(128, 255, 128, 255) ); test!( hsla_with_hue_float, "hsla(120.152, 100%, 75%, 0.5)", - Color::new_rgba(127, 255, 127, 127) + Color::new_rgba(128, 255, 128, 127) ); macro_rules! test_err {