| 1 | use crate::formats::gray_a::GrayA; |
| 2 | use core::ops::Deref; |
| 3 | |
| 4 | #[repr (C)] |
| 5 | #[cfg_attr (feature = "unstable-experimental" , deprecated(note = "renamed to GrayA" ))] |
| 6 | #[cfg_attr (feature = "serde" , derive(serde::Serialize, serde::Deserialize))] |
| 7 | #[cfg_attr (feature = "defmt-03" , derive(defmt::Format))] |
| 8 | #[derive (Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 9 | /// A pixel for grayscale value + alpha components (rgb crate v0.8) |
| 10 | /// |
| 11 | /// Through a `Deref` hack it renames the fields from `.0` and `.1` |
| 12 | /// to `.v` (value) and `.a` (alpha) |
| 13 | #[allow (non_camel_case_types)] |
| 14 | pub struct GrayAlpha_v08<T, A = T>( |
| 15 | /// Grayscale Component |
| 16 | /// |
| 17 | /// This field has been renamed to `.v` |
| 18 | pub T, |
| 19 | /// Alpha Component. This field has been renamed to `.a`. |
| 20 | pub A, |
| 21 | ); |
| 22 | |
| 23 | impl<T: Copy> GrayAlpha_v08<T> { |
| 24 | /// Reads the `.0` field |
| 25 | /// |
| 26 | /// This function isn't necessary, but it is forwards-compatible with the next major version of the RGB crate. |
| 27 | pub fn value(self) -> T { |
| 28 | self.0 |
| 29 | } |
| 30 | |
| 31 | /// Exposes the `.0` field for writing |
| 32 | /// |
| 33 | /// This function isn't necessary, but it is forwards-compatible with the next major version of the RGB crate. |
| 34 | pub fn value_mut(&mut self) -> &mut T { |
| 35 | &mut self.0 |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl<T, A> Deref for GrayAlpha_v08<T, A> { |
| 40 | type Target = GrayA<T, A>; |
| 41 | |
| 42 | /// A trick that allows using `.v` and `.a` on the old `GrayAlpha` type. |
| 43 | fn deref(&self) -> &GrayA<T, A> { |
| 44 | unsafe { |
| 45 | &*(self as *const Self as *const GrayA::<T, A>) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | #[test ] |
| 51 | fn swizzle() { |
| 52 | let g = GrayAlpha_v08(10u8, 20u8); |
| 53 | assert_eq!(10, g.v); |
| 54 | assert_eq!(20, g.a); |
| 55 | } |
| 56 | |