use crate::heatmap::Gradient;
use ratatui::{buffer::Buffer, layout::Rect, style::Color, style::Style};
#[cfg(feature = "theme")]
pub use tui_nuances::Theme;
#[cfg(feature = "theme")]
pub trait ChartThemeExt {
fn gradient(&self) -> Gradient;
fn crosshair_style(&self) -> Style;
fn tooltip_style(&self) -> Style;
fn series_color(&self, i: usize) -> Color;
}
#[cfg(feature = "theme")]
impl ChartThemeExt for Theme {
fn gradient(&self) -> Gradient {
let accent = to_rgb(self.accent);
let low = match self.background {
Some(bg) => mix(to_rgb(bg), accent, 0.2),
None => mix((0, 0, 0), accent, 0.25),
};
let high = to_rgb(self.color(1));
Gradient::Custom(vec![low, accent, high])
}
fn crosshair_style(&self) -> Style {
Style::default().fg(self.crosshair)
}
fn tooltip_style(&self) -> Style {
Style::default().fg(self.tooltip_fg).bg(self.tooltip_bg)
}
fn series_color(&self, i: usize) -> Color {
self.color(i)
}
}
#[cfg(feature = "theme")]
pub(crate) fn to_rgb(c: Color) -> (u8, u8, u8) {
match c {
Color::Rgb(r, g, b) => (r, g, b),
Color::Black => (0, 0, 0),
Color::Red => (205, 49, 49),
Color::Green => (13, 188, 121),
Color::Yellow => (229, 229, 16),
Color::Blue => (36, 114, 200),
Color::Magenta => (188, 63, 188),
Color::Cyan => (17, 168, 205),
Color::Gray => (180, 180, 180),
Color::DarkGray => (102, 102, 102),
Color::LightRed => (241, 76, 76),
Color::LightGreen => (35, 209, 139),
Color::LightYellow => (245, 245, 67),
Color::LightBlue => (59, 142, 234),
Color::LightMagenta => (214, 112, 214),
Color::LightCyan => (41, 184, 219),
Color::White => (255, 255, 255),
_ => (128, 128, 128),
}
}
#[cfg(feature = "theme")]
use crate::scale::mix_rgb as mix;
pub(crate) fn begin(
block: &Option<ratatui::widgets::Block<'_>>,
theme: &Option<Theme>,
style: Option<Style>,
area: Rect,
buf: &mut Buffer,
) -> Rect {
use ratatui::widgets::Widget;
if let Some(t) = theme {
t.fill(area, buf);
}
if let Some(s) = style {
buf.set_style(area, s);
}
match block {
Some(b) => {
let inner = b.inner(area);
let mut b = b.clone();
if let Some(t) = theme {
b = b
.style(Style::default().fg(t.border))
.title_style(Style::default().fg(t.title));
}
b.render(area, buf);
inner
}
None => area,
}
}
#[cfg(not(feature = "theme"))]
#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub(crate) enum Theme {}
#[cfg(not(feature = "theme"))]
#[allow(dead_code)]
pub(crate) struct ThemeFields {
pub background: Option<Color>,
pub foreground: Color,
pub muted: Color,
pub border: Color,
pub title: Color,
pub accent: Color,
pub axis: Color,
pub grid: Color,
pub bull: Color,
pub bear: Color,
pub neutral: Color,
pub progress: Color,
pub crosshair: Color,
pub tooltip_fg: Color,
pub tooltip_bg: Color,
}
#[cfg(not(feature = "theme"))]
#[allow(dead_code)]
impl Theme {
pub fn color(&self, _i: usize) -> Color {
match *self {}
}
pub fn axis_style(&self) -> Style {
match *self {}
}
pub fn label_style(&self) -> Style {
match *self {}
}
pub fn grid_style(&self) -> Style {
match *self {}
}
pub fn fill(&self, _area: Rect, _buf: &mut Buffer) {
match *self {}
}
}
#[cfg(not(feature = "theme"))]
impl std::ops::Deref for Theme {
type Target = ThemeFields;
fn deref(&self) -> &ThemeFields {
match *self {}
}
}
#[cfg(not(feature = "theme"))]
#[allow(dead_code)]
pub(crate) trait ChartThemeExt {
fn gradient(&self) -> Gradient;
}
#[cfg(not(feature = "theme"))]
impl ChartThemeExt for Theme {
fn gradient(&self) -> Gradient {
match *self {}
}
}
#[cfg(all(test, feature = "theme"))]
mod tests {
use super::*;
#[test]
fn gradient_from_theme() {
for t in Theme::all() {
assert!(matches!(t.gradient(), Gradient::Custom(v) if v.len() == 3));
}
}
#[test]
fn ext_styles() {
let t = Theme::nord();
assert_eq!(t.crosshair_style().fg, Some(t.crosshair));
assert_eq!(t.tooltip_style().bg, Some(t.tooltip_bg));
assert_eq!(t.series_color(0), t.series_color(t.palette.len()));
}
#[test]
fn fill_paints_background() {
let mut b = Buffer::empty(Rect::new(0, 0, 3, 2));
Theme::dracula().fill(Rect::new(0, 0, 3, 2), &mut b);
assert_eq!(b[(1, 1)].bg, Theme::dracula().background.unwrap());
}
}