forked from kornelski/rust-rgb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgray_alpha.rs
More file actions
69 lines (62 loc) · 2.26 KB
/
gray_alpha.rs
File metadata and controls
69 lines (62 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::formats::gray_a::GrayA;
use core::ops::{Deref, DerefMut};
#[repr(C)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// A pixel for grayscale value + alpha components (rgb crate v0.8)
///
/// This is the legacy gray+alpha pixel type as opposed to the new gray+alpha type
/// (`rgb::GrayA`). This type is kept for backwards-compatibility.
///
/// You should transition to the new gray+alpha pixel type as this type is
/// due to be removed in a future release.
///
/// Through a `Deref` hack it renames the fields from `.0` and `.1`
/// to `.v` (value) and `.a` (alpha)
#[allow(non_camel_case_types)]
pub struct GrayAlpha_v08<T, A = T>(
/// Grayscale Component
///
/// This field has been renamed to `.v`
pub T,
/// Alpha Component. This field has been renamed to `.a`.
pub A,
);
impl<T, A> Deref for GrayAlpha_v08<T, A> {
type Target = GrayA<T, A>;
/// A trick that allows using `.v` and `.a` on the old `GrayAlpha` type.
fn deref(&self) -> &GrayA<T, A> {
unsafe { &*(self as *const Self).cast::<GrayA<T, A>>() }
}
}
impl<T, A> DerefMut for GrayAlpha_v08<T, A> {
/// Compatibility shim - allows mutable access to `.v` and `.a` on the old `GrayAlpha` type.
///
/// **Deprecated:** Migrate to `GrayA` instead of `GrayAlpha_v08`.
fn deref_mut(&mut self) -> &mut GrayA<T, A> {
unsafe { &mut *(self as *mut Self).cast::<GrayA<T, A>>() }
}
}
impl<T: Copy, A> GrayAlpha_v08<T, A> {
/// Value - the brightness component. May be luma or luminance.
///
/// This is a compatibility shim. Migrate to `GrayA` and use `.v` directly.
#[deprecated(since = "0.8.91", note = "Use GrayA with .v field instead")]
pub fn value(&self) -> T {
self.0
}
/// Exposes the `.0` field for writing
///
/// This is a compatibility shim. Migrate to `GrayA` and use `.v` directly.
#[deprecated(since = "0.8.91", note = "Use GrayA with .v field instead")]
pub fn value_mut(&mut self) -> &mut T {
&mut self.0
}
}
#[test]
fn swizzle() {
let g = GrayAlpha_v08(10_u8, 20_u8);
assert_eq!(10, g.v);
assert_eq!(20, g.a);
}