Add normalize_channel method

This commit is contained in:
Brendan Zabarauskas 2013-07-11 12:56:35 +10:00
parent 659226b15e
commit fd9fcd2075
3 changed files with 18 additions and 15 deletions

View file

@ -71,18 +71,21 @@ impl Channel for f64 {
} }
pub trait FloatChannel: Float + Channel { 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 normalize_degrees(&self) -> Self;
pub fn invert_degrees(&self) -> Self;
} }
impl FloatChannel for f32 { 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 normalize_degrees(&self) -> f32 { (*self) % 360.0 }
#[inline] pub fn invert_degrees(&self) -> f32 { ((*self) + 180.0).normalize_degrees() }
} }
impl FloatChannel for f64 { 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 normalize_degrees(&self) -> f64 { (*self) % 360.0 }
#[inline] pub fn invert_degrees(&self) -> f64 { ((*self) + 180.0).normalize_degrees() }
} }
#[cfg(test)] #[cfg(test)]

View file

@ -56,8 +56,8 @@ impl<T:FloatChannel> FloatColor<T> for HSV<T> {
#[inline] #[inline]
pub fn normalize(&self) -> HSV<T> { pub fn normalize(&self) -> HSV<T> {
HSV::new((*self).h.normalize_degrees(), HSV::new((*self).h.normalize_degrees(),
(*self).s.clamp(&zero!(T), &one!(T)), (*self).s.normalize_channel(),
(*self).v.clamp(&zero!(T), &one!(T))) (*self).v.normalize_channel())
} }
} }
@ -171,9 +171,9 @@ impl<T:FloatChannel> FloatColor<T> for HSVA<T> {
#[inline] #[inline]
pub fn normalize(&self) -> HSVA<T> { pub fn normalize(&self) -> HSVA<T> {
HSVA::new((*self).h.normalize_degrees(), HSVA::new((*self).h.normalize_degrees(),
(*self).s.clamp(&zero!(T), &one!(T)), (*self).s.normalize_channel(),
(*self).v.clamp(&zero!(T), &one!(T)), (*self).v.normalize_channel(),
(*self).a.clamp(&zero!(T), &one!(T))) (*self).a.normalize_channel())
} }
} }

View file

@ -55,9 +55,9 @@ impl<T:FloatChannel> FloatColor<T> for RGB<T> {
/// Normalizes the components of the color by clamping them to the range `(0,1)`. /// Normalizes the components of the color by clamping them to the range `(0,1)`.
#[inline] #[inline]
pub fn normalize(&self) -> RGB<T> { pub fn normalize(&self) -> RGB<T> {
RGB::new((*self).r.clamp(&zero!(T), &one!(T)), RGB::new((*self).r.normalize_channel(),
(*self).g.clamp(&zero!(T), &one!(T)), (*self).g.normalize_channel(),
(*self).b.clamp(&zero!(T), &one!(T))) (*self).b.normalize_channel())
} }
} }
@ -166,10 +166,10 @@ impl<T:FloatChannel> FloatColor<T> for RGBA<T> {
/// Normalizes the components of the color by clamping them to the range `(0,1)`. /// Normalizes the components of the color by clamping them to the range `(0,1)`.
#[inline] #[inline]
pub fn normalize(&self) -> RGBA<T> { pub fn normalize(&self) -> RGBA<T> {
RGBA::new((*self).r.clamp(&zero!(T), &one!(T)), RGBA::new((*self).r.normalize_channel(),
(*self).g.clamp(&zero!(T), &one!(T)), (*self).g.normalize_channel(),
(*self).b.clamp(&zero!(T), &one!(T)), (*self).b.normalize_channel(),
(*self).a.clamp(&zero!(T), &one!(T))) (*self).a.normalize_channel())
} }
} }