Remove free standing color conversion functions

This commit is contained in:
Brendan Zabarauskas 2013-07-09 16:53:24 +10:00
parent afaa3af501
commit b9369247bf
2 changed files with 51 additions and 57 deletions

View file

@ -45,21 +45,16 @@ impl<T:Clone + FloatChannel> ToHSV for HSV<T> {
impl<T:Clone + FloatChannel> ToRGB for HSV<T> { impl<T:Clone + FloatChannel> ToRGB for HSV<T> {
pub fn to_rgb<U:Clone + Channel>(&self) -> RGB<U> { pub fn to_rgb<U:Clone + Channel>(&self) -> RGB<U> {
to_rgb(self.to_hsv::<T>()).to_rgb::<U>()
}
}
priv fn to_rgb<T:Clone + Float>(color: HSV<T>) -> RGB<T> {
// Algorithm taken from the Wikipedia article on HSL and HSV: // Algorithm taken from the Wikipedia article on HSL and HSV:
// http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
let chr = color.v * color.s; let chr = (*self).v * (*self).s;
let h = color.h / num::cast(60); let h = (*self).h / num::cast(60);
// the 2nd largest component // the 2nd largest component
let x = chr * (one!(T) - ((h % two!(T)) - one!(T)).abs()); let x = chr * (one!(T) - ((h % two!(T)) - one!(T)).abs());
let mut color_rgb = cond! ( let mut rgb = cond! (
(h < num::cast(1)) { RGB::new(chr.clone(), x, zero!(T)) } (h < num::cast(1)) { RGB::new(chr.clone(), x, zero!(T)) }
(h < num::cast(2)) { RGB::new(x, chr.clone(), zero!(T)) } (h < num::cast(2)) { RGB::new(x, chr.clone(), zero!(T)) }
(h < num::cast(3)) { RGB::new(zero!(T), chr.clone(), x) } (h < num::cast(3)) { RGB::new(zero!(T), chr.clone(), x) }
@ -70,11 +65,12 @@ priv fn to_rgb<T:Clone + Float>(color: HSV<T>) -> RGB<T> {
); );
// match the value by adding the same amount to each component // match the value by adding the same amount to each component
let mn = color.v - chr; let mn = (*self).v - chr;
color_rgb.r = color_rgb.r + mn; rgb.r = rgb.r + mn;
color_rgb.g = color_rgb.g + mn; rgb.g = rgb.g + mn;
color_rgb.b = color_rgb.b + mn; rgb.b = rgb.b + mn;
color_rgb rgb.to_rgb::<U>()
}
} }

View file

@ -44,26 +44,23 @@ impl<T:Clone + Channel> ToRGB for RGB<T> {
} }
} }
impl<T:Clone + FloatChannel> ToHSV for RGB<T> { impl<T:Clone + Channel> ToHSV for RGB<T> {
#[inline] #[inline]
pub fn to_hsv<U:Clone + FloatChannel>(&self) -> HSV<U> { pub fn to_hsv<U:Clone + FloatChannel>(&self) -> HSV<U> {
to_hsv(self.to_rgb::<U>())
}
}
priv fn to_hsv<T:Clone + Float>(color: RGB<T>) -> HSV<T> {
// Algorithm taken from the Wikipedia article on HSL and HSV: // Algorithm taken from the Wikipedia article on HSL and HSV:
// http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
let mx = color.r.max(&color.g).max(&color.b); let rgb_u = self.to_rgb::<U>();
let mn = color.r.min(&color.g).min(&color.b);
let mx = rgb_u.r.max(&rgb_u.g).max(&rgb_u.b);
let mn = rgb_u.r.min(&rgb_u.g).min(&rgb_u.b);
let chr = mx - mn; let chr = mx - mn;
if chr != zero!(T) { if chr != zero!(U) {
let h = cond! ( let h = cond! (
(color.r == mx) { ((color.g - color.b) / chr) % num::cast(6) } (rgb_u.r == mx) { ((rgb_u.g - rgb_u.b) / chr) % num::cast(6) }
(color.g == mx) { ((color.b - color.r) / chr) + num::cast(2) } (rgb_u.g == mx) { ((rgb_u.b - rgb_u.r) / chr) + num::cast(2) }
_ /* color.b == mx */ { ((color.r - color.g) / chr) + num::cast(4) } _ /* rgb_u.b == mx */ { ((rgb_u.r - rgb_u.g) / chr) + num::cast(4) }
) * num::cast(60); ) * num::cast(60);
let s = chr / mx; let s = chr / mx;
@ -71,6 +68,7 @@ priv fn to_hsv<T:Clone + Float>(color: RGB<T>) -> HSV<T> {
HSV::new(h, s, mx) HSV::new(h, s, mx)
} else { } else {
HSV::new(zero!(T), zero!(T), mx) HSV::new(zero!(U), zero!(U), mx)
}
} }
} }