Properly handle hue values above and below maximum

This commit is contained in:
ConnorSkees 2020-02-14 18:00:16 -05:00
parent 4303bd451c
commit ae9b97a6b2
2 changed files with 16 additions and 2 deletions

View File

@ -246,10 +246,14 @@ impl Color {
/// Create RGBA representation from HSLA values /// Create RGBA representation from HSLA values
pub fn from_hsla(hue: Number, saturation: Number, luminance: Number, alpha: Number) -> Self { 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) Number::from(360) + clamp!(hue, -360, 360)
} else { } else {
clamp!(hue, -360, 360) hue
}; };
let saturation = clamp!(saturation, 0, 1); let saturation = clamp!(saturation, 0, 1);
let luminance = clamp!(luminance, 0, 1); let luminance = clamp!(luminance, 0, 1);

View File

@ -133,6 +133,16 @@ test!(
"a {\n color: hsl(-60deg, 100%, 50%);\n}\n", "a {\n color: hsl(-60deg, 100%, 50%);\n}\n",
"a {\n color: fuchsia;\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!( test!(
hsla_named, hsla_named,
"a {\n color: hsla($hue: 193, $saturation: 67%, $luminance: 99, $alpha: .6);\n}\n", "a {\n color: hsla($hue: 193, $saturation: 67%, $luminance: 99, $alpha: .6);\n}\n",