use plannotator_tui_schema::Kind;
use ratatui::style::{Color, Style};
use crate::theme::{Theme, theme};
pub(crate) struct Chips {
pub(crate) send: Style,
pub(crate) sent: Style,
pub(crate) send_blocked: Style,
pub(crate) send_idle_fg: Color,
pub(crate) approve: Style,
pub(crate) menu_fg: Color,
#[cfg_attr(not(unix), allow(dead_code, reason = "only the code review reads it"))]
pub(crate) switch_on: Style,
#[cfg_attr(not(unix), allow(dead_code, reason = "only the code review reads it"))]
pub(crate) whitespace_hidden: Style,
#[cfg_attr(not(unix), allow(dead_code, reason = "only the code review reads it"))]
pub(crate) switch_off_fg: Color,
toolbar_comment: Color,
toolbar_looks_good: Color,
toolbar_delete: Color,
#[cfg_attr(not(unix), allow(dead_code, reason = "only the code review reads it"))]
pub(crate) review_suggest: Color,
}
impl Chips {
pub(crate) fn toolbar(&self, kind: Kind) -> Color {
match kind {
Kind::Comment => self.toolbar_comment,
Kind::LooksGood => self.toolbar_looks_good,
Kind::Delete => self.toolbar_delete,
}
}
#[cfg_attr(not(unix), allow(dead_code, reason = "only the code review reads it"))]
pub(crate) fn review_comment(&self) -> Color {
self.toolbar_comment
}
}
const BLACK: Color = Color::Indexed(16);
const SEND: Style = Style::new().fg(BLACK).bg(Color::Indexed(37)).bold();
const SEND_BLOCKED: Style = Style::new().fg(BLACK).bg(Color::Indexed(214)).bold();
const APPROVE: Style = Style::new().fg(BLACK).bg(Color::Indexed(34)).bold();
static DARK: Chips = Chips {
send: SEND,
sent: Style::new().fg(Color::Indexed(194)).bg(Color::Indexed(22)),
send_blocked: SEND_BLOCKED,
send_idle_fg: Color::Indexed(250),
approve: APPROVE,
menu_fg: Color::Indexed(80),
switch_on: Style::new().fg(BLACK).bg(Color::Indexed(250)).bold(),
whitespace_hidden: Style::new().fg(BLACK).bg(Color::Indexed(222)).bold(),
switch_off_fg: Color::Indexed(250),
toolbar_comment: Color::Indexed(221),
toolbar_looks_good: Color::Indexed(114),
toolbar_delete: Color::Indexed(217),
review_suggest: Color::Indexed(80),
};
static LIGHT: Chips = Chips {
send: SEND,
sent: Style::new().fg(Color::Indexed(22)).bg(Color::Indexed(194)),
send_blocked: SEND_BLOCKED,
send_idle_fg: Color::Indexed(239),
approve: APPROVE,
menu_fg: Color::Indexed(23),
switch_on: Style::new().fg(Color::Indexed(231)).bg(Color::Indexed(239)).bold(),
whitespace_hidden: Style::new().fg(Color::Indexed(231)).bg(Color::Indexed(94)).bold(),
switch_off_fg: Color::Indexed(239),
toolbar_comment: Color::Indexed(58),
toolbar_looks_good: Color::Indexed(22),
toolbar_delete: Color::Indexed(124),
review_suggest: Color::Indexed(23),
};
pub(crate) fn chips() -> &'static Chips {
of(theme())
}
pub(crate) fn of(theme: Theme) -> &'static Chips {
match theme {
Theme::Dark => &DARK,
Theme::Light => &LIGHT,
}
}
#[cfg(test)]
#[allow(clippy::expect_used, reason = "tests assert by panicking")]
mod tests {
use super::*;
use crate::theme::Palette;
use crate::ui::send_button::{SendState, style_in};
fn rgb(color: Color) -> Option<[f64; 3]> {
let Color::Indexed(index @ 16..) = color else { return None };
let level = |step: u8| if step == 0 { 0.0 } else { f64::from(55 + 40 * step) };
Some(match index {
16..=231 => {
let cube = index - 16;
[level(cube / 36), level(cube / 6 % 6), level(cube % 6)]
}
_ => [f64::from(8 + 10 * (index - 232)); 3],
})
}
fn luminance([r, g, b]: [f64; 3]) -> f64 {
let linear = |channel: f64| {
let c = channel / 255.0;
if c <= 0.04045 { c / 12.92 } else { ((c + 0.055) / 1.055).powf(2.4) }
};
0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b)
}
fn pairs(theme: Theme, palette: &Palette) -> Vec<(String, Style)> {
let chips = of(theme);
let on_toolbar = |fg: Color| Style::new().fg(fg).bg(palette.toolbar_bg);
let mut pairs = vec![
("send, new notes", style_in(theme, palette, &SendState::Ready, 3)),
("send, nothing to send", style_in(theme, palette, &SendState::Ready, 0)),
("send, sent", style_in(theme, palette, &SendState::Sent, 0)),
("send, at a dialog", style_in(theme, palette, &SendState::Blocked("dialog".into()), 3)),
("approve", chips.approve),
("review menu", on_toolbar(chips.menu_fg)),
("header switch, in force", chips.switch_on),
("header switch, the other option", on_toolbar(chips.switch_off_fg)),
("whitespace hidden", chips.whitespace_hidden),
("toolbar comment", on_toolbar(chips.toolbar(Kind::Comment))),
("toolbar looks good", on_toolbar(chips.toolbar(Kind::LooksGood))),
("toolbar delete", on_toolbar(chips.toolbar(Kind::Delete))),
("review toolbar comment", on_toolbar(chips.review_comment())),
("review toolbar suggest", on_toolbar(chips.review_suggest)),
];
let styles = [
("cursor", palette.cursor),
("selection", palette.selection),
("search", palette.search),
("search current", palette.search_current),
];
pairs.extend(styles.into_iter().filter(|(_, style)| style.fg.is_some() && style.bg.is_some()));
pairs.into_iter().map(|(name, style)| (name.to_owned(), style)).collect()
}
#[test]
fn every_painted_pair_reads_at_least_4_5_to_1_in_both_themes_without_ansi_colours() {
for (theme, palette) in [(Theme::Dark, &crate::theme::DARK), (Theme::Light, &crate::theme::LIGHT)] {
for (name, style) in pairs(theme, palette) {
let fg = style.fg.and_then(rgb).expect("a fixed 256-colour foreground");
let bg = style.bg.and_then(rgb).expect("a fixed 256-colour background");
let (fg, bg) = (luminance(fg), luminance(bg));
let ratio = (fg.max(bg) + 0.05) / (fg.min(bg) + 0.05);
assert!(ratio >= 4.5, "{theme:?} {name}: {ratio:.2}:1 ({style:?})");
}
}
}
}