1use c_api::{NcRgb_u32, NcRgba_u32};
4
5#[repr(transparent)]
21#[derive(Clone, Copy, Default, PartialEq, Eq)]
22pub struct NcRgb(pub c_api::NcRgb_u32);
23impl NcRgb {
24 pub const fn new(r: u8, g: u8, b: u8) -> Self {
26 Self((r as NcRgb_u32) << 16 | (g as NcRgb_u32) << 8 | b as NcRgb_u32)
27 }
28}
29
30#[repr(transparent)]
44#[derive(Clone, Copy, Default, PartialEq, Eq)]
45pub struct NcRgba(pub c_api::NcRgba_u32);
46impl NcRgba {
47 pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
49 Self(
50 (a as NcRgba_u32) << 24
51 | (r as NcRgba_u32) << 16
52 | (g as NcRgba_u32) << 8
53 | b as NcRgba_u32,
54 )
55 }
56}
57mod core_impls {
58 use super::{
59 c_api::{NcRgb_u32, NcRgba_u32},
60 NcRgb, NcRgba,
61 };
62 use core::fmt;
63
64 crate::from_primitive![NcRgb, NcRgb_u32];
65 crate::unit_impl_from![NcRgb, NcRgb_u32];
66 crate::unit_impl_fmt![bases; NcRgb];
67
68 impl fmt::Display for NcRgb {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "{self:06X}")
71 }
72 }
73 impl fmt::Debug for NcRgb {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 write!(f, "NcRgb({self})")
76 }
77 }
78
79 impl From<[u8; 3]> for NcRgb {
80 fn from(array: [u8; 3]) -> Self {
81 Self(
83 (array[0] as NcRgb_u32) << 16
84 | (array[1] as NcRgb_u32) << 8
85 | array[2] as NcRgb_u32,
86 )
87 }
88 }
89 impl From<&[u8; 3]> for NcRgb {
90 fn from(array: &[u8; 3]) -> Self {
91 Self(
92 (array[0] as NcRgb_u32) << 16
93 | (array[1] as NcRgb_u32) << 8
94 | array[2] as NcRgb_u32,
95 )
96 }
97 }
98 impl From<NcRgb> for [u8; 3] {
99 #[inline]
100 fn from(rgb: NcRgb) -> Self {
101 [
102 ((rgb.0 & 0xff0000) >> 16) as u8,
103 ((rgb.0 & 0x00ff00) >> 8) as u8,
104 (rgb.0 & 0x0000ff) as u8,
105 ]
106 }
107 }
108 impl From<NcRgb> for (u8, u8, u8) {
109 #[inline]
110 fn from(rgb: NcRgb) -> Self {
111 (
112 ((rgb.0 & 0xff0000) >> 16) as u8,
113 ((rgb.0 & 0x00ff00) >> 8) as u8,
114 (rgb.0 & 0x0000ff) as u8,
115 )
116 }
117 }
118 impl From<(u8, u8, u8)> for NcRgb {
119 fn from(tuple: (u8, u8, u8)) -> Self {
120 Self((tuple.0 as NcRgb_u32) << 16 | (tuple.1 as NcRgb_u32) << 8 | tuple.2 as NcRgb_u32)
121 }
122 }
123
124 crate::from_primitive![NcRgba, NcRgba_u32];
127 crate::unit_impl_from![NcRgba, NcRgba_u32];
128 crate::unit_impl_fmt![bases; NcRgba];
129
130 impl fmt::Display for NcRgba {
131 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132 write!(f, "{self:08X}")
133 }
134 }
135 impl fmt::Debug for NcRgba {
136 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137 write!(f, "NcRgba({self})")
138 }
139 }
140
141 impl From<[u8; 4]> for NcRgba {
143 fn from(array: [u8; 4]) -> Self {
144 u32::from_be_bytes(array).into()
145 }
146 }
147 impl From<&[u8; 4]> for NcRgba {
148 fn from(array: &[u8; 4]) -> Self {
149 u32::from_be_bytes(*array).into()
150 }
151 }
152 impl From<NcRgba> for [u8; 4] {
154 #[inline]
155 fn from(rgba: NcRgba) -> Self {
156 rgba.0.to_be_bytes()
157 }
158 }
159
160 impl From<(u8, u8, u8, u8)> for NcRgba {
162 fn from(tuple: (u8, u8, u8, u8)) -> Self {
163 u32::from_be_bytes([tuple.0, tuple.1, tuple.2, tuple.3]).into()
164 }
165 }
166 impl From<NcRgba> for (u8, u8, u8, u8) {
168 #[inline]
169 fn from(rgba: NcRgba) -> Self {
170 let a = rgba.0.to_be_bytes();
171 (a[0], a[1], a[2], a[3])
172 }
173 }
174
175 #[cfg(test)]
176 mod test {
177 use super::{NcRgb, NcRgba};
178
179 #[test]
180 fn rgbx_from() {
181 let rgb = NcRgb(0x112233_u32);
182 let rgb_arr = [0x11, 0x22, 0x33];
183 let rgb_tup = (0x11, 0x22, 0x33);
184
185 assert_eq!(rgb, NcRgb::from(rgb_arr));
186 assert_eq!(rgb, NcRgb::from(rgb_tup));
187 assert_eq!(rgb_arr, <[u8; 3]>::from(rgb));
188 assert_eq!(rgb_tup, <(u8, u8, u8)>::from(rgb));
189
190 let rgba = NcRgba(0x112233AA_u32);
191 let rgba_arr = [0x11, 0x22, 0x33, 0xAA];
192 let rgba_tup = (0x11, 0x22, 0x33, 0xAA);
193
194 assert_eq!(rgba, NcRgba::from(rgba_arr));
195 assert_eq!(rgba, NcRgba::from(rgba_tup));
196 assert_eq!(rgba_arr, <[u8; 4]>::from(rgba));
197 assert_eq!(rgba_tup, <(u8, u8, u8, u8)>::from(rgba));
198 }
199 }
200}
201
202pub(crate) mod c_api {
203 pub type NcRgb_u32 = u32;
222
223 pub type NcRgba_u32 = u32;
239
240 }