diff --git a/src/builtin/color_hsl.rs b/src/builtin/color_hsl.rs index a322ebb..a6f579c 100644 --- a/src/builtin/color_hsl.rs +++ b/src/builtin/color_hsl.rs @@ -135,6 +135,13 @@ pub(crate) fn register(f: &mut BTreeMap) { }; 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, _| { let weight = match arg!(args, 1, "weight"=Value::Dimension(Number::from(100), Unit::Percent)) { Value::Dimension(n, Unit::None) diff --git a/src/color/mod.rs b/src/color/mod.rs index 29dac18..224ac8d 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -340,6 +340,16 @@ impl Color { 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 diff --git a/tests/color.rs b/tests/color.rs index 7f2dce7..b47c5b1 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -351,3 +351,8 @@ test!( "a {\n color: grayscale(red);\n}\n", "a {\n color: gray;\n}\n" ); +test!( + complement, + "a {\n color: complement(red);\n}\n", + "a {\n color: aqua;\n}\n" +);