From b799d830779d232ba25eb5004aafb5a24d3f48fe Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sun, 9 Apr 2017 13:30:24 -0700 Subject: [PATCH] Add `ColorStyle::Custom` for theme-independent coloring Moved `color_id` to curses backend. --- src/backend/curses/mod.rs | 18 ++++++++++- src/backend/curses/n.rs | 8 ++--- src/backend/curses/pan.rs | 8 ++--- src/theme.rs | 64 ++++++++++++++++----------------------- 4 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/backend/curses/mod.rs b/src/backend/curses/mod.rs index fa5a6df..e0ec9fc 100644 --- a/src/backend/curses/mod.rs +++ b/src/backend/curses/mod.rs @@ -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, + + } +} diff --git a/src/backend/curses/n.rs b/src/backend/curses/n.rs index 467a6c2..0b04e47 100644 --- a/src/backend/curses/n.rs +++ b/src/backend/curses/n.rs @@ -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); diff --git a/src/backend/curses/pan.rs b/src/backend/curses/pan.rs index 062df26..786d544 100644 --- a/src/backend/curses/pan.rs +++ b/src/backend/curses/pan.rs @@ -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(&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(); diff --git a/src/theme.rs b/src/theme.rs index 5b4f0e4..803bad6 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -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 for Error { impl Color { fn parse(value: &str) -> Option { 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 {