From ae9b97a6b2b0337e0db043b0905a4016b89609c4 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 14 Feb 2020 18:00:16 -0500 Subject: [PATCH] Properly handle hue values above and below maximum --- src/color/mod.rs | 8 ++++++-- tests/color.rs | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/color/mod.rs b/src/color/mod.rs index 6c17fd7..7566424 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -246,10 +246,14 @@ impl Color { /// Create RGBA representation from HSLA values pub fn from_hsla(hue: Number, saturation: Number, luminance: Number, alpha: Number) -> Self { - let mut hue = if hue < Number::from(0) { + let mut hue = if hue > Number::from(360) { + hue % Number::from(360) + } else if hue < Number::from(-360) { + Number::from(360) + hue % Number::from(360) + } else if hue < Number::from(0) { Number::from(360) + clamp!(hue, -360, 360) } else { - clamp!(hue, -360, 360) + hue }; let saturation = clamp!(saturation, 0, 1); let luminance = clamp!(luminance, 0, 1); diff --git a/tests/color.rs b/tests/color.rs index abda8fc..2c33039 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -133,6 +133,16 @@ test!( "a {\n color: hsl(-60deg, 100%, 50%);\n}\n", "a {\n color: fuchsia;\n}\n" ); +test!( + hsl_hue_above_max, + "a {\n color: hsl(540, 100%, 50%);\n}\n", + "a {\n color: aqua;\n}\n" +); +test!( + hsl_hue_below_min, + "a {\n color: hsl(-540, 100%, 50%);\n}\n", + "a {\n color: aqua;\n}\n" +); test!( hsla_named, "a {\n color: hsla($hue: 193, $saturation: 67%, $luminance: 99, $alpha: .6);\n}\n",