Fix how hue is calculated when color channels are equal

This commit is contained in:
ConnorSkees 2020-02-14 09:37:05 -05:00
parent be84350d19
commit 201f97a914
2 changed files with 26 additions and 6 deletions

View File

@ -52,12 +52,12 @@ impl Color {
return Number::from(0); return Number::from(0);
} }
let mut hue = if red == max { let mut hue = if blue == max {
(green - blue) / (max - min) Number::from(4) + (red - green) / (max - min)
} else if green == max { } else if green == max {
Number::from(2) + (blue - red) / (max - min) Number::from(2) + (blue - red) / (max - min)
} else { } else {
Number::from(4) + (red - green) / (max - min) (green - blue) / (max - min)
}; };
if hue < Number::from(0) { if hue < Number::from(0) {

View File

@ -163,12 +163,32 @@ test!(
); );
test!( test!(
hue, hue,
"$a: hsl(193, 67%, 28%);\n\na {\n color: hue($a);\n}\n", "a {\n color: hue(hsl(193, 67%, 28%));\n}\n",
"a {\n color: 193deg;\n}\n" "a {\n color: 193deg;\n}\n"
); );
test!(
hue_red_equals_blue,
"a {\n color: hue(rgb(1, 0, 1));\n}\n",
"a {\n color: 300deg;\n}\n"
);
test!(
hue_green_equals_blue,
"a {\n color: hue(rgb(0, 1, 1));\n}\n",
"a {\n color: 180deg;\n}\n"
);
test!(
hue_green_is_1,
"a {\n color: hue(rgb(0, 1, 0));\n}\n",
"a {\n color: 120deg;\n}\n"
);
test!(
hue_rgb_all_equal,
"a {\n color: hue(rgb(1, 1, 1));\n}\n",
"a {\n color: 0deg;\n}\n"
);
test!( test!(
saturation, saturation,
"$a: hsl(193, 67%, 28%);\n\na {\n color: saturation($a);\n}\n", "a {\n color: saturation(hsl(193, 67%, 28%));\n}\n",
"a {\n color: 67%;\n}\n" "a {\n color: 67%;\n}\n"
); );
test!( test!(
@ -178,7 +198,7 @@ test!(
); );
test!( test!(
lightness, lightness,
"$a: hsl(193, 67%, 28%);\n\na {\n color: lightness($a);\n}\n", "a {\n color: lightness(hsl(193, 67%, 28%));\n}\n",
"a {\n color: 28%;\n}\n" "a {\n color: 28%;\n}\n"
); );
test!( test!(