Add channel and color normalization methods

This commit is contained in:
Brendan Zabarauskas 2013-07-10 14:16:16 +10:00
parent 58c3233fe3
commit e12f561930
4 changed files with 49 additions and 4 deletions

View file

@ -70,14 +70,17 @@ impl Channel for f64 {
pub trait FloatChannel: Float + Channel { pub trait FloatChannel: Float + Channel {
pub fn invert_degrees(&self) -> Self; pub fn invert_degrees(&self) -> Self;
pub fn normalize_degrees(&self) -> Self;
} }
impl FloatChannel for f32 { impl FloatChannel for f32 {
#[inline] pub fn invert_degrees(&self) -> f32 { ((*self) + 180.0) % 360.0 } #[inline] pub fn invert_degrees(&self) -> f32 { ((*self) + 180.0).normalize_degrees() }
#[inline] pub fn normalize_degrees(&self) -> f32 { (*self) % 360.0 }
} }
impl FloatChannel for f64 { impl FloatChannel for f64 {
#[inline] pub fn invert_degrees(&self) -> f64 { ((*self) + 180.0) % 360.0 } #[inline] pub fn invert_degrees(&self) -> f64 { ((*self) + 180.0).normalize_degrees() }
#[inline] pub fn normalize_degrees(&self) -> f64 { (*self) % 360.0 }
} }
#[cfg(test)] #[cfg(test)]

View file

@ -27,3 +27,7 @@ pub trait Color<T> {
pub fn clamp(&self, lo: T, hi: T) -> Self; pub fn clamp(&self, lo: T, hi: T) -> Self;
pub fn inverse(&self) -> Self; pub fn inverse(&self) -> Self;
} }
pub trait FloatColor<T>: Color<T> {
pub fn normalize(&self) -> Self;
}

View file

@ -16,7 +16,7 @@
use std::num; use std::num;
use std::cast; use std::cast;
use color::Color; use color::{Color, FloatColor};
use color::{Channel, FloatChannel}; use color::{Channel, FloatChannel};
use color::{RGB, ToRGB, RGBA, ToRGBA}; use color::{RGB, ToRGB, RGBA, ToRGBA};
@ -48,6 +48,15 @@ impl<T:FloatChannel> Color<T> for HSV<T> {
} }
} }
impl<T:FloatChannel> FloatColor<T> for HSV<T> {
#[inline]
pub fn normalize(&self) -> HSV<T> {
HSV::new((*self).h.normalize_degrees(),
(*self).s.clamp(&zero!(T), &one!(T)),
(*self).v.clamp(&zero!(T), &one!(T)))
}
}
pub trait ToHSV { pub trait ToHSV {
pub fn to_hsv<U:FloatChannel>(&self) -> HSV<U>; pub fn to_hsv<U:FloatChannel>(&self) -> HSV<U>;
} }
@ -150,6 +159,16 @@ impl<T:FloatChannel> Color<T> for HSVA<T> {
} }
} }
impl<T:FloatChannel> FloatColor<T> for HSVA<T> {
#[inline]
pub fn normalize(&self) -> HSVA<T> {
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)))
}
}
pub trait ToHSVA { pub trait ToHSVA {
pub fn to_hsva<U:FloatChannel>(&self) -> HSVA<U>; pub fn to_hsva<U:FloatChannel>(&self) -> HSVA<U>;
} }

View file

@ -16,7 +16,7 @@
use std::num; use std::num;
use std::cast; use std::cast;
use color::Color; use color::{Color, FloatColor};
use color::{Channel, FloatChannel}; use color::{Channel, FloatChannel};
use color::{HSV, ToHSV, HSVA, ToHSVA}; use color::{HSV, ToHSV, HSVA, ToHSVA};
@ -49,6 +49,15 @@ impl<T:Channel> Color<T> for RGB<T> {
} }
} }
impl<T:FloatChannel> FloatColor<T> for RGB<T> {
#[inline]
pub fn normalize(&self) -> RGB<T> {
RGB::new((*self).r.clamp(&zero!(T), &one!(T)),
(*self).g.clamp(&zero!(T), &one!(T)),
(*self).b.clamp(&zero!(T), &one!(T)))
}
}
pub trait ToRGB { pub trait ToRGB {
pub fn to_rgb<U:Channel>(&self) -> RGB<U>; pub fn to_rgb<U:Channel>(&self) -> RGB<U>;
} }
@ -148,6 +157,16 @@ impl<T:Channel> Color<T> for RGBA<T> {
} }
} }
impl<T:FloatChannel> FloatColor<T> for RGBA<T> {
#[inline]
pub fn normalize(&self) -> RGBA<T> {
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)))
}
}
pub trait ToRGBA { pub trait ToRGBA {
pub fn to_rgba<U:Channel>(&self) -> RGBA<U>; pub fn to_rgba<U:Channel>(&self) -> RGBA<U>;
} }