use crate::{app::theme::PaletteColor, node::color::Color};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BackgroundColor {
Color(Color),
Palette(PaletteColor),
CurrentForeground,
}
impl From<crate::node::TextColor> for BackgroundColor {
fn from(color: crate::node::TextColor) -> Self {
match color {
crate::node::TextColor::Color(color) => Self::Color(color),
crate::node::TextColor::Palette(role) => Self::Palette(role),
}
}
}
impl BackgroundColor {
pub fn as_color(self) -> Option<Color> {
match self {
Self::Color(color) => Some(color),
Self::Palette(_) | Self::CurrentForeground => None,
}
}
pub(crate) fn resolve(&self, palette: &crate::app::theme::ThemePalette) -> Option<Color> {
match self {
Self::Color(color) => Some(*color),
Self::Palette(color) => palette.try_get(color),
Self::CurrentForeground => None,
}
}
pub(crate) fn resolve_in(
&self,
palette: &crate::app::theme::ThemePalette,
foreground: Option<Color>,
) -> Option<Color> {
match self {
Self::CurrentForeground => foreground.or_else(|| palette.try_get(&PaletteColor::Foreground)),
other => other.resolve(palette),
}
}
}
impl From<Color> for BackgroundColor {
fn from(color: Color) -> Self {
Self::Color(color)
}
}
impl From<&str> for BackgroundColor {
fn from(color: &str) -> Self {
Self::Color(Color::from(color))
}
}
impl From<PaletteColor> for BackgroundColor {
fn from(color: PaletteColor) -> Self {
Self::Palette(color)
}
}
impl From<&PaletteColor> for BackgroundColor {
fn from(color: &PaletteColor) -> Self {
Self::Palette(color.clone())
}
}