use crate::{
c_api::{self, NcPixel_u32},
NcPixel, NcRgb, NcRgba,
};
impl NcPixel {
pub fn new(value: c_api::NcPixel_u32) -> Self {
Self(value)
}
pub fn from_rgb(rgb: impl Into<NcRgb>) -> Self {
let (r, g, b) = rgb.into().into();
c_api::ncpixel(r, g, b).into()
}
pub fn from_rgba(rgba: impl Into<NcRgba>) -> Self {
let (r, g, b, a) = rgba.into().into();
let bgra = (a as NcPixel_u32) << 24
| (b as NcPixel_u32) << 16
| (g as NcPixel_u32) << 8
| r as NcPixel_u32;
bgra.into()
}
pub fn to_rgb(&self) -> NcRgb {
NcRgb::new(
c_api::ncpixel_r(self.0),
c_api::ncpixel_g(self.0),
c_api::ncpixel_b(self.0),
)
}
pub fn to_rgba(&self) -> NcRgba {
NcRgba::new(
c_api::ncpixel_r(self.0),
c_api::ncpixel_g(self.0),
c_api::ncpixel_b(self.0),
c_api::ncpixel_a(self.0),
)
}
pub fn a(self) -> u8 {
c_api::ncpixel_a(self.into())
}
pub fn b(self) -> u8 {
c_api::ncpixel_b(self.into())
}
pub fn g(self) -> u8 {
c_api::ncpixel_g(self.into())
}
pub fn r(self) -> u8 {
c_api::ncpixel_r(self.into())
}
pub fn set_a(&mut self, alpha: u8) {
c_api::ncpixel_set_a(self.into(), alpha)
}
pub fn set_g(&mut self, green: u8) {
c_api::ncpixel_set_b(self.into(), green)
}
pub fn set_b(&mut self, blue: u8) {
c_api::ncpixel_set_b(self.into(), blue)
}
pub fn set_r(&mut self, red: u8) {
c_api::ncpixel_set_r(self.into(), red)
}
pub fn set_rgb(&mut self, rgb: impl Into<NcRgb>) {
let (r, g, b) = rgb.into().into();
c_api::ncpixel_set_rgb8(self.into(), r, g, b);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn abgr() {
let rgba: NcRgba = 0x11223344.into();
let abgr: NcPixel = rgba.into();
assert_eq![0x44332211_u32, abgr.into()];
}
}