From 19f59efd9839c49d39a48fa01a719db475f0f7ba Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 14 Feb 2020 20:13:58 -0500 Subject: [PATCH] Handle alpha over 1 (a little bit) --- src/builtin/color/hsl.rs | 2 +- src/builtin/color/opacity.rs | 4 ++-- src/color/mod.rs | 8 +++++++- tests/color.rs | 5 +++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/builtin/color/hsl.rs b/src/builtin/color/hsl.rs index c26bee4..ab51c8f 100644 --- a/src/builtin/color/hsl.rs +++ b/src/builtin/color/hsl.rs @@ -150,7 +150,7 @@ pub(crate) fn register(f: &mut BTreeMap) { }; match arg!(args, 0, "color") { Value::Color(c) => Some(Value::Color(c.invert(weight))), - _ => todo!("non-color given to builtin function `alpha()`") + _ => todo!("non-color given to builtin function `invert()`") } }); } diff --git a/src/builtin/color/opacity.rs b/src/builtin/color/opacity.rs index ba466af..3755c40 100644 --- a/src/builtin/color/opacity.rs +++ b/src/builtin/color/opacity.rs @@ -8,13 +8,13 @@ use crate::value::{Number, Value}; pub(crate) fn register(f: &mut BTreeMap) { decl!(f "alpha", |args, _| { match arg!(args, 0, "color") { - Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)), + Value::Color(c) => Some(Value::Dimension(c.alpha(), Unit::None)), _ => todo!("non-color given to builtin function `alpha()`") } }); decl!(f "opacity", |args, _| { match arg!(args, 0, "color") { - Value::Color(c) => Some(Value::Dimension(c.alpha() / Number::from(255), Unit::None)), + Value::Color(c) => Some(Value::Dimension(c.alpha(), Unit::None)), Value::Dimension(num, unit) => Some(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)), _ => todo!("non-color given to builtin function `opacity()`") } diff --git a/src/color/mod.rs b/src/color/mod.rs index 753bb64..7ae5ec0 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -255,6 +255,7 @@ impl Color { } else { hue }; + let saturation = clamp!(saturation, 0, 1); let luminance = clamp!(luminance, 0, 1); let alpha = clamp!(alpha, 0, 1); @@ -367,7 +368,12 @@ impl Color { /// Opacity color functions impl Color { pub fn alpha(&self) -> Number { - self.alpha.clone() + let a = self.alpha.clone(); + if a > Number::from(1) { + a / Number::from(255) + } else { + a + } } /// Change `alpha` to value given diff --git a/tests/color.rs b/tests/color.rs index 71bfca3..c59b689 100644 --- a/tests/color.rs +++ b/tests/color.rs @@ -436,3 +436,8 @@ test!( "a {\n color: scale-color(hsl(200, 70%, 80%), $saturation: -90%, $alpha: -30%);\n}\n", "a {\n color: rgba(200, 205, 208, 0.7);\n}\n" ); +test!( + scale_color_alpha_over_1, + "a {\n color: scale-color(sienna, $alpha: -70%);\n}\n", + "a {\n color: rgba(160, 82, 45, 0.3);\n}\n" +);