gitpoulpi-config 1.4.0

GitPoulpi config schema, TOML load/save, defaults, and OS-correct path resolution.
Documentation
//! Presets imported from the terminal palettes of https://terminalcolors.com/
//! (each scheme belongs to its authors). The table in
//! `terminalcolors_data.rs` is generated by `scripts/gen-terminalcolors.py`;
//! one [`derive_tc`] maps a palette (background, foreground, selection and the
//! 16 ANSI colors) to every GitPoulpi role through the shared [`Pal`]
//! derivation: accent = blue, success = green, warning = yellow, danger = red,
//! ai = magenta, cyan = cyan, muted = bright black. Colors that the contrast
//! rules reject are nudged until the theme passes.

use super::contrast_ratio;
use super::themes::{col, derive, mix, Pal, Rgb};
use super::{ThemeColor, ThemeSpec};

/// One imported terminal palette.
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,
    /// Normal 0-7 then bright 8-15 (black, red, green, yellow, blue, magenta, cyan, white).
    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)
}

/// Every imported slug, in table order.
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)
}

/// Of the normal / bright variant of an ANSI color, the one that reads best on `bg`.
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
    }
}

/// `c` towards `towards` until it reads `min` on `bg` (or it is reached).
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))
}

/// The 8 lane colors: the six accents plus an orange and a teal, each
/// lightened or darkened until it reads 2.2 on the background.
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()
}

/// The preset for one imported palette.
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),
    };
    // The scheme's own selection if text reads on it and it stands out,
    // else a softer version of it, else a neutral lift of the background.
    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();
            // Directions: `fill` colors go towards the background on dark
            // themes (they carry light text), towards the text on light ones.
            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);
    // Toolbar icons sit on a chip of their own: deepen or lighten each one
    // until it reads 3.4 there, whatever the palette color it came from.
    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
}