Implement builtin function adjust-hue

This commit is contained in:
ConnorSkees 2020-02-14 08:14:30 -05:00
parent f8a09bec6e
commit 6427a7ab81
4 changed files with 47 additions and 0 deletions

View File

@ -166,4 +166,17 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
_ => 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)))
});
}

View File

@ -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()
}

View File

@ -58,6 +58,12 @@ impl From<BigInt> for Number {
}
}
impl From<Number> for BigInt {
fn from(b: Number) -> Self {
b.to_integer()
}
}
macro_rules! from_integer {
($ty:ty) => {
impl From<$ty> for Number {

View File

@ -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"
// );