eclipse-core 0.13.3

Core library for eclipse: providers, tools, sessions, config, runtime
Documentation
//! The colours, in one place.
//!
//! Two surfaces draw this program: the full-screen interface, in terminal colours, and the
//! dashboard, in CSS. They had drifted — the terminal was amber and the page was blue — because
//! each kept its own list of numbers. The list is here now, and both of them read it: the interface
//! builds its `ratatui::style::Color` from these triples, and the dashboard is handed them as CSS
//! custom properties when the page is rendered. Nothing else may spell a brand colour out.
//!
//! These are RGB triples rather than either surface's own type so that this crate stays free of
//! both ratatui and CSS; the conversion belongs to whichever side is drawing.
//!
//! The surfaces are the dashboard's, not the terminal's: a terminal's background is the user's
//! choice, and the interface draws on whatever it finds.

/// The one accent: a warm amber. Everything the reader is meant to look at first.
pub const ACCENT: (u8, u8, u8) = (214, 137, 58);
/// The two ends of the wordmark's gradient, cream at the top of the letters and amber at the foot.
pub const WORDMARK_TOP: (u8, u8, u8) = (255, 231, 200);
pub const WORDMARK_FOOT: (u8, u8, u8) = (232, 147, 64);
/// What the person at the keyboard typed.
pub const USER: (u8, u8, u8) = (126, 176, 199);
/// A call that worked, a call that broke, a call that needs a decision.
pub const OK: (u8, u8, u8) = (126, 170, 126);
pub const BAD: (u8, u8, u8) = (198, 96, 96);
pub const WARN: (u8, u8, u8) = (214, 176, 90);
/// Secondary text, and the faintest rules and borders — the terminal's, which are drawn against
/// whatever background the user chose, so they are not held to our surfaces' contrast floor.
pub const DIM: (u8, u8, u8) = (128, 128, 128);
pub const FAINT: (u8, u8, u8) = (96, 96, 96);
/// The page's own two greys: secondary text, and the hairlines that carry meaning (a fold's spine,
/// a block's edge). They are warmer and lighter than the terminal's pair because they have a known
/// background to be legible against — 5.97:1 and 3.54:1 on the page.
pub const MUTED: (u8, u8, u8) = (150, 146, 140);
pub const RULE: (u8, u8, u8) = (112, 108, 102);
/// The page's body text.
pub const FG: (u8, u8, u8) = (230, 233, 238);

/// The dashboard's surfaces: the page, a raised block, a hairline, and the row under the pointer.
pub const BG: (u8, u8, u8) = (17, 20, 24);
pub const PANEL: (u8, u8, u8) = (26, 31, 38);
pub const LINE: (u8, u8, u8) = (42, 49, 59);
pub const HOVER: (u8, u8, u8) = (34, 42, 52);
/// Text drawn *on* an accent fill, where the accent is the background.
pub const INK: (u8, u8, u8) = (4, 16, 31);

/// WCAG relative luminance of one colour, 0 (black) to 1 (white).
pub fn luminance((r, g, b): (u8, u8, u8)) -> f64 {
    fn channel(value: u8) -> f64 {
        let c = f64::from(value) / 255.0;
        if c <= 0.039_28 {
            c / 12.92
        } else {
            ((c + 0.055) / 1.055).powf(2.4)
        }
    }
    0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b)
}

/// WCAG contrast between two colours, 1.0 (identical) to 21.0 (black on white).
pub fn contrast(a: (u8, u8, u8), b: (u8, u8, u8)) -> f64 {
    let (la, lb) = (luminance(a), luminance(b));
    let (lighter, darker) = if la > lb { (la, lb) } else { (lb, la) };
    (lighter + 0.05) / (darker + 0.05)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The palette has to be readable, not just consistent. 4.5:1 for body text and 3:1 for large
    /// text and for the borders and marks that carry meaning is the floor; if a colour fails, the
    /// *surface* moves, never the brand.
    #[test]
    fn the_page_is_readable_at_aa() {
        let body = contrast(FG, BG);
        assert!(body >= 4.5, "body text on the page: {body:.2}:1");
        let muted = contrast(MUTED, BG);
        assert!(muted >= 4.5, "secondary text on the page: {muted:.2}:1");
        let muted_on_panel = contrast(MUTED, PANEL);
        assert!(
            muted_on_panel >= 4.5,
            "secondary text on a panel: {muted_on_panel:.2}:1"
        );
        let rule = contrast(RULE, BG);
        assert!(rule >= 3.0, "a hairline on the page: {rule:.2}:1");
        let accent = contrast(ACCENT, BG);
        assert!(accent >= 3.0, "the accent on the page: {accent:.2}:1");
        let accent_on_panel = contrast(ACCENT, PANEL);
        assert!(
            accent_on_panel >= 3.0,
            "the accent on a panel: {accent_on_panel:.2}:1"
        );
        let ok = contrast(OK, BG);
        assert!(ok >= 3.0, "the ok mark on the page: {ok:.2}:1");
        let bad = contrast(BAD, BG);
        assert!(bad >= 3.0, "the failed mark on the page: {bad:.2}:1");
        let warn = contrast(WARN, BG);
        assert!(warn >= 3.0, "the warning mark on the page: {warn:.2}:1");
        // Text on an accent fill is the one pairing where the accent is the background.
        let ink = contrast(INK, ACCENT);
        assert!(ink >= 4.5, "text on an accent button: {ink:.2}:1");
    }

    /// The accent is the colour the terminal draws; a second amber somewhere else is how the two
    /// surfaces drifted apart the first time.
    #[test]
    fn the_accent_is_the_warm_one_and_stays_warm() {
        let (r, g, b) = ACCENT;
        assert!(r > g && g > b, "amber, not a tint of blue: {ACCENT:?}");
        assert!(
            f64::from(r) - f64::from(b) > 100.0,
            "enough of a difference to read as amber: {ACCENT:?}"
        );
    }
}