| 1 | #[allow(deprecated)] |
| 2 | use super::pixel::ComponentSlice; |
| 3 | use super::pixel::{ComponentMap, ColorComponentMap}; |
| 4 | #[cfg(feature = "as-bytes")] |
| 5 | use super::pixel::ComponentBytes; |
| 6 | use crate::alt::GRB; |
| 7 | use crate::alt::{BGR, BGRA}; |
| 8 | use crate::{RGB, RGBA}; |
| 9 | use core::fmt; |
| 10 | |
| 11 | impl<T> BGR<T> { |
| 12 | /// Convenience function for creating a new pixel |
| 13 | /// Warning: The order of arguments is R,G,B |
| 14 | #[deprecated(note = "This function has a misleading order of arguments. Use BGR{} literal instead")] |
| 15 | pub const fn new(r: T, g: T, b: T) -> Self { |
| 16 | Self { b, g, r } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | macro_rules! impl_rgb { |
| 21 | ($RGB:ident) => { |
| 22 | impl<T: Clone> $RGB<T> { |
| 23 | /// Iterate over color components (R, G, and B) |
| 24 | #[inline(always)] |
| 25 | pub fn iter(&self) -> core::iter::Cloned<core::slice::Iter<'_, T>> { |
| 26 | #[allow(deprecated)] |
| 27 | ComponentSlice::as_slice(self).iter().cloned() |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl<T: Copy, B> ComponentMap<$RGB<B>, T, B> for $RGB<T> { |
| 32 | #[inline(always)] |
| 33 | fn map<F>(&self, mut f: F) -> $RGB<B> |
| 34 | where F: FnMut(T) -> B { |
| 35 | $RGB { |
| 36 | r:f(self.r), |
| 37 | g:f(self.g), |
| 38 | b:f(self.b), |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl<T: Copy, B> ColorComponentMap<$RGB<B>, T, B> for $RGB<T> { |
| 44 | #[inline(always)] |
| 45 | fn map_colors<F>(&self, mut f: F) -> $RGB<B> |
| 46 | where F: FnMut(T) -> B { |
| 47 | $RGB { |
| 48 | r:f(self.r), |
| 49 | g:f(self.g), |
| 50 | b:f(self.b), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[allow(deprecated)] |
| 56 | impl<T> ComponentSlice<T> for $RGB<T> { |
| 57 | #[inline(always)] |
| 58 | fn as_slice(&self) -> &[T] { |
| 59 | unsafe { |
| 60 | core::slice::from_raw_parts(self as *const Self as *const T, 3) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #[inline(always)] |
| 65 | fn as_mut_slice(&mut self) -> &mut [T] { |
| 66 | unsafe { |
| 67 | core::slice::from_raw_parts_mut(self as *mut Self as *mut T, 3) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[allow(deprecated)] |
| 73 | impl<T> ComponentSlice<T> for [$RGB<T>] { |
| 74 | #[inline] |
| 75 | fn as_slice(&self) -> &[T] { |
| 76 | unsafe { |
| 77 | core::slice::from_raw_parts(self.as_ptr() as *const _, self.len() * 3) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[inline] |
| 82 | fn as_mut_slice(&mut self) -> &mut [T] { |
| 83 | unsafe { |
| 84 | core::slice::from_raw_parts_mut(self.as_mut_ptr() as *mut _, self.len() * 3) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #[cfg(feature = "as-bytes")] |
| 90 | impl<T: crate::Pod> ComponentBytes<T> for [$RGB<T>] {} |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | macro_rules! impl_rgb_to_alpha { |
| 95 | ($RGB:ident, $RGBA:ident) => { |
| 96 | impl<T: Clone> $RGB<T> { |
| 97 | /// Convenience function for converting to RGBA |
| 98 | #[doc(hidden)] |
| 99 | #[deprecated(note = "use .with_alpha(a) instead; this will become a getter in the future")] |
| 100 | pub fn alpha(&self, a: T) -> $RGBA<T> { |
| 101 | self.with_alpha(a) |
| 102 | } |
| 103 | |
| 104 | /// Convenience function for converting to RGBA |
| 105 | #[inline(always)] |
| 106 | #[doc(alias = "alpha")] |
| 107 | pub fn with_alpha(&self, a: T) -> $RGBA<T> { |
| 108 | $RGBA { |
| 109 | r: self.r.clone(), |
| 110 | g: self.g.clone(), |
| 111 | b: self.b.clone(), |
| 112 | a, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// Convenience function for converting to RGBA with alpha channel of a different type than type of the pixels |
| 117 | #[inline(never)] |
| 118 | #[deprecated(note = "use .with_alpha(a) instead")] |
| 119 | pub fn new_alpha<A>(&self, a: A) -> $RGBA<T, A> { |
| 120 | $RGBA { |
| 121 | r: self.r.clone(), |
| 122 | g: self.g.clone(), |
| 123 | b: self.b.clone(), |
| 124 | a, |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | impl<T> core::iter::FromIterator<T> for RGB<T> { |
| 132 | /// Takes exactly 3 elements from the iterator and creates a new instance. |
| 133 | /// Panics if there are fewer elements in the iterator. |
| 134 | #[inline(always)] |
| 135 | fn from_iter<I: IntoIterator<Item = T>>(into_iter: I) -> Self { |
| 136 | let mut iter = into_iter.into_iter(); |
| 137 | Self { |
| 138 | r: iter.next().unwrap(), |
| 139 | g: iter.next().unwrap(), |
| 140 | b: iter.next().unwrap(), |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | impl_rgb! {RGB} |
| 146 | impl_rgb_to_alpha! {RGB, RGBA} |
| 147 | impl_rgb! {BGR} |
| 148 | impl_rgb_to_alpha! {BGR, BGRA} |
| 149 | impl_rgb! {GRB} |
| 150 | |
| 151 | impl<T: fmt::Display> fmt::Display for RGB<T> { |
| 152 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 153 | write!(f, "rgb({},{},{})", self.r, self.g, self.b) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | impl<T: fmt::UpperHex> fmt::UpperHex for RGB<T> { |
| 158 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 159 | write!(f, "RGB {{ #{:02X}{:02X}{:02X} }}", self.r, self.g, self.b) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | impl<T: fmt::LowerHex> fmt::LowerHex for RGB<T> { |
| 164 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 165 | write!(f, "RGB {{ #{:02x}{:02x}{:02x} }}", self.r, self.g, self.b) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | impl<T: fmt::Display> fmt::Display for BGR<T> { |
| 170 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 171 | write!(f, "bgr({},{},{})", self.b, self.g, self.r) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | impl<T: fmt::UpperHex> fmt::UpperHex for BGR<T> { |
| 176 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 177 | write!(f, "BGR {{ #{:02X}{:02X}{:02X} }}", self.b, self.g, self.r) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | impl<T: fmt::LowerHex> fmt::LowerHex for BGR<T> { |
| 182 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 183 | write!(f, "BGR {{ #{:02x}{:02x}{:02x} }}", self.b, self.g, self.r) |
| 184 | } |
| 185 | } |