Implement builtin function complement()

This commit is contained in:
ConnorSkees 2020-02-14 12:56:28 -05:00
parent 7f17139a3b
commit 3af2292d5b
3 changed files with 22 additions and 0 deletions

View File

@ -135,6 +135,13 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
}; };
Some(Value::Color(color.desaturate(Number::from(1)))) Some(Value::Color(color.desaturate(Number::from(1))))
}); });
decl!(f "complement", |args, _| {
let color = match arg!(args, 0, "color").eval() {
Value::Color(c) => c,
_ => todo!("non-color given to builtin function `complement()`")
};
Some(Value::Color(color.complement()))
});
decl!(f "invert", |args, _| { decl!(f "invert", |args, _| {
let weight = match arg!(args, 1, "weight"=Value::Dimension(Number::from(100), Unit::Percent)) { let weight = match arg!(args, 1, "weight"=Value::Dimension(Number::from(100), Unit::Percent)) {
Value::Dimension(n, Unit::None) Value::Dimension(n, Unit::None)

View File

@ -340,6 +340,16 @@ impl Color {
repr, repr,
} }
} }
pub fn complement(&self) -> Self {
let (hue, saturation, luminance, alpha) = self.as_hsla();
let hue = if hue > Number::from(180) {
Number::from(360) - hue
} else {
hue + Number::from(180)
};
Color::from_hsla(hue, saturation, luminance, alpha)
}
} }
/// Get the proper representation from RGBA values /// Get the proper representation from RGBA values

View File

@ -351,3 +351,8 @@ test!(
"a {\n color: grayscale(red);\n}\n", "a {\n color: grayscale(red);\n}\n",
"a {\n color: gray;\n}\n" "a {\n color: gray;\n}\n"
); );
test!(
complement,
"a {\n color: complement(red);\n}\n",
"a {\n color: aqua;\n}\n"
);