use crate::{
c_api::{self, NcChannel_u32},
NcAlpha, NcChannels, NcPaletteIndex, NcRgb,
};
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct NcChannel(pub NcChannel_u32);
mod std_impls {
use super::{NcChannel, NcChannel_u32};
impl Default for NcChannel {
fn default() -> Self {
Self::with_default()
}
}
impl From<NcChannel> for [u8; 3] {
#[inline]
fn from(rgb: NcChannel) -> Self {
rgb.into()
}
}
impl From<[u8; 3]> for NcChannel {
fn from(array: [u8; 3]) -> Self {
Self::from_rgb(array)
}
}
impl From<NcChannel> for (u8, u8, u8) {
#[inline]
fn from(rgb: NcChannel) -> Self {
rgb.into()
}
}
impl From<(u8, u8, u8)> for NcChannel {
fn from(tuple: (u8, u8, u8)) -> Self {
Self::from_rgb(tuple)
}
}
crate::from_primitive![NcChannel, NcChannel_u32];
crate::unit_impl_from![NcChannel, NcChannel_u32];
crate::unit_impl_fmt![bases+display; NcChannel];
}
impl NcChannel {
pub const DEFAULT_MASK: u32 = super::c_api::NC_BGDEFAULT_MASK;
pub const ALPHA_MASK: u32 = super::c_api::NC_BG_ALPHA_MASK;
pub const PALETTE_MASK: u32 = super::c_api::NC_BG_PALETTE;
pub const RGB_MASK: u32 = super::c_api::NC_BG_RGB_MASK;
}
impl NcChannel {
pub fn new() -> Self {
Self(c_api::NC_BGDEFAULT_MASK)
}
pub fn with_default() -> Self {
Self(0)
}
pub fn from_rgb(rgb: impl Into<NcRgb>) -> Self {
Self::new().set(rgb.into())
}
pub fn from_rgb_alpha(rgb: impl Into<NcRgb>, alpha: NcAlpha) -> Self {
Self::new().set(rgb.into()).set_alpha(alpha)
}
}
impl NcChannel {
pub fn fcombine(&self, bchannel: impl Into<NcChannel>) -> NcChannels {
c_api::ncchannels_combine(self.0, bchannel.into().0).into()
}
pub fn bcombine(&self, fchannel: impl Into<NcChannel>) -> NcChannels {
c_api::ncchannels_combine(fchannel.into().0, self.0).into()
}
pub fn alpha(&self) -> NcAlpha {
c_api::ncchannel_alpha(self.0).into()
}
pub fn set_alpha(&mut self, alpha: impl Into<NcAlpha>) -> Self {
c_api::ncchannel_set_alpha(&mut self.0, alpha.into());
*self
}
pub fn rgb_p(&self) -> bool {
c_api::ncchannel_rgb_p(self.0)
}
pub fn rgb(&self) -> NcRgb {
c_api::ncchannel_rgb(self.0).into()
}
pub fn set(&mut self, rgb: impl Into<NcRgb>) -> Self {
c_api::ncchannel_set(&mut self.0, rgb.into());
*self
}
pub fn rgb8(&self) -> (u8, u8, u8) {
let (mut r, mut g, mut b) = (0, 0, 0);
c_api::ncchannel_rgb8(self.0, &mut r, &mut g, &mut b);
(r, g, b)
}
pub fn set_rgb(&mut self, rgb: impl Into<NcRgb>) -> Self {
let (r, g, b) = rgb.into().into();
c_api::ncchannel_set_rgb8(&mut self.0, r, g, b);
*self
}
pub fn r(&self) -> u8 {
c_api::ncchannel_r(self.0)
}
pub fn g(&self) -> u8 {
c_api::ncchannel_g(self.0)
}
pub fn b(&self) -> u8 {
c_api::ncchannel_b(self.0)
}
pub fn set_r(&mut self, r: impl Into<u8>) -> Self {
c_api::ncchannel_set_r(&mut self.0, r.into()).into()
}
pub fn set_g(&mut self, g: impl Into<u8>) -> Self {
c_api::ncchannel_set_g(&mut self.0, g.into()).into()
}
pub fn set_b(&mut self, b: impl Into<u8>) -> Self {
c_api::ncchannel_set_b(&mut self.0, b.into()).into()
}
pub fn default_p(&self) -> bool {
c_api::ncchannel_default_p(self.0)
}
pub fn set_default(&mut self) -> Self {
c_api::ncchannel_set_default(&mut self.0).into()
}
pub fn set_not_default(&mut self) -> Self {
c_api::ncchannel_set_not_default(&mut self.0).into()
}
pub fn palindex(&self) -> NcPaletteIndex {
c_api::ncchannel_palindex(self.0)
}
pub fn palindex_p(&self) -> bool {
c_api::ncchannel_palindex_p(self.0)
}
pub fn set_palindex(&mut self, index: impl Into<NcPaletteIndex>) -> Self {
c_api::ncchannel_set_palindex(&mut self.0, index.into());
*self
}
}