use std::collections::HashMap;
use ratatui::style::Color;
pub struct Palette(HashMap<String, Color>);
impl Palette {
pub fn new(colors: HashMap<String, Color>) -> Self {
Self(colors)
}
pub fn resolve(&self, name: Option<&str>) -> Result<Option<Color>, String> {
name.map(|name| {
self.0
.get(name)
.copied()
.ok_or_else(|| format!("unknown palette color `{name}`"))
})
.transpose()
}
#[cfg(test)]
pub fn colors(&self) -> Vec<(&str, Color)> {
let mut colors = self
.0
.iter()
.map(|(name, &color)| (name.as_str(), color))
.collect::<Vec<_>>();
colors.sort_unstable_by_key(|&(name, _)| name);
colors
}
#[cfg(test)]
pub fn defines(&self, color: &str) -> bool {
self.0.contains_key(color)
}
#[cfg(test)]
pub fn color_count(&self) -> usize {
self.0.len()
}
}