From 4d989b60b169d32e83c4ca636130bab3a4a37df2 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Wed, 22 Apr 2020 06:05:19 -0400 Subject: [PATCH] remove final clamp! macro --- src/color/mod.rs | 34 ++++++++++------------------------ src/value/number.rs | 6 +++++- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/color/mod.rs b/src/color/mod.rs index f81b665..e9fbc3f 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -132,30 +132,16 @@ impl Color { /// Create a new `Color` with just RGBA values. /// Color representation is created automatically. - pub fn from_rgba(red: Number, green: Number, blue: Number, alpha: Number) -> Self { - macro_rules! clamp { - ($channel:ident) => { - let $channel = if $channel > Number::from(255) { - Number::from(255) - } else if $channel.is_negative() { - Number::zero() - } else { - $channel - }; - }; - } - - clamp!(red); - clamp!(green); - clamp!(blue); - - let alpha = if alpha > Number::one() { - Number::one() - } else if alpha.is_negative() { - Number::zero() - } else { - alpha - }; + pub fn from_rgba( + mut red: Number, + mut green: Number, + mut blue: Number, + mut alpha: Number, + ) -> Self { + red = red.clamp(0, 255); + green = green.clamp(0, 255); + blue = blue.clamp(0, 255); + alpha = alpha.clamp(0, 1); let repr = repr(&red, &green, &blue, &alpha); Color::new_rgba(red, green, blue, alpha, repr) diff --git a/src/value/number.rs b/src/value/number.rs index 85e3bd4..58276a6 100644 --- a/src/value/number.rs +++ b/src/value/number.rs @@ -58,12 +58,16 @@ impl Number { self.val.denom() != &BigInt::from(1) } - pub fn clamp, B: Into>(self, min: A, max: B) -> Self { + pub fn clamp + Zero, B: Into>(self, min: A, max: B) -> Self { let max = max.into(); if self > max { return max; } + if min.is_zero() && self.is_negative() { + return Number::zero(); + } + let min = min.into(); if self < min { return min;