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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
//!
use c_api::{NcRgb_u32, NcRgba_u32};
/// 24 bits broken into 3x RGB components.
///
/// Unlike with [`NcChannel`], operations involving `NcRgb`
/// ignores the last 4th byte (the alpha component).
///
/// ## Diagram
///
/// ```txt
/// -------- RRRRRRRR GGGGGGGG BBBBBBBB
/// ```
/// `type in C: no data type`
///
/// See also: [`NcRgba`] and [`NcChannel`] types.
///
/// [`NcChannel`]: crate::NcChannel
#[repr(transparent)]
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct NcRgb(pub c_api::NcRgb_u32);
impl NcRgb {
/// New const RGB color.
pub const fn new(r: u8, g: u8, b: u8) -> Self {
Self((r as NcRgb_u32) << 16 | (g as NcRgb_u32) << 8 | b as NcRgb_u32)
}
}
/// 32 bits broken into 3x RGB components + alpha component.
///
/// ## Diagram
///
/// ```txt
/// AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB
/// ```
/// `type in C: no data type`
///
/// See also: [`NcRgb`] and [`NcChannel`] types.
///
/// [`NcRgba`]: crate::NcRgba
/// [`NcChannel`]: crate::NcChannel
#[repr(transparent)]
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct NcRgba(pub c_api::NcRgba_u32);
impl NcRgba {
/// New const RGBA color.
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self(
(a as NcRgba_u32) << 24
| (r as NcRgba_u32) << 16
| (g as NcRgba_u32) << 8
| b as NcRgba_u32,
)
}
}
mod core_impls {
use super::{
c_api::{NcRgb_u32, NcRgba_u32},
NcRgb, NcRgba,
};
use core::fmt;
crate::from_primitive![NcRgb, NcRgb_u32];
crate::unit_impl_from![NcRgb, NcRgb_u32];
crate::unit_impl_fmt![bases; NcRgb];
impl fmt::Display for NcRgb {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:06X}")
}
}
impl fmt::Debug for NcRgb {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NcRgb({self})")
}
}
impl From<[u8; 3]> for NcRgb {
fn from(array: [u8; 3]) -> Self {
// u32::from_be_bytes(array).into()
Self(
(array[0] as NcRgb_u32) << 16
| (array[1] as NcRgb_u32) << 8
| array[2] as NcRgb_u32,
)
}
}
impl From<&[u8; 3]> for NcRgb {
fn from(array: &[u8; 3]) -> Self {
Self(
(array[0] as NcRgb_u32) << 16
| (array[1] as NcRgb_u32) << 8
| array[2] as NcRgb_u32,
)
}
}
impl From<NcRgb> for [u8; 3] {
#[inline]
fn from(rgb: NcRgb) -> Self {
[
((rgb.0 & 0xff0000) >> 16) as u8,
((rgb.0 & 0x00ff00) >> 8) as u8,
(rgb.0 & 0x0000ff) as u8,
]
}
}
impl From<NcRgb> for (u8, u8, u8) {
#[inline]
fn from(rgb: NcRgb) -> Self {
(
((rgb.0 & 0xff0000) >> 16) as u8,
((rgb.0 & 0x00ff00) >> 8) as u8,
(rgb.0 & 0x0000ff) as u8,
)
}
}
impl From<(u8, u8, u8)> for NcRgb {
fn from(tuple: (u8, u8, u8)) -> Self {
Self((tuple.0 as NcRgb_u32) << 16 | (tuple.1 as NcRgb_u32) << 8 | tuple.2 as NcRgb_u32)
}
}
//
crate::from_primitive![NcRgba, NcRgba_u32];
crate::unit_impl_from![NcRgba, NcRgba_u32];
crate::unit_impl_fmt![bases; NcRgba];
impl fmt::Display for NcRgba {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:08X}")
}
}
impl fmt::Debug for NcRgba {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NcRgba({self})")
}
}
/// [R, G, B, A]
impl From<[u8; 4]> for NcRgba {
fn from(array: [u8; 4]) -> Self {
u32::from_be_bytes(array).into()
}
}
impl From<&[u8; 4]> for NcRgba {
fn from(array: &[u8; 4]) -> Self {
u32::from_be_bytes(*array).into()
}
}
/// [R, G, B, A]
impl From<NcRgba> for [u8; 4] {
#[inline]
fn from(rgba: NcRgba) -> Self {
rgba.0.to_be_bytes()
}
}
/// (R, G, B, A)
impl From<(u8, u8, u8, u8)> for NcRgba {
fn from(tuple: (u8, u8, u8, u8)) -> Self {
u32::from_be_bytes([tuple.0, tuple.1, tuple.2, tuple.3]).into()
}
}
/// (R, G, B, A)
impl From<NcRgba> for (u8, u8, u8, u8) {
#[inline]
fn from(rgba: NcRgba) -> Self {
let a = rgba.0.to_be_bytes();
(a[0], a[1], a[2], a[3])
}
}
#[cfg(test)]
mod test {
use super::{NcRgb, NcRgba};
#[test]
fn rgbx_from() {
let rgb = NcRgb(0x112233_u32);
let rgb_arr = [0x11, 0x22, 0x33];
let rgb_tup = (0x11, 0x22, 0x33);
assert_eq!(rgb, NcRgb::from(rgb_arr));
assert_eq!(rgb, NcRgb::from(rgb_tup));
assert_eq!(rgb_arr, <[u8; 3]>::from(rgb));
assert_eq!(rgb_tup, <(u8, u8, u8)>::from(rgb));
let rgba = NcRgba(0x112233AA_u32);
let rgba_arr = [0x11, 0x22, 0x33, 0xAA];
let rgba_tup = (0x11, 0x22, 0x33, 0xAA);
assert_eq!(rgba, NcRgba::from(rgba_arr));
assert_eq!(rgba, NcRgba::from(rgba_tup));
assert_eq!(rgba_arr, <[u8; 4]>::from(rgba));
assert_eq!(rgba_tup, <(u8, u8, u8, u8)>::from(rgba));
}
}
}
pub(crate) mod c_api {
/// 24 bits broken into 3x RGB components.
///
/// It's recommended to use [`NcRgb`] instead.
///
/// Unlike with [`NcChannel_u32`], operations involving `NcRgb_u32`
/// ignores the last 4th byte (the alpha component).
///
/// ## Diagram
///
/// ```txt
/// -------- RRRRRRRR GGGGGGGG BBBBBBBB
/// ```
/// `type in C: no data type`
///
/// See also: [`NcRgba_u32`] and [`NcChannel_u32`] types.
///
/// [`NcRgb`]: crate::NcRgb
/// [`NcChannel_u32`]: crate::c_api::NcChannel_u32
pub type NcRgb_u32 = u32;
/// 32 bits broken into 3x RGB components plus one alpha component.
///
/// It's recommended to use [`NcRgba`] instead.
///
/// ## Diagram
///
/// ```txt
/// AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB
/// ```
/// `type in C: no data type`
///
/// See also: [`NcRgb_u32`] and [`NcChannel_u32`] types.
///
/// [`NcRgba`]: crate::NcRgba
/// [`NcChannel_u32`]: crate::c_api::NcChannel_u32
pub type NcRgba_u32 = u32;
// MAYBE?
// // NcBgra
// //
// /// 32 bits broken into 3x 8bpp BGR channels + 8ppp alpha.
// ///
// /// ## Diagram
// ///
// /// ```txt
// /// AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR
// /// ```
// ///
// /// `type in C: no data type`
// ///
// /// See also: [`NcRgba`], [`NcRgb`] and [`NcChannel`] types.
// pub type NcBgra = u32;
}