use std::sync::atomic::{AtomicU8, Ordering};
use egui::{Color32, Stroke, Visuals};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Theme {
#[default]
Dark,
Light,
HighContrastDark,
HighContrastLight,
}
impl Theme {
pub const ALL: [Theme; 4] = [Theme::Dark, Theme::Light, Theme::HighContrastDark, Theme::HighContrastLight];
pub fn id(self) -> &'static str {
match self {
Theme::Dark => "dark",
Theme::Light => "light",
Theme::HighContrastDark => "hc-dark",
Theme::HighContrastLight => "hc-light",
}
}
pub fn from_id(id: &str) -> Option<Theme> {
Theme::ALL.into_iter().find(|t| t.id() == id)
}
pub fn label(self) -> &'static str {
match self {
Theme::Dark => "Dark",
Theme::Light => "Light",
Theme::HighContrastDark => "High contrast dark",
Theme::HighContrastLight => "High contrast light",
}
}
pub fn is_dark(self) -> bool {
matches!(self, Theme::Dark | Theme::HighContrastDark)
}
pub fn high_contrast(self) -> bool {
matches!(self, Theme::HighContrastDark | Theme::HighContrastLight)
}
pub fn flipped(self) -> Theme {
match self {
Theme::Dark => Theme::Light,
Theme::Light => Theme::Dark,
Theme::HighContrastDark => Theme::HighContrastLight,
Theme::HighContrastLight => Theme::HighContrastDark,
}
}
pub fn visuals(self) -> Visuals {
match self {
Theme::Dark => Visuals::dark(),
Theme::Light => Visuals::light(),
Theme::HighContrastDark => high_contrast(Visuals::dark(), Color32::WHITE, Color32::BLACK, Color32::from_rgb(255, 210, 0), Color32::from_rgb(0, 90, 200)),
Theme::HighContrastLight => high_contrast(Visuals::light(), Color32::BLACK, Color32::WHITE, Color32::from_rgb(0, 70, 200), Color32::from_rgb(0, 70, 200)),
}
}
}
fn high_contrast(mut v: Visuals, fg: Color32, bg: Color32, focus: Color32, selection: Color32) -> Visuals {
let dark = v.dark_mode;
let near = |x: u8| if dark { Color32::from_gray(x) } else { Color32::from_gray(255 - x) };
v.override_text_color = Some(fg);
v.weak_text_color = Some(near(200));
v.panel_fill = bg;
v.window_fill = bg;
v.extreme_bg_color = bg;
v.text_edit_bg_color = Some(bg);
v.faint_bg_color = near(28);
v.code_bg_color = near(24);
v.window_stroke = Stroke::new(1.5, fg);
v.hyperlink_color = if dark { Color32::from_rgb(120, 200, 255) } else { Color32::from_rgb(0, 60, 190) };
v.warn_fg_color = if dark { Color32::from_rgb(255, 200, 0) } else { Color32::from_rgb(150, 90, 0) };
v.error_fg_color = if dark { Color32::from_rgb(255, 120, 90) } else { Color32::from_rgb(190, 0, 0) };
v.selection.bg_fill = selection;
v.selection.stroke = Stroke::new(1.5, Color32::WHITE);
let w = &mut v.widgets;
for x in [&mut w.noninteractive, &mut w.inactive, &mut w.hovered, &mut w.active, &mut w.open] {
x.fg_stroke = Stroke::new(1.5, fg);
}
w.noninteractive.bg_fill = bg;
w.noninteractive.weak_bg_fill = bg;
w.noninteractive.bg_stroke = Stroke::new(1.0, near(170));
w.inactive.bg_fill = near(20);
w.inactive.weak_bg_fill = near(20);
w.inactive.bg_stroke = Stroke::new(1.5, near(190));
for x in [&mut w.hovered, &mut w.active, &mut w.open] {
x.bg_fill = near(45);
x.weak_bg_fill = near(45);
x.bg_stroke = Stroke::new(2.0, focus);
}
v
}
static STATE: AtomicU8 = AtomicU8::new(0);
pub fn cvd() -> bool {
STATE.load(Ordering::Relaxed) & 1 != 0
}
pub fn high_contrast_on() -> bool {
STATE.load(Ordering::Relaxed) & 2 != 0
}
pub fn apply(ctx: &egui::Context, theme: Theme, cvd: bool) {
STATE.store(u8::from(cvd) | (u8::from(theme.high_contrast()) << 1), Ordering::Relaxed);
let t = if theme.is_dark() { egui::Theme::Dark } else { egui::Theme::Light };
ctx.set_visuals_of(t, theme.visuals());
ctx.set_theme(t);
}
pub const SCALES: [u16; 5] = [100, 125, 150, 175, 200];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Tone {
Ok,
Warn,
Bad,
Info,
Found,
Caret,
}
fn cvd_tone(t: Tone, dark: bool) -> (u8, u8, u8) {
match (t, dark) {
(Tone::Ok, true) => (0, 170, 125),
(Tone::Ok, false) => (0, 115, 80),
(Tone::Warn, true) => (230, 159, 0),
(Tone::Warn, false) => (170, 100, 0),
(Tone::Bad, true) => (230, 110, 30),
(Tone::Bad, false) => (200, 80, 0),
(Tone::Info, true) => (86, 180, 233),
(Tone::Info, false) => (0, 100, 170),
(Tone::Found, true) => (240, 228, 66),
(Tone::Found, false) => (180, 140, 0),
(Tone::Caret, true) => (200, 200, 255),
(Tone::Caret, false) => (0, 70, 140),
}
}
pub fn contrast(c: Color32, dark: bool) -> Color32 {
let f = |x: u8| if dark { x + ((255 - x) as u16 * 3 / 10) as u8 } else { (x as u16 * 6 / 10) as u8 };
Color32::from_rgb(f(c.r()), f(c.g()), f(c.b()))
}
fn fill_alpha(a: u8) -> u8 {
if high_contrast_on() && a < 255 {
(a as u16 * 3 / 2).min(230) as u8
} else {
a
}
}
pub fn role(normal: Color32, t: Tone, dark: bool) -> Color32 {
if STATE.load(Ordering::Relaxed) == 0 {
return normal;
}
let [r, g, b, a] = normal.to_srgba_unmultiplied();
let (r, g, b) = if cvd() { cvd_tone(t, dark) } else { (r, g, b) };
let mut c = Color32::from_rgb(r, g, b);
if high_contrast_on() && a == 255 {
c = contrast(c, dark);
}
Color32::from_rgba_unmultiplied(c.r(), c.g(), c.b(), fill_alpha(a))
}
pub fn warn_text(ui: &egui::Ui) -> Color32 {
role(Color32::from_rgb(220, 150, 60), Tone::Warn, ui.visuals().dark_mode)
}
pub fn ok_text(ui: &egui::Ui) -> Color32 {
role(Color32::from_rgb(90, 180, 90), Tone::Ok, ui.visuals().dark_mode)
}
pub fn info_text(ui: &egui::Ui) -> Color32 {
role(Color32::from_rgb(90, 160, 220), Tone::Info, ui.visuals().dark_mode)
}
pub fn mark_rgb(i: usize, normal: &[(u8, u8, u8); 8]) -> (u8, u8, u8) {
const CVD: [(u8, u8, u8); 8] = [(230, 159, 0), (86, 180, 233), (0, 158, 115), (240, 228, 66), (0, 114, 178), (213, 94, 0), (204, 121, 167), (150, 150, 150)];
if cvd() {
CVD[i % 8]
} else {
normal[i % 8]
}
}
pub fn mark_alpha(dark: bool) -> u8 {
fill_alpha(if dark { 95 } else { 110 })
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CodeTheme {
#[default]
Auto,
OneDark,
OneLight,
Monokai,
GitHubDark,
GitHubLight,
SolarizedDark,
SolarizedLight,
}
impl CodeTheme {
pub const ALL: [CodeTheme; 8] = [CodeTheme::Auto, CodeTheme::OneDark, CodeTheme::OneLight, CodeTheme::Monokai, CodeTheme::GitHubDark, CodeTheme::GitHubLight, CodeTheme::SolarizedDark, CodeTheme::SolarizedLight];
pub fn id(self) -> &'static str {
match self {
CodeTheme::Auto => "auto",
CodeTheme::OneDark => "one-dark",
CodeTheme::OneLight => "one-light",
CodeTheme::Monokai => "monokai",
CodeTheme::GitHubDark => "github-dark",
CodeTheme::GitHubLight => "github-light",
CodeTheme::SolarizedDark => "solarized-dark",
CodeTheme::SolarizedLight => "solarized-light",
}
}
pub fn from_id(id: &str) -> Option<CodeTheme> {
CodeTheme::ALL.into_iter().find(|t| t.id() == id)
}
pub fn label(self) -> &'static str {
match self {
CodeTheme::Auto => "As the theme (One Dark / One Light)",
CodeTheme::OneDark => "One Dark",
CodeTheme::OneLight => "One Light",
CodeTheme::Monokai => "Monokai",
CodeTheme::GitHubDark => "GitHub Dark",
CodeTheme::GitHubLight => "GitHub Light",
CodeTheme::SolarizedDark => "Solarized Dark",
CodeTheme::SolarizedLight => "Solarized Light",
}
}
pub fn resolve(self, dark: bool) -> CodeTheme {
match self {
CodeTheme::Auto if dark => CodeTheme::OneDark,
CodeTheme::Auto => CodeTheme::OneLight,
t => t,
}
}
pub fn is_dark(self) -> bool {
matches!(self, CodeTheme::OneDark | CodeTheme::Monokai | CodeTheme::GitHubDark | CodeTheme::SolarizedDark)
}
}
static CODE_THEME: AtomicU8 = AtomicU8::new(0);
pub fn set_code_theme(t: CodeTheme) {
let i = CodeTheme::ALL.iter().position(|&x| x == t).unwrap_or(0);
CODE_THEME.store(i as u8, Ordering::Relaxed);
}
pub fn code_theme() -> CodeTheme {
CodeTheme::ALL.get(CODE_THEME.load(Ordering::Relaxed) as usize).copied().unwrap_or_default()
}
pub struct CodeColors {
pub bg: Color32,
pub fg: Color32,
pub line_no: Color32,
pub line_no_active: Color32,
pub cur_line: Color32,
pub guide: Color32,
pub caret: Color32,
pub dark: bool,
}
fn hex(v: u32) -> Color32 {
Color32::from_rgb((v >> 16) as u8, (v >> 8) as u8, v as u8)
}
pub fn code_colors(dark: bool) -> CodeColors {
let t = code_theme().resolve(dark);
let [bg, fg, line_no, active, cur, guide, caret] = match t {
CodeTheme::OneDark | CodeTheme::Auto => [0x23272e, 0xabb2bf, 0x5f687a, 0xc8cdd7, 0x2c313c, 0x404650, 0xc8cdd7],
CodeTheme::OneLight => [0xfafafa, 0x383a42, 0x9d9d9f, 0x383a42, 0xf0f0f2, 0xdedee0, 0x526fff],
CodeTheme::Monokai => [0x272822, 0xf8f8f2, 0x90908a, 0xf8f8f2, 0x3e3d32, 0x464741, 0xf8f8f0],
CodeTheme::GitHubDark => [0x0d1117, 0xe6edf3, 0x6e7681, 0xe6edf3, 0x161b22, 0x21262d, 0xe6edf3],
CodeTheme::GitHubLight => [0xffffff, 0x1f2328, 0x8c959f, 0x1f2328, 0xf6f8fa, 0xd0d7de, 0x1f2328],
CodeTheme::SolarizedDark => [0x002b36, 0x839496, 0x586e75, 0x93a1a1, 0x073642, 0x0e4250, 0x93a1a1],
CodeTheme::SolarizedLight => [0xfdf6e3, 0x657b83, 0x93a1a1, 0x586e75, 0xeee8d5, 0xe4ddc8, 0x586e75],
};
CodeColors { bg: hex(bg), fg: hex(fg), line_no: hex(line_no), line_no_active: hex(active), cur_line: hex(cur), guide: hex(guide), caret: hex(caret), dark: t.is_dark() }
}
pub fn code_token(kind: crate::view::highlight::Kind, dark: bool) -> Option<Color32> {
use crate::view::highlight::Kind;
let t = code_theme().resolve(dark);
let p: [u32; 11] = match t {
CodeTheme::OneDark | CodeTheme::Auto => [0xc678dd, 0x98c379, 0xd19a66, 0x7f848e, 0xe5c07b, 0x61afef, 0xe06c75, 0x56b6c2, 0xe06c75, 0xd19a66, 0xe06c75],
CodeTheme::OneLight => [0x9818a0, 0x28781e, 0xaa5a00, 0x787878, 0x966400, 0x285fd2, 0xc83228, 0x0078a0, 0xc83228, 0xaa5a00, 0xc83228],
CodeTheme::Monokai => [0xf92672, 0xe6db74, 0xae81ff, 0x88846f, 0x66d9ef, 0xa6e22e, 0xfd971f, 0xa6e22e, 0xf92672, 0xa6e22e, 0xf92672],
CodeTheme::GitHubDark => [0xff7b72, 0xa5d6ff, 0x79c0ff, 0x8b949e, 0xffa657, 0xd2a8ff, 0xffa657, 0xd2a8ff, 0x7ee787, 0x79c0ff, 0x7ee787],
CodeTheme::GitHubLight => [0xcf222e, 0x0a3069, 0x0550ae, 0x59636e, 0x953800, 0x8250df, 0x953800, 0x8250df, 0x116329, 0x0550ae, 0x116329],
CodeTheme::SolarizedDark | CodeTheme::SolarizedLight => [0x859900, 0x2aa198, 0xd33682, 0x6c8a91, 0xb58900, 0x268bd2, 0xcb4b16, 0x6c71c4, 0x268bd2, 0xb58900, 0x268bd2],
};
let i = match kind {
Kind::Keyword => 0,
Kind::Str => 1,
Kind::Number => 2,
Kind::Comment => 3,
Kind::Type => 4,
Kind::Function => 5,
Kind::Var => 6,
Kind::Decorator => 7,
Kind::Tag => 8,
Kind::Attr => 9,
Kind::Key => 10,
_ => return None,
};
Some(hex(p[i]))
}
pub fn cvd_syntax(kind: crate::view::highlight::Kind, dark: bool) -> (u8, u8, u8) {
use crate::view::highlight::Kind;
let (d, l) = match kind {
Kind::Keyword => ((204, 121, 167), (160, 60, 120)),
Kind::Str => ((100, 200, 160), (0, 128, 95)),
Kind::Number => ((230, 159, 0), (170, 100, 0)),
Kind::Comment => ((128, 128, 128), (110, 110, 110)),
Kind::Tag => ((86, 180, 233), (0, 100, 170)),
Kind::Attr => ((240, 228, 66), (130, 110, 0)),
Kind::Key => ((86, 180, 233), (0, 100, 170)),
Kind::Error => ((230, 110, 30), (200, 80, 0)),
Kind::Warn => ((240, 228, 66), (150, 120, 0)),
Kind::Info => ((86, 180, 233), (0, 100, 170)),
Kind::Debug => ((128, 128, 128), (120, 120, 120)),
Kind::Time => ((0, 158, 115), (0, 110, 80)),
Kind::Punct => ((171, 178, 191), (90, 90, 90)),
Kind::Type => ((86, 180, 233), (0, 100, 170)),
Kind::Function => ((240, 228, 66), (130, 110, 0)),
Kind::Var => ((230, 110, 30), (200, 80, 0)),
Kind::Decorator => ((204, 121, 167), (160, 60, 120)),
Kind::Col(i) => {
const DARK: [(u8, u8, u8); 6] = [(230, 159, 0), (86, 180, 233), (0, 158, 115), (204, 121, 167), (240, 228, 66), (230, 110, 30)];
const LIGHT: [(u8, u8, u8); 6] = [(170, 100, 0), (0, 100, 170), (0, 115, 80), (160, 60, 120), (130, 110, 0), (200, 80, 0)];
(DARK[i as usize % 6], LIGHT[i as usize % 6])
}
};
if dark {
d
} else {
l
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ids_round_trip_and_flip_keeps_contrast() {
for t in Theme::ALL {
assert_eq!(Theme::from_id(t.id()), Some(t));
assert_eq!(t.flipped().high_contrast(), t.high_contrast());
assert_ne!(t.flipped().is_dark(), t.is_dark());
assert_eq!(t.visuals().dark_mode, t.is_dark());
}
let v = Theme::HighContrastLight.visuals();
assert_eq!((v.panel_fill, v.override_text_color), (Color32::WHITE, Some(Color32::BLACK)));
}
#[test]
fn contrast_moves_away_from_the_background() {
let c = Color32::from_rgb(100, 150, 200);
let d = contrast(c, true);
let l = contrast(c, false);
assert!(d.r() > c.r() && d.g() > c.g() && d.b() > c.b());
assert!(l.r() < c.r() && l.g() < c.g() && l.b() < c.b());
assert_eq!(contrast(Color32::WHITE, true), Color32::WHITE);
}
}