Add generic to_channel method

This commit is contained in:
Brendan Zabarauskas 2013-07-10 10:37:59 +10:00
parent c4728af209
commit 7e70e826f2
3 changed files with 25 additions and 30 deletions

View file

@ -15,7 +15,7 @@
pub trait Channel: Num {
pub fn from<T:Channel>(chan: T) -> Self;
pub fn to_channel<T:Channel>(&self) -> T;
pub fn to_channel_u8(&self) -> u8;
pub fn to_channel_u16(&self) -> u16;
pub fn to_channel_f32(&self) -> f32;
@ -24,7 +24,7 @@ pub trait Channel: Num {
impl Channel for u8 {
#[inline] pub fn from<T:Channel>(chan: T) -> u8 { chan.to_channel_u8() }
#[inlune] pub fn to_channel<T:Channel>(&self) -> T { Channel::from(*self) }
#[inline] pub fn to_channel_u8(&self) -> u8 { (*self) }
#[inline] pub fn to_channel_u16(&self) -> u16 { (*self as u16 << 8) | (*self) as u16 }
#[inline] pub fn to_channel_f32(&self) -> f32 { (*self as f32) / (0xFF as f32) }
@ -33,7 +33,7 @@ impl Channel for u8 {
impl Channel for u16 {
#[inline] pub fn from<T:Channel>(chan: T) -> u16 { chan.to_channel_u16() }
#[inlune] pub fn to_channel<T:Channel>(&self) -> T { Channel::from(*self) }
#[inline] pub fn to_channel_u8(&self) -> u8 { (*self >> 8) as u8 }
#[inline] pub fn to_channel_u16(&self) -> u16 { (*self) }
#[inline] pub fn to_channel_f32(&self) -> f32 { (*self) / 0xFFFF as f32 }
@ -42,7 +42,7 @@ impl Channel for u16 {
impl Channel for f32 {
#[inline] pub fn from<T:Channel>(chan: T) -> f32 { chan.to_channel_f32() }
#[inlune] pub fn to_channel<T:Channel>(&self) -> T { Channel::from(*self) }
#[inline] pub fn to_channel_u8(&self) -> u8 { (*self) * (0xFF_u8 as f32) as u8 }
#[inline] pub fn to_channel_u16(&self) -> u16 { (*self) * (0xFFFF_u16 as f32) as u16 }
#[inline] pub fn to_channel_f32(&self) -> f32 { (*self) }
@ -51,7 +51,7 @@ impl Channel for f32 {
impl Channel for f64 {
#[inline] pub fn from<T:Channel>(chan: T) -> f64 { chan.to_channel_f64() }
#[inlune] pub fn to_channel<T:Channel>(&self) -> T { Channel::from(*self) }
#[inline] pub fn to_channel_u8(&self) -> u8 { (*self) * (0xFF_u8 as f64) as u8 }
#[inline] pub fn to_channel_u16(&self) -> u16 { (*self) * (0xFFFF_u16 as f64) as u16 }
#[inline] pub fn to_channel_f32(&self) -> f32 { (*self) as f32 }
@ -118,9 +118,11 @@ mod tests {
assert_eq!(0.75f32.to_channel_u16(), 0xBFFF);
assert_eq!(1.00f32.to_channel_u16(), 0xFFFF);
// TODO: test to_channel_f32()
assert_eq!(0.00f32.to_channel_f32(), 0.00f32);
assert_eq!(1.00f32.to_channel_f32(), 1.00f32);
// TODO: test to_channel_f64()
assert_eq!(0.00f32.to_channel_f64(), 0.00f64);
assert_eq!(1.00f32.to_channel_f64(), 1.00f64);
}
#[test]
@ -137,8 +139,10 @@ mod tests {
assert_eq!(0.75f64.to_channel_u16(), 0xBFFF);
assert_eq!(1.00f64.to_channel_u16(), 0xFFFF);
// TODO: test to_channel_f32()
assert_eq!(0.00f64.to_channel_f32(), 0.00f32);
assert_eq!(1.00f64.to_channel_f32(), 1.00f32);
// TODO: test to_channel_f64()
assert_eq!(0.00f64.to_channel_f64(), 0.00f64);
assert_eq!(1.00f64.to_channel_f64(), 1.00f64);
}
}

View file

@ -38,9 +38,9 @@ pub trait ToHSV {
impl<T:Clone + Channel + Float> ToHSV for HSV<T> {
#[inline]
pub fn to_hsv<U:Channel + Float>(&self) -> HSV<U> {
HSV::new(Channel::from((*self).h.clone()),
Channel::from((*self).s.clone()),
Channel::from((*self).v.clone()))
HSV::new((*self).h.to_channel(),
(*self).s.to_channel(),
(*self).v.to_channel())
}
}
@ -110,10 +110,7 @@ impl<C: ToHSV, T:Clone + Channel + Float> ToHSVA for (C, T) {
pub fn to_hsva<U:Channel + Float>(&self) -> HSVA<U> {
match *self {
(ref hsv, ref a) => {
HSVA::from_hsv_a(
hsv.to_hsv(),
Channel::from(a.clone())
)
HSVA::from_hsv_a(hsv.to_hsv(), a.to_channel())
}
}
}
@ -122,9 +119,6 @@ impl<C: ToHSV, T:Clone + Channel + Float> ToHSVA for (C, T) {
impl<T:Clone + Channel + Float> ToRGBA for HSVA<T> {
#[inline]
pub fn to_rgba<U:Channel>(&self) -> RGBA<U> {
RGBA::from_rgb_a(
self.hsv().to_rgb(),
Channel::from((*self).a.clone())
)
RGBA::from_rgb_a(self.hsv().to_rgb(), (*self).a.to_channel())
}
}

View file

@ -39,9 +39,9 @@ pub trait ToRGB {
impl<T:Clone + Channel> ToRGB for RGB<T> {
#[inline]
pub fn to_rgb<U:Channel>(&self) -> RGB<U> {
RGB::new(Channel::from((*self).r.clone()),
Channel::from((*self).g.clone()),
Channel::from((*self).b.clone()))
RGB::new((*self).r.to_channel(),
(*self).g.to_channel(),
(*self).b.to_channel())
}
}
@ -108,7 +108,7 @@ impl<C: ToRGB, T:Clone + Channel> ToRGBA for (C, T) {
pub fn to_rgba<U:Channel>(&self) -> RGBA<U> {
match *self {
(ref rgb, ref a) => {
RGBA::from_rgb_a(rgb.to_rgb(), Channel::from(a.clone()))
RGBA::from_rgb_a(rgb.to_rgb(), a.to_channel())
}
}
}
@ -117,10 +117,7 @@ impl<C: ToRGB, T:Clone + Channel> ToRGBA for (C, T) {
impl<T:Clone + Channel> ToHSVA for RGBA<T> {
#[inline]
pub fn to_hsva<U:Channel + Float>(&self) -> HSVA<U> {
HSVA::from_hsv_a(
self.rgb().to_hsv(),
Channel::from((*self).a.clone())
)
HSVA::from_hsv_a(self.rgb().to_hsv(), (*self).a.to_channel())
}
}