use super::contrast_ratio;
use super::themes::{col, derive, mix, Pal, Rgb};
use super::{ThemeColor, ThemeSpec};
pub(super) struct Tc {
pub(super) slug: &'static str,
pub(super) label: &'static str,
pub(super) bg: u32,
pub(super) fg: u32,
pub(super) sel: u32,
pub(super) ansi: [u32; 16],
}
include!(concat!(env!("OUT_DIR"), "/terminalcolors_selected.rs"));
const WHITE: Rgb = (255, 255, 255);
const BLACK: Rgb = (0, 0, 0);
fn rgb(v: u32) -> Rgb {
((v >> 16) as u8, (v >> 8) as u8, v as u8)
}
fn ratio(a: Rgb, b: Rgb) -> f64 {
contrast_ratio(col(a), col(b)).unwrap_or(1.0)
}
pub(super) fn slugs() -> impl Iterator<Item = &'static str> {
TERMINAL_COLORS.iter().map(|t| t.slug)
}
pub(super) fn find(slug: &str) -> Option<&'static Tc> {
TERMINAL_COLORS.iter().find(|t| t.slug == slug)
}
fn is_light(bg: Rgb) -> bool {
ratio(bg, WHITE) < ratio(bg, BLACK)
}
fn pick(t: &Tc, i: usize, bg: Rgb) -> Rgb {
let (n, b) = (rgb(t.ansi[i]), rgb(t.ansi[i + 8]));
if ratio(b, bg) > ratio(n, bg) {
b
} else {
n
}
}
fn ensure(c: Rgb, bg: Rgb, towards: Rgb, min: f64) -> Rgb {
let mut t = 0.0;
while t < 1.0 && ratio(mix(c, towards, t), bg) < min {
t += 0.05;
}
mix(c, towards, t.min(1.0))
}
pub(super) fn lanes(t: &Tc) -> Vec<ThemeColor> {
let bg = rgb(t.bg);
let towards = if is_light(bg) { BLACK } else { WHITE };
let c = |i| pick(t, i, bg);
let (red, green, yellow, blue, magenta, cyan) = (c(1), c(2), c(3), c(4), c(5), c(6));
[
blue,
green,
yellow,
magenta,
red,
cyan,
mix(yellow, red, 0.5),
mix(green, blue, 0.5),
]
.into_iter()
.map(|x| col(ensure(x, bg, towards, 2.2)))
.collect()
}
pub(super) fn derive_tc(t: &Tc) -> ThemeSpec {
let bg = rgb(t.bg);
let light = is_light(bg);
let text = ensure(rgb(t.fg), bg, if light { BLACK } else { WHITE }, 7.0);
let toward_text = if light { BLACK } else { WHITE };
let c = |i| pick(t, i, bg);
let mut p = Pal {
slug: t.slug,
name: t.label,
light,
syntax: if light {
"InspiredGitHub"
} else {
"base16-ocean.dark"
},
lanes: "",
bg,
panel: mix(bg, text, if light { 0.05 } else { 0.06 }),
text,
muted: ensure(rgb(t.ansi[8]), bg, toward_text, 3.6),
accent: ensure(c(4), bg, toward_text, 3.4),
selection: bg,
success: c(2),
warning: c(3),
danger: ensure(c(1), bg, toward_text, 3.4),
ai: c(5),
cyan: c(6),
};
let sel = rgb(t.sel);
p.selection = [sel, mix(bg, sel, 0.5), mix(bg, sel, 0.3)]
.into_iter()
.find(|&s| ratio(text, s) >= 5.0 && ratio(s, bg) >= 1.2)
.unwrap_or_else(|| mix(bg, text, 0.16));
let mut spec = finish(&p, t);
for _ in 0..60 {
let issues = spec.contrast_issues();
if issues.is_empty() {
break;
}
for i in &issues {
let r = i.rule.as_str();
let fill_dir = if light { text } else { bg };
if r.starts_with("muted")
|| r.starts_with("toolbar_disabled")
|| r.starts_with("disabled")
{
p.muted = mix(p.muted, toward_text, 0.1);
} else if r == "text/selection" {
p.selection = mix(p.selection, bg, 0.2);
} else if r.starts_with("text/")
|| r.starts_with("editor.text")
|| r.starts_with("toolbar_fg")
|| r.starts_with("on_button/button")
|| r == "on_button/button_hover"
{
p.text = mix(p.text, toward_text, 0.1);
p.panel = mix(p.panel, bg, 0.3);
} else if r.starts_with("statusbar") {
p.text = mix(p.text, toward_text, 0.1);
} else if r.starts_with("accent")
|| r.starts_with("on_accent")
|| r.starts_with("text_selection")
|| r.starts_with("on_bright/dropdown")
{
p.accent = mix(p.accent, toward_text, 0.08);
} else if r.starts_with("danger") {
p.danger = mix(p.danger, toward_text, 0.08);
} else if r == "on_bright/marker" {
p.ai = mix(p.ai, toward_text, 0.08);
} else if r.starts_with("hover must") || r.starts_with("selection must") {
p.selection = mix(p.selection, bg, 0.08);
} else if r.starts_with("editor.find_hit") {
p.warning = mix(p.warning, bg, 0.08);
} else if r.starts_with("diff.add") {
p.success = mix(p.success, fill_dir, 0.08);
} else if r.starts_with("diff.del") || r.contains("banner_danger") {
p.danger = mix(p.danger, fill_dir, 0.08);
} else if r.contains("pill_onto") {
p.cyan = mix(p.cyan, fill_dir, 0.1);
} else {
p.ai = mix(p.ai, fill_dir, 0.1);
p.cyan = mix(p.cyan, fill_dir, 0.05);
}
}
spec = finish(&p, t);
}
spec
}
fn finish(p: &Pal, t: &Tc) -> ThemeSpec {
let mut spec = derive(p);
spec.graph_palette = lanes(t);
let chip = spec.chrome.toolbar_bg;
let toward = if p.light { BLACK } else { WHITE };
for icon in [
&mut spec.toolbar.pull,
&mut spec.toolbar.push,
&mut spec.toolbar.stash,
&mut spec.toolbar.branch,
&mut spec.toolbar.lfs,
&mut spec.toolbar.tag,
&mut spec.toolbar.search,
&mut spec.toolbar.undo,
] {
if let (Some(c), Some(b)) = (icon.approx_rgb(), chip.approx_rgb()) {
*icon = col(ensure(c, b, toward, 3.4));
}
}
spec
}