diff --git a/src/builtin/color.rs b/src/builtin/color.rs index dc5bd49..2ed5fd6 100644 --- a/src/builtin/color.rs +++ b/src/builtin/color.rs @@ -203,4 +203,28 @@ pub(crate) fn register(f: &mut BTreeMap) { }; Some(Value::Color(color.darken(amount))) }); + decl!(f "saturate", |args, _| { + let color = match arg!(args, 0, "color").eval() { + Value::Color(c) => c, + _ => todo!("non-color given to builtin function `saturate()`") + }; + 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.saturate(amount))) + }); + decl!(f "desaturate", |args, _| { + let color = match arg!(args, 0, "color").eval() { + Value::Color(c) => c, + _ => todo!("non-color given to builtin function `desaturate()`") + }; + 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.desaturate(amount))) + }); } diff --git a/src/color/mod.rs b/src/color/mod.rs index 5b4f129..81a03ef 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -157,6 +157,16 @@ impl Color { Color::from_hsla(hue, saturation, luminance - amount, alpha) } + pub fn saturate(&self, amount: Number) -> Self { + let (hue, saturation, luminance, alpha) = self.as_hsla(); + Color::from_hsla(hue, saturation + amount, luminance, alpha) + } + + pub fn desaturate(&self, amount: Number) -> Self { + let (hue, saturation, luminance, alpha) = self.as_hsla(); + Color::from_hsla(hue, saturation - amount, luminance, alpha) + } + pub fn alpha(&self) -> Number { self.alpha.clone() } diff --git a/tests/color.rs b/tests/color.rs index 43a8c27..c544be8 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -252,3 +252,35 @@ test!( // blocked on recognizing when to use 3-hex over 6-hex "a {\n color: #220000;\n}\n" ); +// blocked on better parsing of call args +// test!( +// saturate_named_args, +// "a {\n color: saturate($color: hsl(25, 100%, 80%), $amount: 30%);\n}\n", +// "a {\n color: #ff6a00;\n}\n" +// ); +test!( + saturate_basic, + "a {\n color: saturate(hsl(120, 30%, 90%), 20%);\n}\n", + "a {\n color: #d9f2d9;\n}\n" +); +test!( + saturate_3_hex, + "a {\n color: saturate(#855, 20%);\n}\n", + "a {\n color: #9e3f3f;\n}\n" +); +// blocked on better parsing of call args +// test!( +// desaturate_named_args, +// "a {\n color: desaturate($color: hsl(25, 100%, 80%), $amount: 30%);\n}\n", +// "a {\n color: #ff6a00;\n}\n" +// ); +test!( + desaturate_basic, + "a {\n color: desaturate(hsl(120, 30%, 90%), 20%);\n}\n", + "a {\n color: #e3e8e3;\n}\n" +); +test!( + desaturate_3_hex, + "a {\n color: desaturate(#855, 20%);\n}\n", + "a {\n color: #726b6b;\n}\n" +);