Debian Codegraph @

src/legacy/internal/rgba.rs

raw symbols SCIP
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::{BGRA, ARGB, ABGR, BGR};
7 use crate::{RGB, RGBA};
8 use core::fmt;
9
10 impl<T, A> RGBA<T, A> {
11 #[inline(always)]
12 /// Convenience function for creating a new pixel
13 /// The order of arguments is R,G,B,A
14 pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self {
15 Self { r, g, b, a }
16 }
17 }
18
19 impl<T> BGRA<T> {
20 #[inline(always)]
21 /// Convenience function for creating a new pixel
22 /// Warning: The order of arguments is R,G,B,A
23 #[deprecated(note = "This function has a misleading order of arguments. Use BGRA{} literal instead")]
24 pub const fn new(r: T, g: T, b: T, a: T) -> Self {
25 Self { b, g, r, a }
26 }
27 }
28
29 /// ```rust,compile_fail
30 /// let r = rgb::BGRA::<u8,u16>::zeroed();
31 /// ```
32 impl<T, A> BGRA<T, A> {
33 #[inline(always)]
34 /// Convenience function for creating a new pixel
35 /// Warning: The order of arguments is R,G,B,A
36 #[deprecated(note = "This function has a misleading order of arguments. Use BGRA{} literal instead")]
37 pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self {
38 Self { b, g, r, a }
39 }
40 }
41
42 impl<T> ARGB<T> {
43 #[inline(always)]
44 /// Convenience function for creating a new pixel
45 /// The order of arguments is R,G,B,A
46 #[deprecated(note = "This function has a misleading order of arguments. Use ARGB{} literal instead")]
47 pub const fn new(r: T, g: T, b: T, a: T) -> Self {
48 Self { a, r, g, b }
49 }
50 }
51
52 impl<T, A> ARGB<T, A> {
53 #[inline(always)]
54 /// Convenience function for creating a new pixel
55 /// The order of arguments is R,G,B,A
56 #[deprecated(note = "This function has a misleading order of arguments. Use ARGB{} literal instead")]
57 pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self {
58 Self { a, r, g, b }
59 }
60 }
61
62 impl<T> ABGR<T> {
63 #[inline(always)]
64 /// Convenience function for creating a new pixel
65 /// The order of arguments is R,G,B,A
66 #[deprecated(note = "This function has a misleading order of arguments. Use ABGR{} literal instead")]
67 pub const fn new(r: T, g: T, b: T, a: T) -> Self {
68 Self { a, b, g, r }
69 }
70 }
71
72 impl<T, A> ABGR<T, A> {
73 #[inline(always)]
74 /// Convenience function for creating a new pixel
75 /// The order of arguments is R,G,B,A
76 #[deprecated(note = "This function has a misleading order of arguments. Use ABGR{} literal instead")]
77 pub const fn new_alpha(r: T, g: T, b: T, a: A) -> Self {
78 Self { a, b, g, r }
79 }
80 }
81
82 macro_rules! impl_rgba {
83 ($RGBA:ident) => {
84 impl<T: Clone> $RGBA<T> {
85 /// Iterate over all components (length=4)
86 #[inline(always)]
87 pub fn iter(&self) -> core::iter::Cloned<core::slice::Iter<'_, T>> {
88 #[allow(deprecated)]
89 ComponentSlice::as_slice(self).iter().cloned()
90 }
91 }
92
93 impl<T: Clone, A> $RGBA<T, A> {
94 /// Copy RGB components out of the RGBA struct
95 ///
96 /// Note: you can use `.into()` to convert between other types
97 #[inline(always)]
98 pub fn bgr(&self) -> BGR<T> {
99 BGR {
100 r: self.r.clone(),
101 g: self.g.clone(),
102 b: self.b.clone(),
103 }
104 }
105 }
106
107 impl<T: Copy, A: Clone> $RGBA<T, A> {
108 /// Create new RGBA with the same alpha value, but different RGB values
109 #[deprecated(note = "Renamed to map_colors()")]
110 pub fn map_rgb<F, U, B>(&self, mut f: F) -> $RGBA<U, B>
111 where F: FnMut(T) -> U, U: Clone, B: From<A> + Clone
112 {
113 $RGBA {
114 r: f(self.r),
115 g: f(self.g),
116 b: f(self.b),
117 a: self.a.clone().into(),
118 }
119 }
120
121 #[doc(hidden)]
122 #[deprecated(note = "use .with_alpha(a) instead")]
123 /// Create a new RGBA with the new alpha value, but same RGB values
124 pub fn alpha(&self, a: A) -> Self {
125 self.with_alpha(a)
126 }
127
128 #[inline(always)]
129 /// Create a new RGBA with the new alpha value, but same RGB values
130 pub fn with_alpha(&self, a: A) -> Self {
131 Self { r: self.r, g: self.g, b: self.b, a }
132 }
133
134 /// Create a new RGBA with a new alpha value created by the callback.
135 /// Allows changing of the type used for the alpha channel.
136 #[inline]
137 pub fn map_alpha<F, B>(&self, f: F) -> $RGBA<T, B>
138 where F: FnOnce(A) -> B {
139 $RGBA {
140 r: self.r,
141 g: self.g,
142 b: self.b,
143 a: f(self.a.clone()),
144 }
145 }
146 }
147
148 impl<T: Copy, B> ComponentMap<$RGBA<B>, T, B> for $RGBA<T> {
149 #[inline(always)]
150 fn map<F>(&self, mut f: F) -> $RGBA<B>
151 where F: FnMut(T) -> B {
152 $RGBA {
153 r: f(self.r),
154 g: f(self.g),
155 b: f(self.b),
156 a: f(self.a),
157 }
158 }
159 }
160
161 impl<T: Copy, A: Copy, B> ColorComponentMap<$RGBA<B, A>, T, B> for $RGBA<T, A> {
162 #[inline(always)]
163 fn map_colors<F>(&self, mut f: F) -> $RGBA<B, A>
164 where F: FnMut(T) -> B {
165 $RGBA {
166 r: f(self.r),
167 g: f(self.g),
168 b: f(self.b),
169 a: self.a,
170 }
171 }
172 }
173
174 #[allow(deprecated)]
175 impl<T> ComponentSlice<T> for $RGBA<T> {
176 #[inline(always)]
177 fn as_slice(&self) -> &[T] {
178 unsafe {
179 core::slice::from_raw_parts(self as *const Self as *const T, 4)
180 }
181 }
182
183 #[inline(always)]
184 fn as_mut_slice(&mut self) -> &mut [T] {
185 unsafe {
186 core::slice::from_raw_parts_mut(self as *mut Self as *mut T, 4)
187 }
188 }
189 }
190
191 #[allow(deprecated)]
192 impl<T> ComponentSlice<T> for [$RGBA<T>] {
193 #[inline]
194 fn as_slice(&self) -> &[T] {
195 unsafe {
196 core::slice::from_raw_parts(self.as_ptr() as *const _, self.len() * 4)
197 }
198 }
199
200 #[inline]
201 fn as_mut_slice(&mut self) -> &mut [T] {
202 unsafe {
203 core::slice::from_raw_parts_mut(self.as_mut_ptr() as *mut _, self.len() * 4)
204 }
205 }
206 }
207
208 #[cfg(feature = "as-bytes")]
209 impl<T: crate::Pod> ComponentBytes<T> for [$RGBA<T>] {}
210 };
211 }
212
213 macro_rules! impl_alpha_conv {
214 ($RGB:ident, $RGBA:ident) => {
215 /// Assumes 255 is opaque
216 impl<T: Copy> From<$RGB<T>> for $RGBA<T, u8> {
217 #[inline(always)]
218 fn from(other: $RGB<T>) -> Self {
219 Self {
220 r: other.r,
221 g: other.g,
222 b: other.b,
223 a: 0xFF,
224 }
225 }
226 }
227
228 /// Assumes 65535 is opaque
229 impl<T: Copy> From<$RGB<T>> for $RGBA<T, u16> {
230 #[inline(always)]
231 fn from(other: $RGB<T>) -> Self {
232 Self {
233 r: other.r,
234 g: other.g,
235 b: other.b,
236 a: 0xFFFF,
237 }
238 }
239 }
240 };
241 }
242
243 impl<T, A> RGBA<T, A> {
244 /// Provide a mutable view of only RGB components (leaving out alpha).
245 /// Useful to change color without changing opacity.
246 #[inline(always)]
247 pub fn rgb_mut(&mut self) -> &mut RGB<T> {
248 unsafe { &mut *(self as *mut Self).cast::<RGB<T>>() }
249 }
250 }
251
252 impl<T, A> BGRA<T, A> {
253 /// Provide a mutable view of only RGB components (leaving out alpha).
254 /// Useful to change color without changing opacity.
255 #[deprecated(note = "This function will change. Use bgr_mut()")]
256 pub fn rgb_mut(&mut self) -> &mut BGR<T> {
257 unsafe { &mut *(self as *mut Self).cast::<BGR<T>>() }
258 }
259
260 /// Provide a mutable view of only RGB components (leaving out alpha).
261 /// Useful to change color without changing opacity.
262 #[inline(always)]
263 pub fn bgr_mut(&mut self) -> &mut BGR<T> {
264 unsafe { &mut *(self as *mut Self).cast::<BGR<T>>() }
265 }
266 }
267
268 impl<T> core::iter::FromIterator<T> for RGBA<T> {
269 #[inline(always)]
270 /// Takes exactly 4 elements from the iterator and creates a new instance.
271 /// Panics if there are fewer elements in the iterator.
272 fn from_iter<I: IntoIterator<Item = T>>(into_iter: I) -> Self {
273 let mut iter = into_iter.into_iter();
274 Self {
275 r: iter.next().unwrap(),
276 g: iter.next().unwrap(),
277 b: iter.next().unwrap(),
278 a: iter.next().unwrap(),
279 }
280 }
281 }
282
283 impl<T: Clone, A> RGBA<T, A> {
284 /// Copy RGB components out of the RGBA struct
285 ///
286 /// Note: you can use `.into()` to convert between other types
287 #[inline(always)]
288 pub fn rgb(&self) -> RGB<T> {
289 RGB {
290 r: self.r.clone(),
291 g: self.g.clone(),
292 b: self.b.clone(),
293 }
294 }
295 }
296
297 impl<T: Clone, A> ARGB<T, A> {
298 /// Copy RGB components out of the ARGB struct
299 ///
300 /// Note: you can use `.into()` to convert between other types
301 #[inline(always)]
302 pub fn rgb(&self) -> RGB<T> {
303 RGB {
304 r: self.r.clone(),
305 g: self.g.clone(),
306 b: self.b.clone(),
307 }
308 }
309 }
310
311 impl<T: Clone, A> BGRA<T, A> {
312 /// Copy RGB components out of the RGBA struct
313 ///
314 /// Note: you can use `.into()` to convert between other types
315 #[deprecated(note = "This function will change. Use bgr()")]
316 pub fn rgb(&self) -> BGR<T> {
317 BGR {
318 r: self.r.clone(),
319 g: self.g.clone(),
320 b: self.b.clone(),
321 }
322 }
323 }
324
325 impl_rgba! {RGBA}
326 impl_rgba! {BGRA}
327 impl_rgba! {ARGB}
328 impl_rgba! {ABGR}
329
330 impl_alpha_conv! {BGR, BGRA}
331 impl_alpha_conv! {RGB, BGRA}
332 impl_alpha_conv! {BGR, RGBA}
333 impl_alpha_conv! {RGB, RGBA}
334 impl_alpha_conv! {BGR, ABGR}
335 impl_alpha_conv! {RGB, ABGR}
336 impl_alpha_conv! {BGR, ARGB}
337 impl_alpha_conv! {RGB, ARGB}
338
339 impl<T: fmt::Display, A: fmt::Display> fmt::Display for RGBA<T, A> {
340 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
341 write!(f, "rgba({},{},{},{})", self.r, self.g, self.b, self.a)
342 }
343 }
344
345 impl<T: fmt::Display, A: fmt::Display> fmt::Display for BGRA<T, A> {
346 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
347 write!(f, "bgra({},{},{},{})", self.r, self.g, self.b, self.a)
348 }
349 }
350