From 38802b69c5cb1b75b7a19ae6a68fa1e45e271b3f Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 14 Feb 2020 09:46:50 -0500 Subject: [PATCH] Fix hue algorithm when red == blue and red > green --- src/color/mod.rs | 8 ++++---- tests/color.rs | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/color/mod.rs b/src/color/mod.rs index a66f9ee..9b3426c 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -102,7 +102,7 @@ impl Color { (((min + max) / Number::from(2)) * Number::from(100)).round() } - pub fn as_hsla(&self) -> (Number, Number, Number, Number) { + fn as_hsla(&self) -> (Number, Number, Number, Number) { let red = self.red.clone() / Number::from(255); let green = self.green.clone() / Number::from(255); let blue = self.blue.clone() / Number::from(255); @@ -125,12 +125,12 @@ impl Color { let mut hue = if min == max { Number::from(0) - } else if red == max { - (green - blue) / (max - min) + } else 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 1276745..7faed60 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -304,3 +304,8 @@ test!( "a {\n color: desaturate(#855, 20%);\n}\n", "a {\n color: #726b6b;\n}\n" ); +test!( + desaturate_correctly_calculates_hue, + "a {\n color: desaturate(plum, 14%);\n}\n", + "a {\n color: #d4a9d4;\n}\n" +);