Add ColorStyle::Custom for theme-independent coloring

Moved `color_id` to curses backend.
This commit is contained in:
Alexandre Bury 2017-04-09 13:30:24 -07:00
parent f3f10b7088
commit b799d83077
4 changed files with 51 additions and 47 deletions

View File

@ -1,4 +1,4 @@
use theme::{BaseColor, Color}; use theme::{BaseColor, Color, ColorStyle};
#[cfg(feature = "ncurses")] #[cfg(feature = "ncurses")]
mod n; mod n;
@ -38,3 +38,19 @@ fn find_closest(color: &Color) -> u8 {
Color::RgbLowRes(r, g, b) => (16 + 36 * r + 6 * g + b) as u8, Color::RgbLowRes(r, g, b) => (16 + 36 * r + 6 * g + b) as u8,
} }
} }
fn color_id(style: ColorStyle) -> i16 {
match style {
ColorStyle::Background => 1,
ColorStyle::Shadow => 2,
ColorStyle::Primary => 3,
ColorStyle::Secondary => 4,
ColorStyle::Tertiary => 5,
ColorStyle::TitlePrimary => 6,
ColorStyle::TitleSecondary => 7,
ColorStyle::Highlight => 8,
ColorStyle::HighlightInactive => 9,
ColorStyle::Custom { front, back } => 10,
}
}

View File

