#[allow(deprecated)] use super::pixel::ComponentSlice; use super::pixel::{ComponentMap, ColorComponentMap}; #[cfg(feature = "as-bytes")] use super::pixel::ComponentBytes; use crate::alt::{BGRA, ARGB, ABGR, BGR}; use crate::{RGB, RGBA}; use core::fmt; impl RGBA { #[inline(always)] /// Convenience function for creating a new pixel /// The order of arguments is R,G,B,A pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self { Self { r, g, b, a } } } impl BGRA { #[inline(always)] /// Convenience function for creating a new pixel /// Warning: The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use BGRA{} literal instead")] pub const fn new(r: T, g: T, b: T, a: T) -> Self { Self { b, g, r, a } } } /// ```rust,compile_fail /// let r = rgb::BGRA::::zeroed(); /// ``` impl BGRA { #[inline(always)] /// Convenience function for creating a new pixel /// Warning: The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use BGRA{} literal instead")] pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self { Self { b, g, r, a } } } impl ARGB { #[inline(always)] /// Convenience function for creating a new pixel /// The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use ARGB{} literal instead")] pub const fn new(r: T, g: T, b: T, a: T) -> Self { Self { a, r, g, b } } } impl ARGB { #[inline(always)] /// Convenience function for creating a new pixel /// The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use ARGB{} literal instead")] pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self { Self { a, r, g, b } } } impl ABGR { #[inline(always)] /// Convenience function for creating a new pixel /// The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use ABGR{} literal instead")] pub const fn new(r: T, g: T, b: T, a: T) -> Self { Self { a, b, g, r } } } impl ABGR { #[inline(always)] /// Convenience function for creating a new pixel /// The order of arguments is R,G,B,A #[deprecated(note = "This function has a misleading order of arguments. Use ABGR{} literal instead")] pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self { Self { a, b, g, r } } } macro_rules! impl_rgba { ($RGBA:ident) => { impl $RGBA { /// Iterate over all components (length=4) #[inline(always)] pub fn iter(&self) -> core::iter::Cloned> { #[allow(deprecated)] ComponentSlice::as_slice(self).iter().cloned() } } impl $RGBA { /// Copy RGB components out of the RGBA struct /// /// Note: you can use `.into()` to convert between other types #[inline(always)] pub fn bgr(&self) -> BGR { BGR { r: self.r.clone(), g: self.g.clone(), b: self.b.clone(), } } } impl $RGBA { /// Create new RGBA with the same alpha value, but different RGB values #[deprecated(note = "Renamed to map_colors()")] pub fn map_rgb(&self, mut f: F) -> $RGBA where F: FnMut(T) -> U, U: Clone, B: From + Clone { $RGBA { r: f(self.r), g: f(self.g), b: f(self.b), a: self.a.clone().into(), } } #[doc(hidden)] #[deprecated(note = "use .with_alpha(a) instead")] /// Create a new RGBA with the new alpha value, but same RGB values pub fn alpha(&self, a: A) -> Self { self.with_alpha(a) } #[inline(always)] /// Create a new RGBA with the new alpha value, but same RGB values pub fn with_alpha(&self, a: A) -> Self { Self { r: self.r, g: self.g, b: self.b, a } } /// Create a new RGBA with a new alpha value created by the callback. /// Allows changing of the type used for the alpha channel. #[inline] pub fn map_alpha(&self, f: F) -> $RGBA where F: FnOnce(A) -> B { $RGBA { r: self.r, g: self.g, b: self.b, a: f(self.a.clone()), } } } impl ComponentMap<$RGBA, T, B> for $RGBA { #[inline(always)] fn map(&self, mut f: F) -> $RGBA where F: FnMut(T) -> B { $RGBA { r: f(self.r), g: f(self.g), b: f(self.b), a: f(self.a), } } } impl ColorComponentMap<$RGBA, T, B> for $RGBA { #[inline(always)] fn map_colors(&self, mut f: F) -> $RGBA where F: FnMut(T) -> B { $RGBA { r: f(self.r), g: f(self.g), b: f(self.b), a: self.a, } } } #[allow(deprecated)] impl ComponentSlice for $RGBA { #[inline(always)] fn as_slice(&self) -> &[T] { unsafe { core::slice::from_raw_parts(self as *const Self as *const T, 4) } } #[inline(always)] fn as_mut_slice(&mut self) -> &mut [T] { unsafe { core::slice::from_raw_parts_mut(self as *mut Self as *mut T, 4) } } } #[allow(deprecated)] impl ComponentSlice for [$RGBA] { #[inline] fn as_slice(&self) -> &[T] { unsafe { core::slice::from_raw_parts(self.as_ptr() as *const _, self.len() * 4) } } #[inline] fn as_mut_slice(&mut self) -> &mut [T] { unsafe { core::slice::from_raw_parts_mut(self.as_mut_ptr() as *mut _, self.len() * 4) } } } #[cfg(feature = "as-bytes")] impl ComponentBytes for [$RGBA] {} }; } macro_rules! impl_alpha_conv { ($RGB:ident, $RGBA:ident) => { /// Assumes 255 is opaque impl From<$RGB> for $RGBA { #[inline(always)] fn from(other: $RGB) -> Self { Self { r: other.r, g: other.g, b: other.b, a: 0xFF, } } } /// Assumes 65535 is opaque impl From<$RGB> for $RGBA { #[inline(always)] fn from(other: $RGB) -> Self { Self { r: other.r, g: other.g, b: other.b, a: 0xFFFF, } } } }; } impl RGBA { /// Provide a mutable view of only RGB components (leaving out alpha). /// Useful to change color without changing opacity. #[inline(always)] pub fn rgb_mut(&mut self) -> &mut RGB { unsafe { &mut *(self as *mut Self).cast::>() } } } impl BGRA { /// Provide a mutable view of only RGB components (leaving out alpha). /// Useful to change color without changing opacity. #[deprecated(note = "This function will change. Use bgr_mut()")] pub fn rgb_mut(&mut self) -> &mut BGR { unsafe { &mut *(self as *mut Self).cast::>() } } /// Provide a mutable view of only RGB components (leaving out alpha). /// Useful to change color without changing opacity. #[inline(always)] pub fn bgr_mut(&mut self) -> &mut BGR { unsafe { &mut *(self as *mut Self).cast::>() } } } impl core::iter::FromIterator for RGBA { #[inline(always)] /// Takes exactly 4 elements from the iterator and creates a new instance. /// Panics if there are fewer elements in the iterator. fn from_iter>(into_iter: I) -> Self { let mut iter = into_iter.into_iter(); Self { r: iter.next().unwrap(), g: iter.next().unwrap(), b: iter.next().unwrap(), a: iter.next().unwrap(), } } } impl RGBA { /// Copy RGB components out of the RGBA struct /// /// Note: you can use `.into()` to convert between other types #[inline(always)] pub fn rgb(&self) -> RGB { RGB { r: self.r.clone(), g: self.g.clone(), b: self.b.clone(), } } } impl ARGB { /// Copy RGB components out of the ARGB struct /// /// Note: you can use `.into()` to convert between other types #[inline(always)] pub fn rgb(&self) -> RGB { RGB { r: self.r.clone(), g: self.g.clone(), b: self.b.clone(), } } } impl BGRA { /// Copy RGB components out of the RGBA struct /// /// Note: you can use `.into()` to convert between other types #[deprecated(note = "This function will change. Use bgr()")] pub fn rgb(&self) -> BGR { BGR { r: self.r.clone(), g: self.g.clone(), b: self.b.clone(), } } } impl_rgba! {RGBA} impl_rgba! {BGRA} impl_rgba! {ARGB} impl_rgba! {ABGR} impl_alpha_conv! {BGR, BGRA} impl_alpha_conv! {RGB, BGRA} impl_alpha_conv! {BGR, RGBA} impl_alpha_conv! {RGB, RGBA} impl_alpha_conv! {BGR, ABGR} impl_alpha_conv! {RGB, ABGR} impl_alpha_conv! {BGR, ARGB} impl_alpha_conv! {RGB, ARGB} impl fmt::Display for RGBA { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "rgba({},{},{},{})", self.r, self.g, self.b, self.a) } } impl fmt::Display for BGRA { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "bgra({},{},{},{})", self.r, self.g, self.b, self.a) } }