use super::Color;
use enum_map::EnumMap;
use toml;
pub type Palette = EnumMap<PaletteColor, Color>;
pub fn default_palette() -> Palette {
use self::PaletteColor::*;
use theme::BaseColor::*;
use theme::Color::*;
enum_map!{
Background => Dark(Blue),
Shadow => Dark(Black),
View => Dark(White),
Primary => Dark(Black),
Secondary => Dark(Blue),
Tertiary => Dark(White),
TitlePrimary => Dark(Red),
TitleSecondary => Dark(Yellow),
Highlight => Dark(Red),
HighlightInactive => Dark(Blue),
}
}
pub(crate) fn load_table(palette: &mut Palette, table: &toml::value::Table) {
load_color(
&mut palette[PaletteColor::Background],
table.get("background"),
);
load_color(&mut palette[PaletteColor::Shadow], table.get("shadow"));
load_color(&mut palette[PaletteColor::View], table.get("view"));
load_color(&mut palette[PaletteColor::Primary], table.get("primary"));
load_color(
&mut palette[PaletteColor::Secondary],
table.get("secondary"),
);
load_color(&mut palette[PaletteColor::Tertiary], table.get("tertiary"));
load_color(
&mut palette[PaletteColor::TitlePrimary],
table.get("title_primary"),
);
load_color(
&mut palette[PaletteColor::TitleSecondary],
table.get("title_secondary"),
);
load_color(
&mut palette[PaletteColor::Highlight],
table.get("highlight"),
);
load_color(
&mut palette[PaletteColor::HighlightInactive],
table.get("highlight_inactive"),
);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, EnumMap)]
pub enum PaletteColor {
Background,
Shadow,
View,
Primary,
Secondary,
Tertiary,
TitlePrimary,
TitleSecondary,
Highlight,
HighlightInactive,
}
impl PaletteColor {
pub fn resolve(self, palette: &Palette) -> Color {
palette[self]
}
}
fn load_color(target: &mut Color, value: Option<&toml::Value>) -> bool {
if let Some(value) = value {
match *value {
toml::Value::String(ref value) => {
if let Some(color) = Color::parse(value) {
*target = color;
true
} else {
false
}
}
toml::Value::Array(ref array) => {
array.iter().any(|item| load_color(target, Some(item)))
}
_ => false,
}
} else {
false
}
}