diff --git a/src/builtin/color/hsl.rs b/src/builtin/color/hsl.rs index 85dbfde..94df868 100644 --- a/src/builtin/color/hsl.rs +++ b/src/builtin/color/hsl.rs @@ -88,7 +88,7 @@ pub(crate) fn register(f: &mut BTreeMap) { v => return Err(format!("$color: {} is not a color.", v).into()), }; let amount = match arg!(args, 1, "amount").eval() { - Value::Dimension(n, _) => n / Number::from(100), + Value::Dimension(n, u) => bound!(n, u, 0, 100) / Number::from(100), v => return Err(format!("$amount: {} is not a number.", v).into()) }; Ok(Value::Color(color.lighten(amount))) diff --git a/src/builtin/macros.rs b/src/builtin/macros.rs index cd1f8c6..c2040a2 100644 --- a/src/builtin/macros.rs +++ b/src/builtin/macros.rs @@ -46,3 +46,17 @@ macro_rules! max_args { } }; } + +macro_rules! bound { + ($arg:ident, $unit:ident, $low:literal, $high:literal) => { + if $arg > Number::from($high) || $arg < Number::from($low) { + return Err(format!( + "Expected {}{} to be within {}{} and {}{}.", + $arg, $unit, $low, $unit, $high, $unit, + ) + .into()); + } else { + $arg + } + }; +} diff --git a/src/color/mod.rs b/src/color/mod.rs index 48e6cc9..8bc907d 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -232,7 +232,7 @@ impl Color { /// Calculate saturation from RGBA values pub fn saturation(&self) -> Number { if let Some(h) = &self.hsla { - return h.saturation() * Number::from(100); + return h.saturation() * Number::from(100); } let red = self.red() / Number::from(255);