From fd9fcd20756b7797f97b6c4bffb0becc4ed67665 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 11 Jul 2013 12:56:35 +1000 Subject: [PATCH] Add normalize_channel method --- src/color/channel.rs | 9 ++++++--- src/color/hsv.rs | 10 +++++----- src/color/rgb.rs | 14 +++++++------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/color/channel.rs b/src/color/channel.rs index dd64fdc..dc6c969 100644 --- a/src/color/channel.rs +++ b/src/color/channel.rs @@ -71,18 +71,21 @@ impl Channel for f64 { } pub trait FloatChannel: Float + Channel { - pub fn invert_degrees(&self) -> Self; + pub fn normalize_channel(&self) -> Self; pub fn normalize_degrees(&self) -> Self; + pub fn invert_degrees(&self) -> Self; } impl FloatChannel for f32 { - #[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 { (*self) % 360.0 } + #[inline] pub fn invert_degrees(&self) -> f32 { ((*self) + 180.0).normalize_degrees() } } impl FloatChannel for f64 { - #[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 { (*self) % 360.0 } + #[inline] pub fn invert_degrees(&self) -> f64 { ((*self) + 180.0).normalize_degrees() } } #[cfg(test)] diff --git a/src/color/hsv.rs b/src/color/hsv.rs index 89804fd..c5cd5d0 100644 --- a/src/color/hsv.rs +++ b/src/color/hsv.rs @@ -56,8 +56,8 @@ impl FloatColor for HSV { #[inline] pub fn normalize(&self) -> HSV { HSV::new((*self).h.normalize_degrees(), - (*self).s.clamp(&zero!(T), &one!(T)), - (*self).v.clamp(&zero!(T), &one!(T))) + (*self).s.normalize_channel(), + (*self).v.normalize_channel()) } } @@ -171,9 +171,9 @@ impl FloatColor for HSVA { #[inline] pub fn normalize(&self) -> HSVA { HSVA::new((*self).h.normalize_degrees(), - (*self).s.clamp(&zero!(T), &one!(T)), - (*self).v.clamp(&zero!(T), &one!(T)), - (*self).a.clamp(&zero!(T), &one!(T))) + (*self).s.normalize_channel(), + (*self).v.normalize_channel(), + (*self).a.normalize_channel()) } } diff --git a/src/color/rgb.rs b/src/color/rgb.rs index e06bb67..f4e0639 100644 --- a/src/color/rgb.rs +++ b/src/color/rgb.rs @@ -55,9 +55,9 @@ impl FloatColor for RGB { /// Normalizes the components of the color by clamping them to the range `(0,1)`. #[inline] pub fn normalize(&self) -> RGB { - RGB::new((*self).r.clamp(&zero!(T), &one!(T)), - (*self).g.clamp(&zero!(T), &one!(T)), - (*self).b.clamp(&zero!(T), &one!(T))) + RGB::new((*self).r.normalize_channel(), + (*self).g.normalize_channel(), + (*self).b.normalize_channel()) } } @@ -166,10 +166,10 @@ impl FloatColor for RGBA { /// Normalizes the components of the color by clamping them to the range `(0,1)`. #[inline] pub fn normalize(&self) -> RGBA { - RGBA::new((*self).r.clamp(&zero!(T), &one!(T)), - (*self).g.clamp(&zero!(T), &one!(T)), - (*self).b.clamp(&zero!(T), &one!(T)), - (*self).a.clamp(&zero!(T), &one!(T))) + RGBA::new((*self).r.normalize_channel(), + (*self).g.normalize_channel(), + (*self).b.normalize_channel(), + (*self).a.normalize_channel()) } }