use std::fmt;
use urushi_terminal::TerminalBackground;
use crate::{BlockStyle, List, Scrollbar, Table, TextStyle, Tree, View};
use super::{BlockThemeRole, ComponentTheme, SemanticTokens, TextThemeRole};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorScheme {
Light,
Dark,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ThemeMode {
Light,
Dark,
Auto { fallback: ColorScheme },
}
impl ThemeMode {
pub fn resolve(self, background: Option<TerminalBackground>) -> ColorScheme {
match self {
Self::Light => ColorScheme::Light,
Self::Dark => ColorScheme::Dark,
Self::Auto { fallback } => background.map_or(fallback, classify_background),
}
}
}
fn classify_background(background: TerminalBackground) -> ColorScheme {
let red = linear_srgb(background.red());
let green = linear_srgb(background.green());
let blue = linear_srgb(background.blue());
let luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
if luminance < 0.5 {
ColorScheme::Dark
} else {
ColorScheme::Light
}
}
fn linear_srgb(channel: u16) -> f64 {
let encoded = f64::from(channel) / f64::from(u16::MAX);
if encoded <= 0.04045 {
encoded / 12.92
} else {
((encoded + 0.055) / 1.055).powf(2.4)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Theme {
tokens: SemanticTokens,
components: ComponentTheme,
}
impl Theme {
pub fn from_tokens(tokens: SemanticTokens) -> Self {
let components = ComponentTheme::from_tokens(&tokens);
Self::new(tokens, components)
}
pub fn new(tokens: SemanticTokens, components: ComponentTheme) -> Self {
Self { tokens, components }
}
pub fn tokens(&self) -> &SemanticTokens {
&self.tokens
}
pub fn components(&self) -> &ComponentTheme {
&self.components
}
pub fn list<T>(&self, list: &List<T>) -> View
where
T: fmt::Display,
{
self.components.list().compose(list)
}
pub fn tree<T>(&self, tree: &Tree<T>) -> View
where
T: fmt::Display,
{
self.components.tree().compose(tree)
}
pub fn table<T>(&self, table: &Table<T>) -> View
where
T: crate::TableRow,
{
self.components.table().compose(table)
}
pub fn scrollbar(&self, scrollbar: &Scrollbar) -> View {
self.components.scrollbar().compose(scrollbar)
}
pub fn text_style<R>(&self, role: R) -> TextStyle
where
R: TextThemeRole,
{
role.resolve(self)
}
pub fn block_style<R>(&self, role: R) -> BlockStyle
where
R: BlockThemeRole,
{
role.resolve(self)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ThemeSet {
light: Theme,
dark: Theme,
}
impl ThemeSet {
pub fn new(light: Theme, dark: Theme) -> Self {
Self { light, dark }
}
pub fn light(&self) -> &Theme {
&self.light
}
pub fn dark(&self) -> &Theme {
&self.dark
}
pub fn select(&self, scheme: ColorScheme) -> &Theme {
match scheme {
ColorScheme::Light => self.light(),
ColorScheme::Dark => self.dark(),
}
}
}