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")]
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,
}
}
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;
use self::super::find_closest;
use self::super::{color_id, find_closest};
use backend;
use event::{Event, Key};
use theme::{Color, ColorStyle, Effect};
@ -23,7 +23,7 @@ impl backend::Backend for Concrete {
ncurses::start_color();
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
ncurses::wbkgd(ncurses::stdscr(),
ncurses::COLOR_PAIR(ColorStyle::Background.id()));
ncurses::COLOR_PAIR(color_id(ColorStyle::Background)));
Concrete
}
@ -48,7 +48,7 @@ impl backend::Backend for Concrete {
background: &Color) {
// 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(background) as i16);
}
@ -58,7 +58,7 @@ impl backend::Backend for Concrete {
let mut current_color: i16 = 0;
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);
f();
// 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 event::{Event, Key};
use theme::{Color, ColorStyle, Effect};
@ -21,7 +21,7 @@ impl backend::Backend for Concrete {
pancurses::cbreak();
pancurses::start_color();
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 }
}
@ -41,14 +41,14 @@ impl backend::Backend for Concrete {
fn init_color_style(&mut self, style: ColorStyle, foreground: &Color,
background: &Color) {
pancurses::init_pair(style.id(),
pancurses::init_pair(color_id(style),
find_closest(foreground) as i16,
find_closest(background) as i16);
}
fn with_color<F: FnOnce()>(&self, color: ColorStyle, f: F) {
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);
f();

View File

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