use crate::{
color::{Channel, Rgb},
error::NotcursesResult as Result,
sys::{NcChannel, NcPalette},
Notcurses,
};
#[derive(Clone, PartialEq, Eq)]
pub struct Palette {
nc: *mut NcPalette,
}
mod core_impls {
use super::{NcPalette, Palette};
use core::fmt;
impl Drop for Palette {
fn drop(&mut self) {
if crate::Notcurses::is_initialized() {
self.into_ref_mut().free()
}
}
}
impl fmt::Debug for Palette {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Palette {{ {:?}, {:?}, {:?}, … }}",
self.get_channel(0),
self.get_channel(1),
self.get_channel(2),
)
}
}
impl From<&mut NcPalette> for Palette {
fn from(ncplane: &mut NcPalette) -> Palette {
Palette {
nc: ncplane as *mut NcPalette,
}
}
}
}
impl Palette {
pub fn new(terminal: &Notcurses) -> Palette {
terminal.with_nc_mut(|nc| Self {
nc: NcPalette::new(nc),
})
}
pub fn into_ref(&self) -> &NcPalette {
unsafe { &*self.nc }
}
pub fn into_ref_mut(&mut self) -> &mut NcPalette {
unsafe { &mut *self.nc }
}
}
impl Palette {
pub fn use_in(&self, terminal: &Notcurses) -> Result<()> {
terminal.with_nc_mut(|nc| Ok(self.into_ref().r#use(nc)?))
}
pub fn get(&self, index: impl Into<u8>) -> Rgb {
self.into_ref().get(index.into()).into()
}
pub fn set(&mut self, index: impl Into<u8>, rgb: impl Into<Rgb>) {
self.into_ref_mut().set(index.into(), rgb.into());
}
pub fn get_channel(&self, index: impl Into<u8>) -> Channel {
NcChannel::from(self.into_ref().chans[index.into() as usize]).into()
}
pub fn set_channel(&mut self, index: impl Into<u8>, channel: impl Into<Channel>) {
let ncc = NcChannel::from(channel.into());
self.into_ref_mut().chans[index.into() as usize] = ncc.into();
}
}