Skip to main content

eclipse_core/
palette.rs

1//! The colours, in one place.
2//!
3//! Two surfaces draw this program: the full-screen interface, in terminal colours, and the
4//! dashboard, in CSS. They had drifted — the terminal was amber and the page was blue — because
5//! each kept its own list of numbers. The list is here now, and both of them read it: the interface
6//! builds its `ratatui::style::Color` from these triples, and the dashboard is handed them as CSS
7//! custom properties when the page is rendered. Nothing else may spell a brand colour out.
8//!
9//! These are RGB triples rather than either surface's own type so that this crate stays free of
10//! both ratatui and CSS; the conversion belongs to whichever side is drawing.
11//!
12//! The surfaces are the dashboard's, not the terminal's: a terminal's background is the user's
13//! choice, and the interface draws on whatever it finds.
14
15/// The one accent: a warm amber. Everything the reader is meant to look at first.
16pub const ACCENT: (u8, u8, u8) = (214, 137, 58);
17/// The two ends of the wordmark's gradient, cream at the top of the letters and amber at the foot.
18pub const WORDMARK_TOP: (u8, u8, u8) = (255, 231, 200);
19pub const WORDMARK_FOOT: (u8, u8, u8) = (232, 147, 64);
20/// What the person at the keyboard typed.
21pub const USER: (u8, u8, u8) = (126, 176, 199);
22/// A call that worked, a call that broke, a call that needs a decision.
23pub const OK: (u8, u8, u8) = (126, 170, 126);
24pub const BAD: (u8, u8, u8) = (198, 96, 96);
25pub const WARN: (u8, u8, u8) = (214, 176, 90);
26/// Secondary text, and the faintest rules and borders — the terminal's, which are drawn against
27/// whatever background the user chose, so they are not held to our surfaces' contrast floor.
28pub const DIM: (u8, u8, u8) = (128, 128, 128);
29pub const FAINT: (u8, u8, u8) = (96, 96, 96);
30/// The page's own two greys: secondary text, and the hairlines that carry meaning (a fold's spine,
31/// a block's edge). They are warmer and lighter than the terminal's pair because they have a known
32/// background to be legible against — 5.97:1 and 3.54:1 on the page.
33pub const MUTED: (u8, u8, u8) = (150, 146, 140);
34pub const RULE: (u8, u8, u8) = (112, 108, 102);
35/// The page's body text.
36pub const FG: (u8, u8, u8) = (230, 233, 238);
37
38/// The dashboard's surfaces: the page, a raised block, a hairline, and the row under the pointer.
39pub const BG: (u8, u8, u8) = (17, 20, 24);
40pub const PANEL: (u8, u8, u8) = (26, 31, 38);
41pub const LINE: (u8, u8, u8) = (42, 49, 59);
42pub const HOVER: (u8, u8, u8) = (34, 42, 52);
43/// Text drawn *on* an accent fill, where the accent is the background.
44pub const INK: (u8, u8, u8) = (4, 16, 31);
45
46/// WCAG relative luminance of one colour, 0 (black) to 1 (white).
47pub fn luminance((r, g, b): (u8, u8, u8)) -> f64 {
48    fn channel(value: u8) -> f64 {
49        let c = f64::from(value) / 255.0;
50        if c <= 0.039_28 {
51            c / 12.92
52        } else {
53            ((c + 0.055) / 1.055).powf(2.4)
54        }
55    }
56    0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b)
57}
58
59/// WCAG contrast between two colours, 1.0 (identical) to 21.0 (black on white).
60pub fn contrast(a: (u8, u8, u8), b: (u8, u8, u8)) -> f64 {
61    let (la, lb) = (luminance(a), luminance(b));
62    let (lighter, darker) = if la > lb { (la, lb) } else { (lb, la) };
63    (lighter + 0.05) / (darker + 0.05)
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    /// The palette has to be readable, not just consistent. 4.5:1 for body text and 3:1 for large
71    /// text and for the borders and marks that carry meaning is the floor; if a colour fails, the
72    /// *surface* moves, never the brand.
73    #[test]
74    fn the_page_is_readable_at_aa() {
75        let body = contrast(FG, BG);
76        assert!(body >= 4.5, "body text on the page: {body:.2}:1");
77        let muted = contrast(MUTED, BG);
78        assert!(muted >= 4.5, "secondary text on the page: {muted:.2}:1");
79        let muted_on_panel = contrast(MUTED, PANEL);
80        assert!(
81            muted_on_panel >= 4.5,
82            "secondary text on a panel: {muted_on_panel:.2}:1"
83        );
84        let rule = contrast(RULE, BG);
85        assert!(rule >= 3.0, "a hairline on the page: {rule:.2}:1");
86        let accent = contrast(ACCENT, BG);
87        assert!(accent >= 3.0, "the accent on the page: {accent:.2}:1");
88        let accent_on_panel = contrast(ACCENT, PANEL);
89        assert!(
90            accent_on_panel >= 3.0,
91            "the accent on a panel: {accent_on_panel:.2}:1"
92        );
93        let ok = contrast(OK, BG);
94        assert!(ok >= 3.0, "the ok mark on the page: {ok:.2}:1");
95        let bad = contrast(BAD, BG);
96        assert!(bad >= 3.0, "the failed mark on the page: {bad:.2}:1");
97        let warn = contrast(WARN, BG);
98        assert!(warn >= 3.0, "the warning mark on the page: {warn:.2}:1");
99        // Text on an accent fill is the one pairing where the accent is the background.
100        let ink = contrast(INK, ACCENT);
101        assert!(ink >= 4.5, "text on an accent button: {ink:.2}:1");
102    }
103
104    /// The accent is the colour the terminal draws; a second amber somewhere else is how the two
105    /// surfaces drifted apart the first time.
106    #[test]
107    fn the_accent_is_the_warm_one_and_stays_warm() {
108        let (r, g, b) = ACCENT;
109        assert!(r > g && g > b, "amber, not a tint of blue: {ACCENT:?}");
110        assert!(
111            f64::from(r) - f64::from(b) > 100.0,
112            "enough of a difference to read as amber: {ACCENT:?}"
113        );
114    }
115}