From 92ee628c066132b9e3a54040cf064ca861953cab Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 11 Jul 2013 13:05:29 +1000 Subject: [PATCH] Fix normalize_degrees impls --- src/color/channel.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/color/channel.rs b/src/color/channel.rs index dc6c969..8db4b17 100644 --- a/src/color/channel.rs +++ b/src/color/channel.rs @@ -77,15 +77,39 @@ pub trait FloatChannel: Float + Channel { } impl FloatChannel for f32 { - #[inline] pub fn normalize_channel(&self) -> f32 { self.clamp(&0.0, &1.0) } - #[inline] pub fn normalize_degrees(&self) -> f32 { (*self) % 360.0 } - #[inline] pub fn invert_degrees(&self) -> f32 { ((*self) + 180.0).normalize_degrees() } + #[inline] pub fn normalize_channel(&self) -> f32 { + self.clamp(&0.0, &1.0) + } + + #[inline] pub fn normalize_degrees(&self) -> f32 { + if (*self) < 0.0 { + (*self + 360.0) % 360.0 + } else { + *self % 360.0 + } + } + + #[inline] pub fn invert_degrees(&self) -> f32 { + (*self + 180.0).normalize_degrees() + } } impl FloatChannel for f64 { - #[inline] pub fn normalize_channel(&self) -> f64 { self.clamp(&0.0, &1.0) } - #[inline] pub fn normalize_degrees(&self) -> f64 { (*self) % 360.0 } - #[inline] pub fn invert_degrees(&self) -> f64 { ((*self) + 180.0).normalize_degrees() } + #[inline] pub fn normalize_channel(&self) -> f64 { + self.clamp(&0.0, &1.0) + } + + #[inline] pub fn normalize_degrees(&self) -> f64 { + if (*self) < 0.0 { + (*self + 360.0) % 360.0 + } else { + *self % 360.0 + } + } + + #[inline] pub fn invert_degrees(&self) -> f64 { + (*self + 180.0).normalize_degrees() + } } #[cfg(test)]