@ -1,7 +1,7 @@
extern crate ncurses; extern crate ncurses;
use self::super::find_closest; use self::super::{color_id, find_closest};
use backend; use backend;
use event::{Event, Key}; use event::{Event, Key};
use theme::{Color, ColorStyle, Effect}; use theme::{Color, ColorStyle, Effect};
@ -23,7 +23,7 @@ impl backend::Backend for Concrete {
ncurses::start_color(); ncurses::start_color();
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE); ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
ncurses::wbkgd(ncurses::stdscr(), ncurses::wbkgd(ncurses::stdscr(),
ncurses::COLOR_PAIR(ColorStyle::Background.id())); ncurses::COLOR_PAIR(color_id(ColorStyle::Background)));
Concrete Concrete
} }
@ -48,7 +48,7 @@ impl backend::Backend for Concrete {
background: &Color) { background: &Color) {
// TODO: build the color on the spot // TODO: build the color on the spot
ncurses::init_pair(style.id(), ncurses::init_pair(color_id(style),
find_closest(foreground) as i16, find_closest(foreground) as i16,
find_closest(background) as i16); find_closest(background) as i16);
} }
@ -58,7 +58,7 @@ impl backend::Backend for Concrete {
let mut current_color: i16 = 0; let mut current_color: i16 = 0;
ncurses::attr_get(&mut current_style, &mut current_color); ncurses::attr_get(&mut current_style, &mut current_color);
let style = ncurses::COLOR_PAIR(color.id()); let style = ncurses::COLOR_PAIR(color_id(color));
ncurses::attron(style); ncurses::attron(style);
f(); f();
// ncurses::attroff(style); // ncurses::attroff(style);

View File

@ -2,7 +2,7 @@ extern crate pancurses;
use self::super::find_closest; use self::super::{color_id, find_closest};
use backend; use backend;
use event::{Event, Key}; use event::{Event, Key};
use theme::{Color, ColorStyle, Effect}; use theme::{Color, ColorStyle, Effect};
@ -21,7 +21,7 @@ impl backend::Backend for Concrete {
pancurses::cbreak(); pancurses::cbreak();
pancurses::start_color(); pancurses::start_color();
pancurses::curs_set(0); pancurses::curs_set(0);
window.bkgd(pancurses::ColorPair(ColorStyle::Background.id() as u8)); window.bkgd(pancurses::ColorPair(color_id(ColorStyle::Background) as u8));
Concrete { window: window } Concrete { window: window }
} }
@ -41,14 +41,14 @@ impl backend::Backend for Concrete {
fn init_color_style(&mut self, style: ColorStyle, foreground: &Color, fn init_color_style(&mut self, style: ColorStyle, foreground: &Color,
background: &Color) { background: &Color) {
pancurses::init_pair(style.id(), pancurses::init_pair(color_id(style),
find_closest(foreground) as i16, find_closest(foreground) as i16,
find_closest(background) as i16); find_closest(background) as i16);
} }
fn with_color<F: FnOnce()>(&self, color: ColorStyle, f: F) { fn with_color<F: FnOnce()>(&self, color: ColorStyle, f: F) {
let (_, current_color_pair) = self.window.attrget(); let (_, current_color_pair) = self.window.attrget();
let color_attribute = pancurses::ColorPair(color.id() as u8); let color_attribute = pancurses::ColorPair(color_id(color) as u8);
self.window.attron(color_attribute); self.window.attron(color_attribute);
f(); f();

View File

@ -158,23 +158,13 @@ pub enum ColorStyle {
Highlight, Highlight,
/// Highlight color for inactive views (not in focus). /// Highlight color for inactive views (not in focus).
HighlightInactive, HighlightInactive,
} /// Directly specifies colors, independently of the theme.
Custom {
impl ColorStyle { /// Foreground color
/// Returns the ncurses pair ID associated with this color pair. front: Color,
pub fn id(self) -> i16 { /// Background color
match self { back: Color
ColorStyle::Background => 1, },
ColorStyle::Shadow => 2,
ColorStyle::Primary => 3,
ColorStyle::Secondary => 4,
ColorStyle::Tertiary => 5,
ColorStyle::TitlePrimary => 6,
ColorStyle::TitleSecondary => 7,
ColorStyle::Highlight => 8,
ColorStyle::HighlightInactive => 9,
}
}
} }
/// Represents the style a Cursive application will use. /// Represents the style a Cursive application will use.
@ -288,7 +278,7 @@ impl BorderStyle {
/// Color configuration for the application. /// Color configuration for the application.
/// ///
/// Assign each color role an actual color. /// Assign each color role an actual color.
#[derive(Clone,Debug)] #[derive(Copy,Clone,Debug)]
pub struct Palette { pub struct Palette {
/// Color used for the application background. /// Color used for the application background.
pub background: Color, pub background: Color,
@ -405,8 +395,6 @@ pub enum Color {
RgbLowRes(u8, u8, u8), RgbLowRes(u8, u8, u8),
} }
impl Color {}
/// Possible error returned when loading a theme. /// Possible error returned when loading a theme.
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
@ -431,24 +419,24 @@ impl From<toml::de::Error> for Error {
impl Color { impl Color {
fn parse(value: &str) -> Option<Self> { fn parse(value: &str) -> Option<Self> {
Some(match value { Some(match value {
"black" => Color::Dark(BaseColor::Black), "black" => Color::Dark(BaseColor::Black),
"red" => Color::Dark(BaseColor::Red), "red" => Color::Dark(BaseColor::Red),
"green" => Color::Dark(BaseColor::Green), "green" => Color::Dark(BaseColor::Green),
"yellow" => Color::Dark(BaseColor::Yellow), "yellow" => Color::Dark(BaseColor::Yellow),
"blue" => Color::Dark(BaseColor::Blue), "blue" => Color::Dark(BaseColor::Blue),
"magenta" => Color::Dark(BaseColor::Magenta), "magenta" => Color::Dark(BaseColor::Magenta),
"cyan" => Color::Dark(BaseColor::Cyan), "cyan" => Color::Dark(BaseColor::Cyan),
"white" => Color::Dark(BaseColor::White), "white" => Color::Dark(BaseColor::White),
"light black" => Color::Light(BaseColor::Black), "light black" => Color::Light(BaseColor::Black),
"light red" => Color::Light(BaseColor::Red), "light red" => Color::Light(BaseColor::Red),
"light green" => Color::Light(BaseColor::Green), "light green" => Color::Light(BaseColor::Green),
"light yellow" => Color::Light(BaseColor::Yellow), "light yellow" => Color::Light(BaseColor::Yellow),
"light blue" => Color::Light(BaseColor::Blue), "light blue" => Color::Light(BaseColor::Blue),
"light magenta" => Color::Light(BaseColor::Magenta), "light magenta" => Color::Light(BaseColor::Magenta),
"light cyan" => Color::Light(BaseColor::Cyan), "light cyan" => Color::Light(BaseColor::Cyan),
"light white" => Color::Light(BaseColor::White), "light white" => Color::Light(BaseColor::White),
value => return Color::parse_special(value), value => return Color::parse_special(value),
}) })
} }
fn parse_special(value: &str) -> Option<Color> { fn parse_special(value: &str) -> Option<Color> {