graphics/color/
argb8888.rs1use core::fmt::Debug;
2
3use crate::ColorRGB888;
4
5use super::{ColorRGB565, ColorRGBA8888};
6
7#[derive(Clone, Copy, PartialEq, Eq)]
8#[repr(transparent)]
9pub struct ColorARGB8888(u32);
10
11impl Debug for ColorARGB8888 {
12 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
13 formatter
14 .debug_struct("Color_ARGB8888_type")
15 .field("Alpha", &self.get_alpha())
16 .field("Red", &self.get_red())
17 .field("Green", &self.get_green())
18 .field("Blue", &self.get_blue())
19 .finish()
20 }
21}
22
23impl ColorARGB8888 {
24 pub const fn new(alpha: u8, red: u8, green: u8, blue: u8) -> Self {
25 Self(0)
26 .set_alpha(alpha)
27 .set_red(red)
28 .set_green(green)
29 .set_blue(blue)
30 }
31
32 pub const fn from_rgb565(value: ColorRGB565) -> Self {
33 Self::new(
34 0xFF,
35 value.get_red() << 3 | value.get_red() >> 2,
36 value.get_green() << 2 | value.get_green() >> 4,
37 value.get_blue() << 3 | value.get_blue() >> 2,
38 )
39 }
40
41 pub const fn as_u32(self) -> u32 {
42 self.0
43 }
44
45 pub const fn get_alpha(&self) -> u8 {
46 ((self.0 >> 24) & 0b1111_1111) as u8
47 }
48
49 pub const fn get_red(&self) -> u8 {
50 ((self.0 >> 16) & 0b1111_1111) as u8
51 }
52
53 pub const fn get_green(&self) -> u8 {
54 ((self.0 >> 8) & 0b1111_1111) as u8
55 }
56
57 pub const fn get_blue(&self) -> u8 {
58 ((self.0) & 0b1111_1111) as u8
59 }
60
61 pub const fn set_alpha(mut self, value: u8) -> Self {
62 self.0 = (self.0 & !(0b1111_1111 << 24)) | ((value as u32) << 24);
63 self
64 }
65
66 pub const fn set_red(mut self, value: u8) -> Self {
67 self.0 = (self.0 & 0xFF00_FFFF) | ((value as u32) << 16);
68 self
69 }
70
71 pub const fn set_green(mut self, value: u8) -> Self {
72 self.0 = (self.0 & 0xFFFF_00FF) | ((value as u32) << 8);
73 self
74 }
75
76 pub const fn set_blue(mut self, value: u8) -> Self {
77 self.0 = (self.0 & 0xFFFF_FF00) | (value as u32);
78 self
79 }
80}
81
82impl From<ColorRGB888> for ColorARGB8888 {
83 fn from(value: ColorRGB888) -> Self {
84 Self::new(0xFF, value.get_red(), value.get_green(), value.get_blue())
85 }
86}
87
88impl From<ColorARGB8888> for u32 {
89 fn from(value: ColorARGB8888) -> u32 {
90 value.0
91 }
92}
93
94impl From<u32> for ColorARGB8888 {
95 fn from(value: u32) -> Self {
96 Self(value)
97 }
98}
99
100impl From<ColorRGB565> for ColorARGB8888 {
101 fn from(value: ColorRGB565) -> Self {
102 Self::from_rgb565(value)
103 }
104}
105
106impl From<ColorRGBA8888> for ColorARGB8888 {
107 fn from(value: ColorRGBA8888) -> Self {
108 Self::new(
109 value.get_alpha(),
110 value.get_red(),
111 value.get_green(),
112 value.get_blue(),
113 )
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 #[test]
122 fn test_argb8888() {
123 let color = ColorARGB8888::new(255, 255, 255, 255);
124 assert_eq!(color.get_alpha(), 0xFF);
125 assert_eq!(color.get_red(), 0xFF);
126 assert_eq!(color.get_green(), 0xFF);
127 assert_eq!(color.get_blue(), 0xFF);
128 assert_eq!(color.0, 0xFFFF_FFFF);
129
130 let color = ColorARGB8888::new(255, 0, 0, 0);
131 assert_eq!(color.get_alpha(), 0xFF);
132 assert_eq!(color.get_red(), 0);
133 assert_eq!(color.get_green(), 0);
134 assert_eq!(color.get_blue(), 0);
135 assert_eq!(color.0, 0xFF00_0000);
136
137 let color = ColorARGB8888::new(0, 255, 0, 0);
138 assert_eq!(color.get_alpha(), 0);
139 assert_eq!(color.get_red(), 0xFF);
140 assert_eq!(color.get_green(), 0);
141 assert_eq!(color.get_blue(), 0);
142 assert_eq!(color.0, 0x00FF_0000);
143
144 let color = ColorARGB8888::new(0, 0, 255, 0);
145 assert_eq!(color.get_alpha(), 0);
146 assert_eq!(color.get_red(), 0);
147 assert_eq!(color.get_green(), 0xFF);
148 assert_eq!(color.get_blue(), 0);
149 assert_eq!(color.0, 0x0000_FF00);
150
151 let color = ColorARGB8888::new(0, 0, 0, 255);
152 assert_eq!(color.get_alpha(), 0);
153 assert_eq!(color.get_red(), 0);
154 assert_eq!(color.get_green(), 0);
155 assert_eq!(color.get_blue(), 0xFF);
156 assert_eq!(color.0, 0x0000_00FF);
157
158 let color = ColorARGB8888::new(0, 0, 0, 0);
159 assert_eq!(color.get_alpha(), 0);
160 assert_eq!(color.get_red(), 0);
161 assert_eq!(color.get_green(), 0);
162 assert_eq!(color.get_blue(), 0);
163 assert_eq!(color.0, 0);
164 }
165}