pancurses_result/
color.rs

1use general::*;
2use std::marker::PhantomData;
3
4/// A color value represented as RGB
5pub struct ColorContent {
6    pub red: i16,
7    pub green: i16,
8    pub blue: i16,
9}
10impl From<(i16, i16, i16)> for ColorContent {
11    fn from(p: (i16, i16, i16)) -> Self {
12        ColorContent {
13            red: p.0,
14            green: p.1,
15            blue: p.2,
16        }
17    }
18}
19impl From<ColorContent> for (i16, i16, i16) {
20    fn from(color_content: ColorContent) -> (i16, i16, i16) {
21        (color_content.red, color_content.green, color_content.blue)
22    }
23}
24
25/// Color subsystem.  It can be accessed via [`Curses::color`].
26///
27/// [`Curses::color`]: struct.Curses.html#method.color
28pub struct Color {
29    marker: PhantomData<()>,
30}
31
32impl Color {
33    pub(crate) fn new() -> Self {
34        Color {
35            marker: PhantomData,
36        }
37    }
38
39    /// The maximum number of colors supported.
40    ///
41    /// This corresponds to `COLORS`.
42    pub fn max_colors(&self) -> i32 {
43        pancurses::COLORS()
44    }
45    /// Get the `n`th color pair.
46    ///
47    /// This corresponds to `COLOR_PAIR`.
48    pub fn color_pair<T: Into<Chtype>>(&self, n: T) -> Chtype {
49        pancurses::COLOR_PAIR(n.into())
50    }
51    /// Get the number of color pairs.
52    ///
53    /// This corresponds to `COLOR_PAIRS`.
54    pub fn color_pairs(&self) -> i32 {
55        pancurses::COLOR_PAIRS()
56    }
57    /// Get the [`ColorContent`] of a certain color.
58    ///
59    /// [`ColorContent`]: struct.ColorContent.html
60    pub fn color_content(&self, color: i16) -> ColorContent {
61        pancurses::color_content(color).into()
62    }
63    /// Is it possible to change colors?
64    pub fn can_change_color(&self) -> bool {
65        pancurses::can_change_color()
66    }
67    /// Tell the curses instance to use default colors.
68    pub fn use_default_colors(&mut self) -> Result<(), ()> {
69        check(pancurses::use_default_colors())
70    }
71    /// Set the nth color to a certain [`ColorContent`].
72    ///
73    /// This corresponds to `init_color`.
74    ///
75    /// [`ColorContent`]: struct.ColorContent.html
76    pub fn set_color(&mut self, color: i16, color_content: ColorContent) -> Result<(), ()> {
77        check(pancurses::init_color(
78            color,
79            color_content.red,
80            color_content.green,
81            color_content.blue,
82        ))
83    }
84    /// Set the `color_pair` to a combination of the `foregrond` and `background` colors.
85    ///
86    /// This corresponds to `init_pair`.
87    pub fn set_color_pair(
88        &mut self,
89        color_pair: i16,
90        foreground: i16,
91        background: i16,
92    ) -> Result<(), ()> {
93        check(pancurses::init_pair(color_pair, foreground, background))
94    }
95}