libnotcurses_sys/palette/methods.rs
1//! `NcPalette` methods and associated functions.
2
3use crate::{c_api, error, Nc, NcPalette, NcPaletteIndex, NcResult, NcRgb};
4
5impl NcPalette {
6 /// Returns a new `NcPalette`.
7 ///
8 /// *C style function: [ncpalette_new()][c_api::ncpalette_new].*
9 pub fn new<'a>(nc: &mut Nc) -> &'a mut Self {
10 unsafe { &mut *c_api::ncpalette_new(nc) }
11 }
12
13 /// Frees this `NcPalette`.
14 ///
15 /// *C style function: [ncpalette_free()][c_api::ncpalette_free].*
16 pub fn free(&mut self) {
17 unsafe {
18 c_api::ncpalette_free(self);
19 }
20 }
21
22 /// Attempts to configure the terminal with this `NcPalette`.
23 ///
24 /// *C style function: [ncpalette_use()][c_api::ncpalette_use].*
25 pub fn r#use(&self, nc: &mut Nc) -> NcResult<()> {
26 error![unsafe { c_api::ncpalette_use(nc, self) }]
27 }
28
29 /// Returns the [`NcRgb`] value from an [`NcChannel`][crate::NcChannel]
30 /// entry inside this `NcPalette`.
31 ///
32 /// *C style function: [ncpalette_get()][c_api::ncpalette_get].*
33 pub fn get(&self, index: impl Into<NcPaletteIndex>) -> NcRgb {
34 c_api::ncpalette_get(self, index.into()).into()
35 }
36
37 /// Sets the [`NcRgb`] value of the [`NcChannel`][crate::NcChannel] entry
38 /// inside this NcPalette.
39 ///
40 /// *C style function: [ncpalette_set()][c_api::ncpalette_set].*
41 pub fn set(&mut self, index: impl Into<NcPaletteIndex>, rgb: impl Into<NcRgb>) {
42 c_api::ncpalette_set(self, index.into(), rgb.into().into())
43 }
44}