ncursesw/normal/color.rs
1/*
2 src/normal/color.rs
3
4 Copyright (c) 2019-2021 Stephen Whittle All rights reserved.
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom
11 the Software is furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 IN THE SOFTWARE.
21*/
22
23#![allow(deprecated)]
24
25use crate::{
26 NCurseswError,
27 gen::ColorType,
28 ncursescolortype::{set_ncurses_colortype, NCursesColorType},
29 shims::ncurses::{short_t, SCREEN},
30 normal::{ColorPalette, RGB},
31 ncurses::{init_color, color_content, init_color_sp, color_content_sp}
32};
33
34/// A terminal color.
35#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
36pub struct Color {
37 screen: Option<SCREEN>,
38 color_palette: ColorPalette
39}
40
41impl Color {
42 pub(in crate) fn _from(screen: Option<SCREEN>, color_palette: ColorPalette) -> Self {
43 assert!(screen.map_or_else(|| true, |screen| !screen.is_null()), "Color::_from() : screen.is_null()");
44
45 set_ncurses_colortype(NCursesColorType::Normal);
46
47 Self { screen, color_palette }
48 }
49}
50
51impl Color {
52 pub fn new(color_palette: ColorPalette) -> Self {
53 Self::_from(None, color_palette)
54 }
55
56 pub fn new_sp(screen: SCREEN, color_palette: ColorPalette) -> Self {
57 Self::_from(Some(screen), color_palette)
58 }
59
60 /// # Safety
61 ///
62 /// Set the screen of the `Color`.
63 ///
64 /// Use with caution!!! This function only need's to be used if using the screen type
65 /// functions and is provided to allow the alignment of the screen pointer with the
66 /// screen that the `ColorPair` are for as this crate will apply a screen of `None`
67 /// by default when retriving `Attributes` from functions such as `attr_get()` and
68 /// `wattr_get()`.
69 pub unsafe fn set_screen(&mut self, screen: Option<SCREEN>) {
70 self.screen = screen
71 }
72
73 pub fn screen(&self) -> Option<SCREEN> {
74 self.screen
75 }
76
77 pub fn color_palette(&self) -> ColorPalette {
78 self.color_palette
79 }
80
81 pub fn set_rgb(&self, rgb: RGB) -> result!(()) {
82 match self.screen {
83 None => init_color(self.color_palette.number(), rgb),
84 Some(screen) => init_color_sp(screen, self.color_palette.number(), rgb)
85 }
86 }
87
88 pub fn rgb(&self) -> result!(RGB) {
89 match self.screen {
90 None => color_content(self.color_palette.number()),
91 Some(screen) => color_content_sp(screen, self.color_palette.number())
92 }
93 }
94}
95
96impl Default for Color {
97 fn default() -> Self {
98 Self::_from(None, ColorPalette::default())
99 }
100}
101
102impl ColorType<short_t> for Color {
103 fn number(&self) -> i32 {
104 i32::from(self.color_palette.number())
105 }
106}