diff --git a/src/buffer.rs b/src/buffer.rs index 98330a8e76..b1a8d308e9 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -42,8 +42,13 @@ pub trait Pixel: Copy + Clone { Self::COLOR_MODEL } + /// ColorType for this pixel format + const COLOR_TYPE: ColorType; /// Returns the ColorType for this pixel format - fn color_type() -> ColorType; + #[deprecated(note="please use COLOR_TYPE associated constant")] + fn color_type() -> ColorType { + Self::COLOR_TYPE + } /// Returns the channels of this pixel as a 4 tuple. If the pixel /// has less than 4 channels the remainder is filled with the maximum value @@ -650,7 +655,7 @@ where FlatSamples { samples: self.data, layout, - color_hint: Some(P::color_type()), + color_hint: Some(P::COLOR_TYPE), } } @@ -664,7 +669,7 @@ where FlatSamples { samples: self.data.as_ref(), layout, - color_hint: Some(P::color_type()), + color_hint: Some(P::COLOR_TYPE), } } } @@ -758,7 +763,7 @@ where self.as_bytes(), self.width(), self.height(), -

::color_type(), +

::COLOR_TYPE, ) } } @@ -784,7 +789,7 @@ where self.as_bytes(), self.width(), self.height(), -

::color_type(), +

::COLOR_TYPE, format, ) } diff --git a/src/color.rs b/src/color.rs index dfe2030762..d1e678a6a1 100644 --- a/src/color.rs +++ b/src/color.rs @@ -181,12 +181,8 @@ impl Pixel for $ident { const COLOR_MODEL: &'static str = $interpretation; - fn color_type() -> ColorType { - match std::mem::size_of::() { - 1 => $color_type_u8, - _ => $color_type_u16, - } - } + const COLOR_TYPE: ColorType = + [$color_type_u8, $color_type_u16][(std::mem::size_of::() > 1) as usize]; #[inline(always)] fn channels(&self) -> &[T] { diff --git a/src/flat.rs b/src/flat.rs index 994e5de0d3..316c2564d8 100644 --- a/src/flat.rs +++ b/src/flat.rs @@ -560,7 +560,7 @@ impl FlatSamples { where P: Pixel, Buffer: AsRef<[P::Subpixel]>, { if self.layout.channels != P::CHANNEL_COUNT { - return Err(Error::WrongColor(P::color_type())) + return Err(Error::WrongColor(P::COLOR_TYPE)) } let as_ref = self.samples.as_ref(); @@ -597,7 +597,7 @@ impl FlatSamples { where P: Pixel, Buffer: AsMut<[P::Subpixel]>, { if self.layout.channels != P::CHANNEL_COUNT { - return Err(Error::WrongColor(P::color_type())) + return Err(Error::WrongColor(P::COLOR_TYPE)) } let as_mut = self.samples.as_mut(); @@ -634,7 +634,7 @@ impl FlatSamples { } if self.layout.channels != P::CHANNEL_COUNT { - return Err(Error::WrongColor(P::color_type())) + return Err(Error::WrongColor(P::COLOR_TYPE)) } let as_mut = self.samples.as_mut(); @@ -718,7 +718,7 @@ impl FlatSamples { } if self.layout.channels != P::CHANNEL_COUNT { - return Err((Error::WrongColor(P::color_type()), self)) + return Err((Error::WrongColor(P::COLOR_TYPE), self)) } if !self.fits(self.samples.deref().len()) {