Implement builtin function darken()

This commit is contained in:
ConnorSkees 2020-02-14 08:54:43 -05:00
parent db8e8eaebb
commit c7c1ad5fe5
3 changed files with 35 additions and 0 deletions

View File

@ -191,4 +191,16 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
}; };
Some(Value::Color(color.lighten(amount))) 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)))
});
} }

View File

@ -152,6 +152,11 @@ impl Color {
Color::from_hsla(hue, saturation, luminance + amount, alpha) 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 { pub fn alpha(&self) -> Number {
self.alpha.clone() self.alpha.clone()
} }

View File

@ -234,3 +234,21 @@ test!(
// blocked on recognizing when to use 3-hex over 6-hex // blocked on recognizing when to use 3-hex over 6-hex
"a {\n color: #ee0000;\n}\n" "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"
);