use crate::{
c_api::{self, NcChannels_u64},
error, NcAlpha, NcChannel, NcPaletteIndex, NcResult, NcRgb,
};
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct NcChannels(pub NcChannels_u64);
mod std_impls {
use super::{NcChannels, NcChannels_u64};
impl Default for NcChannels {
fn default() -> Self {
Self::with_default()
}
}
crate::from_primitive![NcChannels, NcChannels_u64];
crate::unit_impl_from![NcChannels, NcChannels_u64];
crate::unit_impl_fmt![bases+display; NcChannels];
impl From<NcChannels> for [u8; 6] {
#[inline]
fn from(rgb: NcChannels) -> Self {
let fg: (u8, u8, u8) = rgb.fg_rgb().into();
let bg: (u8, u8, u8) = rgb.bg_rgb().into();
[fg.0, fg.1, fg.2, bg.0, bg.1, bg.2]
}
}
impl From<[u8; 6]> for NcChannels {
#[inline]
fn from(a: [u8; 6]) -> Self {
Self::from_rgb((a[0], a[1], a[2]), (a[3], a[4], a[5]))
}
}
impl From<NcChannels> for (u8, u8, u8, u8, u8, u8) {
#[inline]
fn from(rgb: NcChannels) -> Self {
let fg: (u8, u8, u8) = rgb.fg_rgb().into();
let bg: (u8, u8, u8) = rgb.bg_rgb().into();
(fg.0, fg.1, fg.2, bg.0, bg.1, bg.2)
}
}
impl From<(u8, u8, u8, u8, u8, u8)> for NcChannels {
fn from(t: (u8, u8, u8, u8, u8, u8)) -> Self {
Self::from_rgb([t.0, t.1, t.2], [t.3, t.4, t.5])
}
}
}
impl NcChannels {
pub const BG_DEFAULT_MASK: u32 = super::c_api::NC_BGDEFAULT_MASK;
pub const BG_ALPHA_MASK: u32 = super::c_api::NC_BG_ALPHA_MASK;
pub const BG_PALETTE_MASK: u32 = super::c_api::NC_BG_PALETTE;
pub const BG_RGB_MASK: u32 = super::c_api::NC_BG_RGB_MASK;
pub const NOBACKGROUND_MASK: u64 = c_api::NC_NOBACKGROUND_MASK;
}
impl NcChannels {
pub fn new() -> Self {
Self::combine(NcChannel::new(), NcChannel::new())
}
pub fn with_default() -> Self {
Self::combine(NcChannel::with_default(), NcChannel::with_default())
}
pub fn from_rgb(fg_rgb: impl Into<NcRgb>, bg_rgb: impl Into<NcRgb>) -> Self {
Self::combine(NcChannel::from_rgb(fg_rgb), NcChannel::from_rgb(bg_rgb))
}
pub fn from_rgb_both(rgb: impl Into<NcRgb>) -> Self {
let channel = NcChannel::new().set(rgb.into());
Self::combine(channel, channel)
}
pub fn from_rgb_alpha(
fg_rgb: impl Into<NcRgb>,
fg_alpha: impl Into<NcAlpha>,
bg_rgb: impl Into<NcRgb>,
bg_alpha: impl Into<NcAlpha>,
) -> Self {
Self::combine(
NcChannel::from_rgb(fg_rgb).set_alpha(fg_alpha),
NcChannel::from_rgb(bg_rgb).set_alpha(bg_alpha),
)
}
pub fn from_rgb_alpha_both(rgb: impl Into<NcRgb>, alpha: impl Into<NcAlpha>) -> Self {
let channel = NcChannel::new().set(rgb.into()).set_alpha(alpha.into());
Self::combine(channel, channel)
}
pub fn combine(fchannel: impl Into<NcChannel>, bchannel: impl Into<NcChannel>) -> Self {
c_api::ncchannels_combine(fchannel.into().0, bchannel.into().0).into()
}
pub fn reverse(&mut self) -> Self {
*self = c_api::ncchannels_reverse(self.0).into();
*self
}
}
impl NcChannels {
pub fn fchannel(&self) -> NcChannel {
c_api::ncchannels_fchannel(self.0).into()
}
pub fn bchannel(&self) -> NcChannel {
c_api::ncchannels_bchannel(self.0).into()
}
pub fn set_fchannel(&mut self, fchannel: impl Into<NcChannel>) -> Self {
c_api::ncchannels_set_fchannel(&mut self.0, fchannel.into().0).into()
}
pub fn set_bchannel(&mut self, bchannel: impl Into<NcChannel>) -> Self {
c_api::ncchannels_set_bchannel(&mut self.0, bchannel.into().0).into()
}
pub fn channels(&self) -> NcChannels {
c_api::ncchannels_channels(self.0).into()
}
pub fn set_channels(&mut self, from_channels: impl Into<NcChannels>) -> Self {
c_api::ncchannels_set_channels(&mut self.0, from_channels.into().0).into()
}
pub fn fg_alpha(&self) -> NcAlpha {
c_api::ncchannels_fg_alpha(self.0).into()
}
pub fn bg_alpha(&self) -> NcAlpha {
c_api::ncchannels_bg_alpha(self.0).into()
}
pub fn set_fg_alpha(&mut self, alpha: impl Into<NcAlpha>) -> NcResult<()> {
error![c_api::ncchannels_set_fg_alpha(&mut self.0, alpha.into())]
}
pub fn set_bg_alpha(&mut self, alpha: impl Into<NcAlpha>) -> NcResult<()> {
error![c_api::ncchannels_set_bg_alpha(&mut self.0, alpha.into())]
}
pub fn fg_rgb_p(&self) -> bool {
c_api::ncchannels_fg_rgb_p(self.0)
}
pub fn bg_rgb_p(&self) -> bool {
c_api::ncchannels_bg_rgb_p(self.0)
}
pub fn fg_rgb(&self) -> NcRgb {
c_api::ncchannels_fg_rgb(self.0).into()
}
pub fn bg_rgb(&self) -> NcRgb {
c_api::ncchannels_bg_rgb(self.0).into()
}
pub fn set_fg_rgb(&mut self, rgb: impl Into<NcRgb>) -> Self {
c_api::ncchannels_set_fg_rgb(&mut self.0, rgb.into().0);
*self
}
pub fn set_bg_rgb(&mut self, rgb: impl Into<NcRgb>) -> Self {
c_api::ncchannels_set_bg_rgb(&mut self.0, rgb.into().0);
*self
}
pub fn fg_r(&self) -> u8 {
c_api::ncchannel_r(c_api::ncchannels_fchannel(self.0))
}
pub fn fg_g(&self) -> u8 {
c_api::ncchannel_g(c_api::ncchannels_fchannel(self.0))
}
pub fn fg_b(&self) -> u8 {
c_api::ncchannel_b(c_api::ncchannels_fchannel(self.0))
}
pub fn bg_r(&self) -> u8 {
c_api::ncchannel_r(c_api::ncchannels_bchannel(self.0))
}
pub fn bg_g(&self) -> u8 {
c_api::ncchannel_g(c_api::ncchannels_bchannel(self.0))
}
pub fn bg_b(&self) -> u8 {
c_api::ncchannel_b(c_api::ncchannels_bchannel(self.0))
}
pub fn fg_set_r(&mut self, r: impl Into<u8>) -> Self {
let (_, g, b) = self.bg_rgb().into();
c_api::ncchannels_set_fg_rgb8(&mut self.0, r.into(), g, b).into()
}
pub fn fg_set_g(&mut self, g: impl Into<u8>) -> Self {
let (r, _, b) = self.bg_rgb().into();
c_api::ncchannels_set_fg_rgb8(&mut self.0, r, g.into(), b).into()
}
pub fn fg_set_b(&mut self, b: impl Into<u8>) -> Self {
let (r, g, _) = self.bg_rgb().into();
c_api::ncchannels_set_fg_rgb8(&mut self.0, r, g, b.into()).into()
}
pub fn bg_set_r(&mut self, r: impl Into<u8>) -> Self {
let (_, g, b) = self.bg_rgb().into();
c_api::ncchannels_set_bg_rgb8(&mut self.0, r.into(), g, b).into()
}
pub fn bg_set_g(&mut self, g: impl Into<u8>) -> Self {
let (r, _, b) = self.bg_rgb().into();
c_api::ncchannels_set_bg_rgb8(&mut self.0, r, g.into(), b).into()
}
pub fn bg_set_b(&mut self, b: impl Into<u8>) -> Self {
let (r, g, _) = self.bg_rgb().into();
c_api::ncchannels_set_bg_rgb8(&mut self.0, r, g, b.into()).into()
}
pub fn fg_default_p(&self) -> bool {
c_api::ncchannels_fg_default_p(self.0)
}
pub fn bg_default_p(&self) -> bool {
c_api::ncchannels_bg_default_p(self.0)
}
pub fn set_fg_default(&mut self) -> Self {
c_api::ncchannels_set_fg_default(&mut self.0).into()
}
pub fn set_bg_default(&mut self) -> Self {
c_api::ncchannels_set_bg_default(&mut self.0).into()
}
pub fn set_fg_not_default(&mut self) -> Self {
c_api::ncchannels_set_fg_not_default(&mut self.0).into()
}
pub fn set_bg_not_default(&mut self) -> Self {
c_api::ncchannels_set_bg_not_default(&mut self.0).into()
}
pub fn set_default(&mut self) -> Self {
c_api::ncchannels_set_fg_default(&mut c_api::ncchannels_set_bg_default(&mut self.0)).into()
}
pub fn set_not_default(&mut self) -> Self {
c_api::ncchannels_set_fg_not_default(&mut c_api::ncchannels_set_bg_not_default(&mut self.0))
.into()
}
pub fn fg_palindex(&self) -> NcPaletteIndex {
c_api::ncchannels_fg_palindex(self.0)
}
pub fn bg_palindex(&self) -> NcPaletteIndex {
c_api::ncchannels_bg_palindex(self.0)
}
pub fn fg_palindex_p(&self) -> bool {
c_api::ncchannels_fg_palindex_p(self.0)
}
pub fn bg_palindex_p(&self) -> bool {
c_api::ncchannels_bg_palindex_p(self.0)
}
pub fn set_fg_palindex(&mut self, index: impl Into<NcPaletteIndex>) -> Self {
c_api::ncchannels_set_fg_palindex(&mut self.0, index.into());
*self
}
pub fn set_bg_palindex(&mut self, index: impl Into<NcPaletteIndex>) -> Self {
c_api::ncchannels_set_bg_palindex(&mut self.0, index.into());
*self
}
}