use super::Color;
use enum_map::{enum_map, Enum, EnumMap};
#[cfg(feature = "toml")]
use log::warn;
use std::ops::{Index, IndexMut};
type HashMap<K, V> = std::collections::HashMap<K, V, ahash::ABuildHasher>;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Palette {
basic: EnumMap<PaletteColor, Color>,
custom: HashMap<String, PaletteNode>,
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum PaletteNode {
Color(Color),
Namespace(HashMap<String, PaletteNode>),
}
impl Index<PaletteColor> for Palette {
type Output = Color;
fn index(&self, palette_color: PaletteColor) -> &Color {
&self.basic[palette_color]
}
}
impl IndexMut<PaletteColor> for Palette {
fn index_mut(&mut self, palette_color: PaletteColor) -> &mut Color {
&mut self.basic[palette_color]
}
}
impl Palette {
pub fn custom<'a>(&'a self, key: &str) -> Option<&'a Color> {
self.custom.get(key).and_then(|node| {
if let PaletteNode::Color(ref color) = *node {
Some(color)
} else {
None
}
})
}
pub fn merge(&self, namespace: &str) -> Palette {
let mut result = self.clone();
if let Some(&PaletteNode::Namespace(ref palette)) =
self.custom.get(namespace)
{
for (key, value) in palette.iter() {
match *value {
PaletteNode::Color(color) => result.set_color(key, color),
PaletteNode::Namespace(ref map) => {
result.add_namespace(key, map.clone())
}
}
}
}
result
}
pub fn set_color(&mut self, key: &str, color: Color) {
use crate::theme::PaletteColor::*;
match key {
"background" => self.basic[Background] = color,
"shadow" => self.basic[Shadow] = color,
"view" => self.basic[View] = color,
"primary" => self.basic[Primary] = color,
"secondary" => self.basic[Secondary] = color,
"tertiary" => self.basic[Tertiary] = color,
"title_primary" => self.basic[TitlePrimary] = color,
"title_secondary" => self.basic[TitleSecondary] = color,
"highlight" => self.basic[Highlight] = color,
"highlight_inactive" => self.basic[HighlightInactive] = color,
other => {
self.custom
.insert(other.to_string(), PaletteNode::Color(color));
}
}
}
pub fn add_namespace(
&mut self,
key: &str,
namespace: HashMap<String, PaletteNode>,
) {
self.custom
.insert(key.to_string(), PaletteNode::Namespace(namespace));
}
}
impl Default for Palette {
fn default() -> Palette {
use self::PaletteColor::*;
use crate::theme::BaseColor::*;
use crate::theme::Color::*;
Palette {
basic: enum_map! {
Background => Dark(Blue),
Shadow => Dark(Black),
View => Dark(White),
Primary => Dark(Black),
Secondary => Dark(Blue),
Tertiary => Light(White),
TitlePrimary => Dark(Red),
TitleSecondary => Light(Blue),
Highlight => Dark(Red),
HighlightInactive => Dark(Blue),
},
custom: HashMap::default(),
}
}
}
#[cfg(feature = "toml")]
fn iterate_toml<'a>(
table: &'a toml::value::Table,
) -> impl Iterator<Item = (&'a str, PaletteNode)> + 'a {
table.iter().flat_map(|(key, value)| {
let node = match value {
toml::Value::Table(table) => {
let map = iterate_toml(table)
.map(|(key, value)| (key.to_string(), value))
.collect();
Some(PaletteNode::Namespace(map))
}
toml::Value::Array(colors) => {
colors
.iter()
.flat_map(toml::Value::as_str)
.flat_map(Color::parse)
.map(PaletteNode::Color)
.next()
}
toml::Value::String(color) => {
Color::parse(color).map(PaletteNode::Color)
}
other => {
warn!(
"Found unexpected value in theme: {} = {:?}",
key, other
);
None
}
};
node.map(|node| (key.as_str(), node))
})
}
#[cfg(feature = "toml")]
pub(crate) fn load_toml(palette: &mut Palette, table: &toml::value::Table) {
for (key, value) in iterate_toml(table) {
match value {
PaletteNode::Color(color) => palette.set_color(key, color),
PaletteNode::Namespace(map) => palette.add_namespace(key, map),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Enum)]
pub enum PaletteColor {
Background,
Shadow,
View,
Primary,
Secondary,
Tertiary,
TitlePrimary,
TitleSecondary,
Highlight,
HighlightInactive,
}
impl PaletteColor {
pub fn resolve(self, palette: &Palette) -> Color {
palette[self]
}
}