From 201f97a91431a8af8b0d0bdfabfaf8b851e7b1b7 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 14 Feb 2020 09:37:05 -0500 Subject: [PATCH] Fix how hue is calculated when color channels are equal --- src/color/mod.rs | 6 +++--- tests/color.rs | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/color/mod.rs b/src/color/mod.rs index 81a03ef..a66f9ee 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -52,12 +52,12 @@ impl Color { return Number::from(0); } - let mut hue = if red == max { - (green - blue) / (max - min) + let mut hue = if blue == max { + Number::from(4) + (red - green) / (max - min) } else if green == max { Number::from(2) + (blue - red) / (max - min) } else { - Number::from(4) + (red - green) / (max - min) + (green - blue) / (max - min) }; if hue < Number::from(0) { diff --git a/tests/color.rs b/tests/color.rs index c544be8..1276745 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -163,12 +163,32 @@ test!( ); test!( 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" ); +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!( 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" ); test!( @@ -178,7 +198,7 @@ test!( ); test!( 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" ); test!(