From 6427a7ab81d0f2b5ef0f8e2f57f2cf91048c6ba4 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 14 Feb 2020 08:14:30 -0500 Subject: [PATCH] Implement builtin function `adjust-hue` --- src/builtin/color.rs | 13 +++++++++++++ src/color/mod.rs | 7 +++++++ src/value/number.rs | 6 ++++++ tests/color.rs | 21 +++++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/src/builtin/color.rs b/src/builtin/color.rs index 0736c85..bfbcf94 100644 --- a/src/builtin/color.rs +++ b/src/builtin/color.rs @@ -166,4 +166,17 @@ pub(crate) fn register(f: &mut BTreeMap) { _ => todo!("non-color given to builtin function `alpha()`") } }); + decl!(f "adjust-hue", |args, _| { + let color = match arg!(args, 0, "color").eval() { + Value::Color(c) => c, + _ => todo!("non-color given to builtin function `adjust-hue()`") + }; + let degrees = match arg!(args, 1, "degrees").eval() { + Value::Dimension(n, Unit::None) + | Value::Dimension(n, Unit::Percent) + | Value::Dimension(n, Unit::Deg) => n, + _ => todo!("expected either unitless or % number for alpha"), + }; + Some(Value::Color(color.adjust_hue(degrees))) + }); } diff --git a/src/color/mod.rs b/src/color/mod.rs index b67853c..b7c94f8 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -102,6 +102,13 @@ impl Color { (((min + max) / Number::from(2)) * Number::from(100)).round() } + pub fn adjust_hue(&self, degrees: Number) -> Self { + let hue = self.hue(); + let saturation = Number::ratio(self.saturation(), 100); + let luminance = Number::ratio(self.lightness(), 100); + Color::from_hsla(hue + degrees, saturation, luminance, self.alpha()) + } + pub fn alpha(&self) -> Number { self.alpha.clone() } diff --git a/src/value/number.rs b/src/value/number.rs index 95820a1..f11343d 100644 --- a/src/value/number.rs +++ b/src/value/number.rs @@ -58,6 +58,12 @@ impl From for Number { } } +impl From for BigInt { + fn from(b: Number) -> Self { + b.to_integer() + } +} + macro_rules! from_integer { ($ty:ty) => { impl From<$ty> for Number { diff --git a/tests/color.rs b/tests/color.rs index 00fa809..a969061 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -196,3 +196,24 @@ test!( "a {\n color: invert(white, 20);\n}\n", "a {\n color: #cccccc;\n}\n" ); +test!( + adjust_hue_positive, + "a {\n color: adjust-hue(hsl(120, 30%, 90%), 60deg);\n}\n", + "a {\n color: #deeded;\n}\n" +); +test!( + adjust_hue_negative, + "a {\n color: adjust-hue(hsl(120, 30%, 90%), -60deg);\n}\n", + "a {\n color: #ededde;\n}\n" +); +test!( + adjust_hue_3_hex, + "a {\n color: adjust-hue(#811, 45deg);\n}\n", + "a {\n color: #886a11;\n}\n" +); +// blocked on better parsing of call args +// test!( +// adjust_hue_named_args, +// "a {\n color: adjust-hue($color: hsl(120, 30%, 90%), $degrees: 60deg);\n}\n", +// "a {\n color: #deeded;\n}\n" +// );