From c7c1ad5fe57c7c0c856857095d871d8e954d5bc3 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 14 Feb 2020 08:54:43 -0500 Subject: [PATCH] Implement builtin function `darken()` --- src/builtin/color.rs | 12 ++++++++++++ src/color/mod.rs | 5 +++++ tests/color.rs | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/builtin/color.rs b/src/builtin/color.rs index 728f686..dc5bd49 100644 --- a/src/builtin/color.rs +++ b/src/builtin/color.rs @@ -191,4 +191,16 @@ pub(crate) fn register(f: &mut BTreeMap) { }; Some(Value::Color(color.lighten(amount))) }); + decl!(f "darken", |args, _| { + let color = match arg!(args, 0, "color").eval() { + Value::Color(c) => c, + _ => todo!("non-color given to builtin function `darken()`") + }; + let amount = match arg!(args, 1, "amount").eval() { + Value::Dimension(n, Unit::None) => n, + Value::Dimension(n, Unit::Percent) => n / Number::from(100), + _ => todo!("expected either unitless or % number for amount"), + }; + Some(Value::Color(color.darken(amount))) + }); } diff --git a/src/color/mod.rs b/src/color/mod.rs index e247aa5..5b4f129 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -152,6 +152,11 @@ impl Color { Color::from_hsla(hue, saturation, luminance + amount, alpha) } + pub fn darken(&self, amount: Number) -> Self { + let (hue, saturation, luminance, alpha) = self.as_hsla(); + Color::from_hsla(hue, saturation, luminance - amount, alpha) + } + pub fn alpha(&self) -> Number { self.alpha.clone() } diff --git a/tests/color.rs b/tests/color.rs index 26f1cb4..43a8c27 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -234,3 +234,21 @@ test!( // blocked on recognizing when to use 3-hex over 6-hex "a {\n color: #ee0000;\n}\n" ); +// blocked on better parsing of call args +// test!( +// darken_named_args, +// "a {\n color: darken($color: hsl(25, 100%, 80%), $amount: 30%);\n}\n", +// "a {\n color: #ff6a00;\n}\n" +// ); +test!( + darken_basic, + "a {\n color: darken(hsl(25, 100%, 80%), 30%);\n}\n", + "a {\n color: #ff6a00;\n}\n" +); +test!( + darken_3_hex, + "a {\n color: darken(#800, 20%);\n}\n", + // eventually, this should become `#200` + // blocked on recognizing when to use 3-hex over 6-hex + "a {\n color: #220000;\n}\n" +);