pixeli/
with_alpha.rs

1use crate::*;
2
3/// A pixel which can gain an alpha component.
4pub trait WithAlpha: Pixel {
5    /// The pixel type with its alpha component.
6    type WithAlpha: Pixel;
7
8    /// Returns the pixel type with its alpha component. If no alpha component is already contained
9    /// then it is set to the maximum value.
10    /// [`PixelComponent`].
11    fn with_alpha(self) -> Self::WithAlpha;
12}
13/// A pixel which can lose its alpha component.
14pub trait WithoutAlpha: Pixel {
15    /// The pixel type without its alpha component.
16    type WithoutAlpha: Pixel;
17
18    /// Returns the pixel type without its alpha component.
19    fn without_alpha(self) -> Self::WithoutAlpha;
20}
21
22macro_rules! implement_lower_upper {
23    ($lower:ident, $upper:ident, {$($bit:ident),*}) => {
24        impl<T> WithAlpha for $lower<T> where T: PixelComponent {
25            type WithAlpha = $upper<T>;
26
27            fn with_alpha(self) -> Self::WithAlpha {
28                $upper {
29                    $($bit: self.$bit),*,
30                    a: <$lower<T> as Pixel>::Component::COMPONENT_MAX,
31                }
32            }
33        }
34        impl<T> WithoutAlpha for $upper<T> where T: PixelComponent {
35            type WithoutAlpha = $lower<T>;
36
37            fn without_alpha(self) -> Self::WithoutAlpha {
38                $lower {
39                    $($bit: self.$bit),*,
40                }
41            }
42        }
43    };
44}
45macro_rules! implement_with_no_op {
46    ($original:ident) => {
47        impl<T> WithAlpha for $original<T>
48        where
49            T: PixelComponent,
50        {
51            type WithAlpha = $original<T>;
52
53            fn with_alpha(self) -> Self::WithAlpha {
54                self
55            }
56        }
57    };
58}
59macro_rules! implement_without_no_op {
60    ($original:ident) => {
61        impl<T> WithoutAlpha for $original<T>
62        where
63            T: PixelComponent,
64        {
65            type WithoutAlpha = $original<T>;
66
67            fn without_alpha(self) -> Self::WithoutAlpha {
68                self
69            }
70        }
71    };
72}
73
74implement_without_no_op!(Rgb);
75implement_without_no_op!(Bgr);
76implement_without_no_op!(Gray);
77
78implement_with_no_op!(Rgba);
79implement_with_no_op!(Argb);
80implement_with_no_op!(Bgra);
81implement_with_no_op!(Abgr);
82implement_with_no_op!(GrayAlpha);
83
84implement_lower_upper!(Rgb, Rgba, {r, g, b});
85implement_lower_upper!(Bgr, Bgra, {r, g, b});
86implement_lower_upper!(Gray, GrayAlpha, { gray });