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,36 +45,32 @@ 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>() // Algorithm taken from the Wikipedia article on HSL and HSV:
// http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
let chr = (*self).v * (*self).s;
let h = (*self).h / num::cast(60);
// the 2nd largest component
let x = chr * (one!(T) - ((h % two!(T)) - one!(T)).abs());
let mut rgb = cond! (
(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(3)) { RGB::new(zero!(T), chr.clone(), x) }
(h < num::cast(4)) { RGB::new(zero!(T), x, chr.clone()) }
(h < num::cast(5)) { RGB::new(x, zero!(T), chr.clone()) }
(h < num::cast(6)) { RGB::new(chr.clone(), zero!(T), x) }
_ { RGB::new(zero!(T), zero!(T), zero!(T)) }
);
// match the value by adding the same amount to each component
let mn = (*self).v - chr;
rgb.r = rgb.r + mn;
rgb.g = rgb.g + mn;
rgb.b = rgb.b + mn;
rgb.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:
// http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
let chr = color.v * color.s;
let h = color.h / num::cast(60);
// the 2nd largest component
let x = chr * (one!(T) - ((h % two!(T)) - one!(T)).abs());
let mut color_rgb = cond! (
(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(3)) { RGB::new(zero!(T), chr.clone(), x) }
(h < num::cast(4)) { RGB::new(zero!(T), x, chr.clone()) }
(h < num::cast(5)) { RGB::new(x, zero!(T), chr.clone()) }
(h < num::cast(6)) { RGB::new(chr.clone(), zero!(T), x) }
_ { RGB::new(zero!(T), zero!(T), zero!(T)) }
);
// match the value by adding the same amount to each component
let mn = color.v - chr;
color_rgb.r = color_rgb.r + mn;
color_rgb.g = color_rgb.g + mn;
color_rgb.b = color_rgb.b + mn;
color_rgb
}

View file

@ -44,33 +44,31 @@ 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>()) // Algorithm taken from the Wikipedia article on HSL and HSV:
} // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
}
let rgb_u = 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: let mx = rgb_u.r.max(&rgb_u.g).max(&rgb_u.b);
// http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV let mn = rgb_u.r.min(&rgb_u.g).min(&rgb_u.b);
let chr = mx - mn;
let mx = color.r.max(&color.g).max(&color.b);
let mn = color.r.min(&color.g).min(&color.b); if chr != zero!(U) {
let chr = mx - mn; let h = cond! (
(rgb_u.r == mx) { ((rgb_u.g - rgb_u.b) / chr) % num::cast(6) }
if chr != zero!(T) { (rgb_u.g == mx) { ((rgb_u.b - rgb_u.r) / chr) + num::cast(2) }
let h = cond! ( _ /* rgb_u.b == mx */ { ((rgb_u.r - rgb_u.g) / chr) + num::cast(4) }
(color.r == mx) { ((color.g - color.b) / chr) % num::cast(6) } ) * num::cast(60);
(color.g == mx) { ((color.b - color.r) / chr) + num::cast(2) }
_ /* color.b == mx */ { ((color.r - color.g) / chr) + num::cast(4) } let s = chr / mx;
) * num::cast(60);
HSV::new(h, s, mx)
let s = chr / mx;
} else {
HSV::new(h, s, mx) HSV::new(zero!(U), zero!(U), mx)
}
} else {
HSV::new(zero!(T), zero!(T), mx)
} }
} }