| 1 | //! One theme: color roles from the Codewhale design tokens. |
| 2 | //! |
| 3 | //! Components name a [`Role`]; they never name a color. [`Theme`] resolves a |
| 4 | //! role for the terminal it is painting on, every time it paints, so a theme |
| 5 | //! or depth change reaches every component on the next frame. |
| 6 | //! |
| 7 | //! | Depth | Grounds | Ink | State hues | |
| 8 | //! |---|---|---|---| |
| 9 | //! | TrueColor | exact token RGB | exact RGB | exact RGB | |
| 10 | //! | Ansi256 | nearest fixed index | generated table, audited for contrast | generated table | |
| 11 | //! | Ansi16 | none (the terminal owns its ground) | `Reset`; `Muted`, `Hint` and `Dim` add `DIM` | Blue, Cyan, Yellow, Red (bright 9–14 on Dark) | |
| 12 | //! | Monochrome (`NO_COLOR`) | none | `Reset`; hierarchy by `BOLD`/`DIM` only | `Reset`: the mark and word carry state | |
| 13 | //! |
| 14 | //! Dark terminals take the blue ombre by default ([`Ground::Ocean`]): the |
| 15 | //! token grounds and quiet lines tinted toward the logo's deep blue at the |
| 16 | //! same luminance, so every contrast the tokens audit still holds. It shows |
| 17 | //! at truecolor only; the 256-color cube has no navy fine enough, so 256 |
| 18 | //! colors keep the token grounds. [`Ground::Graphite`] keeps the exact token |
| 19 | //! grounds everywhere. |
| 20 | //! |
| 21 | //! When nothing measured the terminal's ground ([`Appearance::Unknown`]), a |
| 22 | //! truecolor terminal is painted as at ANSI-16: its named colors were chosen |
| 23 | //! for its own ground, and ours were not. This is stricter than the engine's |
| 24 | //! palette, which assumes dark. A host that knows better (a theme setting the |
| 25 | //! person chose) sets [`Caps::appearance`] itself; a person can force it with |
| 26 | //! `CODEWHALE_APPEARANCE=light` or `=dark`. |
| 27 | |
| 28 | use ratatui::style::{Color, Modifier, Style}; |
| 29 | |
| 30 | use crate::color::{ColorDepth, rgb}; |
| 31 | use crate::detect::{Appearance, terminal_background}; |
| 32 | use crate::roles; |
| 33 | |
| 34 | pub use crate::roles::{Role, TOKENS_VERSION}; |
| 35 | |
| 36 | /// The logo's ombre, top-left to bottom-right (`#1E8FD8` to `#0B48BB`), from |
| 37 | /// the design direction. The whale wears it; [`Ground::Ocean`] grounds lean |
| 38 | /// toward its deep end. |
| 39 | pub const LOGO_TOP: u32 = 0x1e8fd8; |
| 40 | pub const LOGO_BOTTOM: u32 = 0x0b48bb; |
| 41 | |
| 42 | /// How far [`Ground::Ocean`] moves each dark ground and quiet line toward |
| 43 | /// [`LOGO_BOTTOM`] before restoring its luminance. |
| 44 | pub const OCEAN_TINT: f64 = 0.5; |
| 45 | |
| 46 | /// Which grounds a dark theme paints. |
| 47 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)] |
| 48 | pub enum Ground { |
| 49 | /// Deep navy rising into the logo's blue: the preferred look. |
| 50 | #[default] |
| 51 | Ocean, |
| 52 | /// The exact token grounds (graphite), as the desktop app paints them. |
| 53 | Graphite, |
| 54 | } |
| 55 | |
| 56 | impl Role { |
| 57 | /// Whether [`Ground::Ocean`] tints this role toward the logo's blue: the |
| 58 | /// five grounds and quiet lines. Ink, state hues and control edges keep |
| 59 | /// their token values; the diff tints are re-mixed over the ombre's own |
| 60 | /// background instead ([`Role::is_content_tint`]). |
| 61 | #[must_use] |
| 62 | pub const fn ocean_tinted(self) -> bool { |
| 63 | matches!( |
| 64 | self, |
| 65 | Role::Sidebar |
| 66 | | Role::Background |
| 67 | | Role::Surface |
| 68 | | Role::Hover |
| 69 | | Role::Selected |
| 70 | | Role::Border |
| 71 | ) |
| 72 | } |
| 73 | |
| 74 | /// A content tint: a ground behind a line of content, painted behind |
| 75 | /// `Foreground` ink only. Where grounds do not paint (16 colors, |
| 76 | /// `NO_COLOR`) the tint is dropped and a glyph carries the meaning. |
| 77 | #[must_use] |
| 78 | pub const fn is_content_tint(self) -> bool { |
| 79 | matches!(self, Role::DiffAddedTint | Role::DiffRemovedTint) |
| 80 | } |
| 81 | |
| 82 | /// Whether this role is painted as a background: the grounds, primary |
| 83 | /// action fill and content tints. |
| 84 | #[must_use] |
| 85 | pub const fn is_ground(self) -> bool { |
| 86 | matches!( |
| 87 | self, |
| 88 | Role::Sidebar |
| 89 | | Role::Background |
| 90 | | Role::Surface |
| 91 | | Role::Hover |
| 92 | | Role::Selected |
| 93 | | Role::Primary |
| 94 | ) || self.is_content_tint() |
| 95 | } |
| 96 | |
| 97 | /// The named color a 16-color terminal shows for this role, or `None` |
| 98 | /// when the role takes the terminal's own foreground. |
| 99 | const fn ansi16(self, appearance: Appearance) -> Option<Color> { |
| 100 | match (self, appearance) { |
| 101 | (Role::Accent, Appearance::Dark) => Some(Color::LightBlue), |
| 102 | (Role::Accent, _) => Some(Color::Blue), |
| 103 | (Role::Live | Role::LivePeak, Appearance::Dark) => Some(Color::LightCyan), |
| 104 | (Role::Live | Role::LivePeak, _) => Some(Color::Cyan), |
| 105 | (Role::Attention, Appearance::Dark) => Some(Color::LightYellow), |
| 106 | (Role::Attention, _) => Some(Color::Yellow), |
| 107 | (Role::Danger, Appearance::Dark) => Some(Color::LightRed), |
| 108 | (Role::Danger, _) => Some(Color::Red), |
| 109 | _ => None, |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Ink that recedes. Where color is unavailable it recedes with `DIM`. |
| 114 | const fn recedes(self) -> bool { |
| 115 | matches!(self, Role::Muted | Role::Hint | Role::Dim | Role::Border) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// What the terminal can show. |
| 120 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 121 | pub struct Caps { |
| 122 | pub depth: ColorDepth, |
| 123 | /// `CODEWHALE_ASCII_SAFE`: draw marks from the ASCII fallbacks. |
| 124 | pub ascii: bool, |
| 125 | pub appearance: Appearance, |
| 126 | } |
| 127 | |
| 128 | impl Caps { |
| 129 | /// Read depth, ASCII-safety and appearance from the environment and any |
| 130 | /// completed [`crate::detect::probe_terminal_background`]. |
| 131 | #[must_use] |
| 132 | pub fn detect() -> Self { |
| 133 | Self::detect_with( |
| 134 | |key| std::env::var_os(key), |
| 135 | || terminal_background().appearance(), |
| 136 | ) |
| 137 | } |
| 138 | |
| 139 | /// [`Caps::detect`] with an injected environment reader and ground |
| 140 | /// detector, so every branch is testable without the process state. |
| 141 | /// |
| 142 | /// `CODEWHALE_APPEARANCE` (`light` or `dark`) overrides detection: the |
| 143 | /// person's word beats a measurement, and the detector is not called. |
| 144 | #[must_use] |
| 145 | pub fn detect_with( |
| 146 | get: impl Fn(&str) -> Option<std::ffi::OsString>, |
| 147 | detect_ground: impl FnOnce() -> Appearance, |
| 148 | ) -> Self { |
| 149 | let ascii = |
| 150 | get("CODEWHALE_ASCII_SAFE").is_some_and(|v| !v.is_empty() && v != "0" && v != "false"); |
| 151 | let forced = get("CODEWHALE_APPEARANCE").and_then(|v| { |
| 152 | match v.to_string_lossy().trim().to_ascii_lowercase().as_str() { |
| 153 | "light" => Some(Appearance::Light), |
| 154 | "dark" => Some(Appearance::Dark), |
| 155 | _ => None, |
| 156 | } |
| 157 | }); |
| 158 | Self { |
| 159 | depth: ColorDepth::detect_with(&get), |
| 160 | ascii, |
| 161 | appearance: forced.unwrap_or_else(detect_ground), |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// Whether this terminal shows token colors at all. |
| 166 | #[must_use] |
| 167 | pub const fn paints_tokens(&self) -> bool { |
| 168 | matches!(self.depth, ColorDepth::TrueColor | ColorDepth::Ansi256) |
| 169 | && !matches!(self.appearance, Appearance::Unknown) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// The Codewhale theme resolved for one terminal. |
| 174 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 175 | pub struct Theme { |
| 176 | caps: Caps, |
| 177 | grounds: bool, |
| 178 | ground: Ground, |
| 179 | native: Option<crate::TuiPalette>, |
| 180 | } |
| 181 | |
| 182 | impl Theme { |
| 183 | #[must_use] |
| 184 | pub const fn new(caps: Caps) -> Self { |
| 185 | Self { |
| 186 | caps, |
| 187 | grounds: true, |
| 188 | ground: Ground::Ocean, |
| 189 | native: None, |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /// Choose the dark grounds: the blue ombre (default) or graphite. Light |
| 194 | /// terminals always paint the light token grounds. |
| 195 | #[must_use] |
| 196 | pub const fn ground(mut self, ground: Ground) -> Self { |
| 197 | self.ground = ground; |
| 198 | self.native = None; |
| 199 | self |
| 200 | } |
| 201 | |
| 202 | /// Use one of the palettes shipped by the current Codewhale TUI. |
| 203 | /// Terminal capability and unknown-ground fallbacks remain in force. |
| 204 | #[must_use] |
| 205 | pub const fn tui_palette(mut self, palette: crate::TuiPalette) -> Self { |
| 206 | self.native = Some(palette); |
| 207 | self |
| 208 | } |
| 209 | |
| 210 | #[must_use] |
| 211 | pub const fn native_palette(&self) -> Option<crate::TuiPalette> { |
| 212 | self.native |
| 213 | } |
| 214 | |
| 215 | /// A distinct native TUI semantic ink, adapted by the same terminal rules. |
| 216 | #[must_use] |
| 217 | pub fn tui_ink(&self, ink: crate::TuiInk) -> Style { |
| 218 | if self.caps.paints_tokens() |
| 219 | && let Some(palette) = self.native |
| 220 | { |
| 221 | let color = match (self.depth(), palette.ink(ink)) { |
| 222 | (ColorDepth::Ansi256, Color::Rgb(r, g, b)) => { |
| 223 | Color::Indexed(crate::color::rgb_to_ansi256(r, g, b)) |
| 224 | } |
| 225 | (_, color) => color, |
| 226 | }; |
| 227 | Style::default().fg(color) |
| 228 | } else { |
| 229 | self.fg(ink.fallback_role()) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /// Native background slots such as composer and footer. Limited color |
| 234 | /// and unknown-ground terminals keep the existing capability fallbacks. |
| 235 | #[must_use] |
| 236 | pub fn tui_ground(&self, ground: crate::TuiGround) -> Style { |
| 237 | if self.paints_grounds() |
| 238 | && let Some(palette) = self.native |
| 239 | { |
| 240 | let color = match (self.depth(), palette.ground(ground)) { |
| 241 | (ColorDepth::Ansi256, Color::Rgb(r, g, b)) => { |
| 242 | Color::Indexed(crate::color::rgb_to_ansi256(r, g, b)) |
| 243 | } |
| 244 | (_, color) => color, |
| 245 | }; |
| 246 | Style::default().bg(color) |
| 247 | } else { |
| 248 | self.bg(ground.fallback_role()) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /// Codewhale's default Underwater, or its light palette on a light terminal. |
| 253 | #[must_use] |
| 254 | pub const fn tui(self) -> Self { |
| 255 | self.tui_palette(if self.light() { |
| 256 | crate::TuiPalette::WhaleLight |
| 257 | } else { |
| 258 | crate::TuiPalette::Underwater |
| 259 | }) |
| 260 | } |
| 261 | |
| 262 | /// The dark grounds this theme was built with. |
| 263 | #[must_use] |
| 264 | pub const fn ground_kind(&self) -> Ground { |
| 265 | self.ground |
| 266 | } |
| 267 | |
| 268 | /// Detect everything from the environment. |
| 269 | #[must_use] |
| 270 | pub fn detect() -> Self { |
| 271 | Self::new(Caps::detect()) |
| 272 | } |
| 273 | |
| 274 | /// Leave the terminal's own ground alone, even where token grounds would |
| 275 | /// render. Raised panels and selection still paint. |
| 276 | #[must_use] |
| 277 | pub const fn without_base_ground(mut self) -> Self { |
| 278 | self.grounds = false; |
| 279 | self |
| 280 | } |
| 281 | |
| 282 | #[must_use] |
| 283 | pub const fn caps(&self) -> Caps { |
| 284 | self.caps |
| 285 | } |
| 286 | |
| 287 | #[must_use] |
| 288 | pub const fn ascii(&self) -> bool { |
| 289 | self.caps.ascii |
| 290 | } |
| 291 | |
| 292 | #[must_use] |
| 293 | pub const fn depth(&self) -> ColorDepth { |
| 294 | self.caps.depth |
| 295 | } |
| 296 | |
| 297 | /// Which token table this theme reads. Unknown reads the dark table, |
| 298 | /// but [`Caps::paints_tokens`] is false then, so no token color reaches |
| 299 | /// the screen. |
| 300 | const fn light(&self) -> bool { |
| 301 | matches!(self.caps.appearance, Appearance::Light) |
| 302 | } |
| 303 | |
| 304 | /// The exact token color (`0xRRGGBB`), before depth adaptation. |
| 305 | #[must_use] |
| 306 | pub const fn token_hex(&self, role: Role) -> u32 { |
| 307 | if let Some(palette) = self.native |
| 308 | && let Color::Rgb(r, g, b) = palette.color(role) |
| 309 | { |
| 310 | return ((r as u32) << 16) | ((g as u32) << 8) | b as u32; |
| 311 | } |
| 312 | if self.light() { |
| 313 | roles::LIGHT[role.index()] |
| 314 | } else if matches!(self.ground, Ground::Ocean) { |
| 315 | roles::OCEAN[role.index()] |
| 316 | } else { |
| 317 | roles::DARK[role.index()] |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /// The exact token color, before depth adaptation. Under |
| 322 | /// [`Ground::Ocean`] on a dark ground, grounds and quiet lines are the |
| 323 | /// tinted values; 256 colors still show the token grounds. |
| 324 | #[must_use] |
| 325 | pub const fn token(&self, role: Role) -> Color { |
| 326 | match self.native { |
| 327 | Some(palette) => palette.color(role), |
| 328 | None => rgb(self.token_hex(role)), |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /// The color this terminal shows for `role`, or `None` when it shows the |
| 333 | /// terminal's own color. |
| 334 | #[must_use] |
| 335 | pub fn color(&self, role: Role) -> Option<Color> { |
| 336 | if self.caps.paints_tokens() { |
| 337 | if self.native.is_some() { |
| 338 | return Some(match (self.caps.depth, self.token(role)) { |
| 339 | (ColorDepth::Ansi256, Color::Rgb(r, g, b)) => { |
| 340 | Color::Indexed(crate::color::rgb_to_ansi256(r, g, b)) |
| 341 | } |
| 342 | (_, color) => color, |
| 343 | }); |
| 344 | } |
| 345 | return Some(match self.caps.depth { |
| 346 | ColorDepth::TrueColor => self.token(role), |
| 347 | _ if self.light() => Color::Indexed(roles::LIGHT_256[role.index()]), |
| 348 | _ => Color::Indexed(roles::DARK_256[role.index()]), |
| 349 | }); |
| 350 | } |
| 351 | match self.caps.depth { |
| 352 | ColorDepth::Monochrome => None, |
| 353 | _ => role.ansi16(self.caps.appearance), |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /// Ink: `role` as a foreground. |
| 358 | #[must_use] |
| 359 | pub fn fg(&self, role: Role) -> Style { |
| 360 | let style = Style::default(); |
| 361 | match self.color(role) { |
| 362 | Some(color) => style.fg(color), |
| 363 | None if role.recedes() => style.add_modifier(Modifier::DIM), |
| 364 | None if role == Role::Subagent |
| 365 | && (self.caps.depth == ColorDepth::Monochrome |
| 366 | || self.caps.appearance == Appearance::Dark) => |
| 367 | { |
| 368 | style.add_modifier(Modifier::BOLD) |
| 369 | } |
| 370 | None => style, |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /// Ground: `role` as a background, or nothing where grounds do not paint. |
| 375 | /// The base `Background` also stays unpainted after |
| 376 | /// [`Theme::without_base_ground`]. |
| 377 | #[must_use] |
| 378 | pub fn bg(&self, role: Role) -> Style { |
| 379 | if !self.caps.paints_tokens() || (!self.grounds && role == Role::Background) { |
| 380 | return Style::default(); |
| 381 | } |
| 382 | match self.color(role) { |
| 383 | Some(color) => Style::default().bg(color), |
| 384 | None => Style::default(), |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /// Whether grounds paint at all. Where they do not, components must draw |
| 389 | /// an edge or a mark instead of relying on a fill. |
| 390 | #[must_use] |
| 391 | pub fn paints_grounds(&self) -> bool { |
| 392 | self.caps.paints_tokens() |
| 393 | } |
| 394 | |
| 395 | /// Whether grounds `a` and `b` look different on this terminal. False |
| 396 | /// where grounds do not paint, and where the two roles quantize to the |
| 397 | /// same color (light 256-color `Surface` and `Background`, for one), so |
| 398 | /// a component relying on the difference must draw an edge instead. |
| 399 | #[must_use] |
| 400 | pub fn grounds_differ(&self, a: Role, b: Role) -> bool { |
| 401 | self.bg(a) != self.bg(b) |
| 402 | } |
| 403 | |
| 404 | /// Whether the base `Background` ground is painted by this theme (false |
| 405 | /// without grounds and after [`Theme::without_base_ground`]). |
| 406 | #[must_use] |
| 407 | pub fn paints_base_ground(&self) -> bool { |
| 408 | self.bg(Role::Background).bg.is_some() |
| 409 | } |
| 410 | |
| 411 | /// Every role that resolves to `color` here, grounds first for |
| 412 | /// backgrounds and ink first for foregrounds. More than one means those |
| 413 | /// roles look the same at this depth. |
| 414 | #[must_use] |
| 415 | pub fn roles_of(&self, color: Color, as_ground: bool) -> Vec<Role> { |
| 416 | let (mut preferred, rest): (Vec<Role>, Vec<Role>) = Role::ALL |
| 417 | .iter() |
| 418 | .copied() |
| 419 | .filter(|r| self.color(*r) == Some(color)) |
| 420 | .partition(|r| r.is_ground() == as_ground); |
| 421 | if preferred.is_empty() { |
| 422 | preferred = rest; |
| 423 | } |
| 424 | preferred |
| 425 | } |
| 426 | |
| 427 | /// Reverse lookup for snapshots: the first of [`Theme::roles_of`]. |
| 428 | #[must_use] |
| 429 | pub fn role_of(&self, color: Color, as_ground: bool) -> Option<Role> { |
| 430 | self.roles_of(color, as_ground).first().copied() |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | #[cfg(test)] |
| 435 | mod tests { |
| 436 | use super::*; |
| 437 | use crate::color::contrast_ratio; |
| 438 | |
| 439 | fn theme(depth: ColorDepth, appearance: Appearance) -> Theme { |
| 440 | Theme::new(Caps { |
| 441 | depth, |
| 442 | ascii: false, |
| 443 | appearance, |
| 444 | }) |
| 445 | } |
| 446 | |
| 447 | #[test] |
| 448 | fn truecolor_reads_the_token_tables() { |
| 449 | let dark = theme(ColorDepth::TrueColor, Appearance::Dark); |
| 450 | assert_eq!( |
| 451 | dark.color(Role::Primary), |
| 452 | Some(rgb(crate::tokens::DARK.primary)) |
| 453 | ); |
| 454 | let light = theme(ColorDepth::TrueColor, Appearance::Light); |
| 455 | assert_eq!( |
| 456 | light.color(Role::Danger), |
| 457 | Some(rgb(crate::tokens::LIGHT.danger)) |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | /// The blue ombre tints dark grounds and the quiet line toward the |
| 462 | /// logo's deep blue, keeps every ink, and shows only at truecolor. |
| 463 | #[test] |
| 464 | fn ocean_tints_dark_grounds_only() { |
| 465 | let ocean = theme(ColorDepth::TrueColor, Appearance::Dark); |
| 466 | let graphite = ocean.ground(Ground::Graphite); |
| 467 | assert_eq!(ocean.ground_kind(), Ground::Ocean, "the preferred look"); |
| 468 | for role in Role::ALL { |
| 469 | let (o, g) = (ocean.token_hex(role), graphite.token_hex(role)); |
| 470 | if role.is_content_tint() { |
| 471 | // Mixed over the ombre's own background; the contrast test |
| 472 | // holds them to their floors. |
| 473 | assert_ne!(o, g, "{role:?} sits on the ombre, not on graphite"); |
| 474 | } else if role.ocean_tinted() { |
| 475 | let (r, gr, b) = ((o >> 16) & 0xff, (o >> 8) & 0xff, o & 0xff); |
| 476 | assert!(b > r && b > gr, "{role:?} #{o:06x} leans blue"); |
| 477 | let lum = |c| crate::color::relative_luminance(rgb(c)).unwrap(); |
| 478 | assert!((lum(o) - lum(g)).abs() < 0.002, "{role:?} keeps luminance"); |
| 479 | } else { |
| 480 | assert_eq!(o, g, "{role:?} keeps its token ink"); |
| 481 | } |
| 482 | } |
| 483 | for depth in [ |
| 484 | ColorDepth::Ansi256, |
| 485 | ColorDepth::Ansi16, |
| 486 | ColorDepth::Monochrome, |
| 487 | ] { |
| 488 | let o = theme(depth, Appearance::Dark); |
| 489 | let g = o.ground(Ground::Graphite); |
| 490 | for role in Role::ALL { |
| 491 | assert_eq!(o.color(role), g.color(role), "{depth:?} {role:?}"); |
| 492 | } |
| 493 | } |
| 494 | let light = theme(ColorDepth::TrueColor, Appearance::Light); |
| 495 | for role in Role::ALL { |
| 496 | assert_eq!( |
| 497 | light.color(role), |
| 498 | light.ground(Ground::Graphite).color(role), |
| 499 | "light {role:?}" |
| 500 | ); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn unknown_ground_never_paints_token_colors() { |
| 506 | let t = theme(ColorDepth::TrueColor, Appearance::Unknown); |
| 507 | assert_eq!(t.bg(Role::Background), Style::default()); |
| 508 | assert_eq!(t.color(Role::Foreground), None); |
| 509 | assert_eq!(t.color(Role::Live), Some(Color::Cyan)); |
| 510 | assert!(t.fg(Role::Muted).add_modifier.contains(Modifier::DIM)); |
| 511 | } |
| 512 | |
| 513 | #[test] |
| 514 | fn no_color_uses_only_modifiers() { |
| 515 | let t = theme(ColorDepth::Monochrome, Appearance::Dark); |
| 516 | for role in Role::ALL { |
| 517 | let fg = t.fg(role); |
| 518 | assert_eq!(fg.fg, None, "{role:?}"); |
| 519 | assert_eq!(t.bg(role).bg, None, "{role:?}"); |
| 520 | } |
| 521 | assert!(t.fg(Role::Muted).add_modifier.contains(Modifier::DIM)); |
| 522 | } |
| 523 | |
| 524 | #[test] |
| 525 | fn ansi16_state_hues_are_distinct() { |
| 526 | let t = theme(ColorDepth::Ansi16, Appearance::Dark); |
| 527 | let hues: Vec<_> = [Role::Accent, Role::Live, Role::Attention, Role::Danger] |
| 528 | .iter() |
| 529 | .map(|r| t.color(*r).unwrap()) |
| 530 | .collect(); |
| 531 | for (i, a) in hues.iter().enumerate() { |
| 532 | for b in &hues[i + 1..] { |
| 533 | assert_ne!(a, b); |
| 534 | } |
| 535 | } |
| 536 | assert_eq!(t.bg(Role::Selected), Style::default()); |
| 537 | } |
| 538 | |
| 539 | fn env(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<std::ffi::OsString> { |
| 540 | let pairs: Vec<(String, String)> = pairs |
| 541 | .iter() |
| 542 | .map(|(k, v)| ((*k).to_string(), (*v).to_string())) |
| 543 | .collect(); |
| 544 | move |key| pairs.iter().find(|(k, _)| k == key).map(|(_, v)| v.into()) |
| 545 | } |
| 546 | |
| 547 | #[test] |
| 548 | fn caps_read_ascii_safety_from_the_environment() { |
| 549 | let on = Caps::detect_with(env(&[("CODEWHALE_ASCII_SAFE", "1")]), || Appearance::Dark); |
| 550 | assert!(on.ascii); |
| 551 | for off in ["", "0", "false"] { |
| 552 | let caps = |
| 553 | Caps::detect_with(env(&[("CODEWHALE_ASCII_SAFE", off)]), || Appearance::Dark); |
| 554 | assert!(!caps.ascii, "{off:?}"); |
| 555 | } |
| 556 | assert!(!Caps::detect_with(env(&[]), || Appearance::Dark).ascii); |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn a_forced_appearance_beats_detection() { |
| 561 | let light = Caps::detect_with( |
| 562 | env(&[ |
| 563 | ("CODEWHALE_APPEARANCE", "Light"), |
| 564 | ("COLORTERM", "truecolor"), |
| 565 | ]), |
| 566 | || panic!("forced appearance must not probe"), |
| 567 | ); |
| 568 | assert_eq!(light.appearance, Appearance::Light); |
| 569 | assert!(light.paints_tokens()); |
| 570 | let dark = Caps::detect_with(env(&[("CODEWHALE_APPEARANCE", "dark")]), || { |
| 571 | Appearance::Unknown |
| 572 | }); |
| 573 | assert_eq!(dark.appearance, Appearance::Dark); |
| 574 | let junk = Caps::detect_with(env(&[("CODEWHALE_APPEARANCE", "sepia")]), || { |
| 575 | Appearance::Unknown |
| 576 | }); |
| 577 | assert_eq!(junk.appearance, Appearance::Unknown); |
| 578 | } |
| 579 | |
| 580 | /// Profiles the snapshots do not render separately, because they must |
| 581 | /// look exactly like one that is rendered. |
| 582 | #[test] |
| 583 | fn unsnapshotted_profiles_match_their_twins() { |
| 584 | let dark16 = theme(ColorDepth::Ansi16, Appearance::Dark); |
| 585 | let light16 = theme(ColorDepth::Ansi16, Appearance::Light); |
| 586 | let unknown256 = theme(ColorDepth::Ansi256, Appearance::Unknown); |
| 587 | for role in Role::ALL { |
| 588 | assert_eq!(light16.fg(role), unknown256.fg(role), "{role:?}"); |
| 589 | assert_eq!(light16.bg(role), Style::default(), "{role:?}"); |
| 590 | assert_eq!(unknown256.bg(role), Style::default(), "{role:?}"); |
| 591 | } |
| 592 | assert_eq!(dark16.color(Role::Accent), Some(Color::LightBlue)); |
| 593 | assert_eq!(dark16.color(Role::Live), Some(Color::LightCyan)); |
| 594 | assert_eq!(dark16.color(Role::LivePeak), Some(Color::LightCyan)); |
| 595 | assert_eq!(dark16.color(Role::Attention), Some(Color::LightYellow)); |
| 596 | assert_eq!(dark16.color(Role::Danger), Some(Color::LightRed)); |
| 597 | assert_eq!(light16.color(Role::Accent), Some(Color::Blue)); |
| 598 | assert_eq!(light16.color(Role::Live), Some(Color::Cyan)); |
| 599 | assert_eq!(light16.color(Role::LivePeak), Some(Color::Cyan)); |
| 600 | assert_eq!(light16.color(Role::Attention), Some(Color::Yellow)); |
| 601 | assert_eq!(light16.color(Role::Danger), Some(Color::Red)); |
| 602 | } |
| 603 | |
| 604 | /// Grounds that quantize to one color at 256 colors. Each one needs an |
| 605 | /// edge or a mark wherever a component relies on the difference; a new |
| 606 | /// collapse fails here so it gets that review. |
| 607 | #[test] |
| 608 | fn collapsed_grounds_are_known() { |
| 609 | let grounds = [ |
| 610 | Role::Sidebar, |
| 611 | Role::Background, |
| 612 | Role::Surface, |
| 613 | Role::Hover, |
| 614 | Role::Selected, |
| 615 | ]; |
| 616 | let mut collapsed = Vec::new(); |
| 617 | for appearance in [Appearance::Dark, Appearance::Light] { |
| 618 | let t = theme(ColorDepth::Ansi256, appearance); |
| 619 | for (i, a) in grounds.iter().enumerate() { |
| 620 | for b in &grounds[i + 1..] { |
| 621 | if !t.grounds_differ(*a, *b) { |
| 622 | collapsed.push(format!("{appearance:?} {a:?}={b:?}")); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | assert_eq!( |
| 628 | collapsed, |
| 629 | [ |
| 630 | "Dark Background=Surface", |
| 631 | "Light Sidebar=Background", |
| 632 | "Light Sidebar=Surface", |
| 633 | "Light Background=Surface", |
| 634 | ], |
| 635 | "Raised panels add edges when Surface=Background; workbench separates its sidebar" |
| 636 | ); |
| 637 | } |
| 638 | |
| 639 | /// Contrast audit at the two depths that paint token colors, with the |
| 640 | /// design's own floors (`generate.py`): every text role clears WCAG AA |
| 641 | /// (4.5:1) on every ground it can sit on, and the strong border 3:1. |
| 642 | #[test] |
| 643 | fn contrast_holds_at_truecolor_and_ansi256() { |
| 644 | let grounds = [ |
| 645 | Role::Background, |
| 646 | Role::Surface, |
| 647 | Role::Sidebar, |
| 648 | Role::Hover, |
| 649 | Role::Selected, |
| 650 | ]; |
| 651 | let mut failures = Vec::new(); |
| 652 | for (appearance, ground) in [ |
| 653 | (Appearance::Dark, Ground::Ocean), |
| 654 | (Appearance::Dark, Ground::Graphite), |
| 655 | (Appearance::Light, Ground::Ocean), |
| 656 | ] { |
| 657 | for depth in [ColorDepth::TrueColor, ColorDepth::Ansi256] { |
| 658 | let t = theme(depth, appearance).ground(ground); |
| 659 | for ground in grounds { |
| 660 | let bg = t.color(ground).unwrap(); |
| 661 | for (ink, floor) in [ |
| 662 | (Role::Foreground, 4.5), |
| 663 | (Role::Subagent, 4.5), |
| 664 | (Role::Muted, 4.5), |
| 665 | (Role::Accent, 4.5), |
| 666 | (Role::Live, 4.5), |
| 667 | (Role::LivePeak, 4.5), |
| 668 | (Role::Attention, 4.5), |
| 669 | (Role::Danger, 4.5), |
| 670 | (Role::Hint, 4.5), |
| 671 | (Role::Dim, 3.0), |
| 672 | (Role::BorderStrong, 3.0), |
| 673 | ] { |
| 674 | let ratio = contrast_ratio(t.color(ink).unwrap(), bg).unwrap(); |
| 675 | if ratio < floor { |
| 676 | failures.push(format!( |
| 677 | "{appearance:?} {depth:?} {ink:?} on {ground:?} ({:?}): {ratio:.2} < {floor}", |
| 678 | t.ground_kind() |
| 679 | )); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | // Diff tints sit behind body ink and their own state hue; |
| 684 | // line numbers (`Muted`) only need the 3:1 the 256-color |
| 685 | // cube can give a green. |
| 686 | for (tint, hue) in [ |
| 687 | (Role::DiffAddedTint, Role::Live), |
| 688 | (Role::DiffRemovedTint, Role::Danger), |
| 689 | ] { |
| 690 | let bg = t.color(tint).unwrap(); |
| 691 | for (ink, floor) in [(Role::Foreground, 4.5), (hue, 4.5), (Role::Muted, 3.0)] { |
| 692 | let ratio = contrast_ratio(t.color(ink).unwrap(), bg).unwrap(); |
| 693 | if ratio < floor { |
| 694 | failures.push(format!( |
| 695 | "{appearance:?} {depth:?} {ink:?} on {tint:?} ({:?}): {ratio:.2} < {floor}", |
| 696 | t.ground_kind() |
| 697 | )); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | let on_primary = contrast_ratio( |
| 702 | t.color(Role::PrimaryForeground).unwrap(), |
| 703 | t.color(Role::Primary).unwrap(), |
| 704 | ) |
| 705 | .unwrap(); |
| 706 | if on_primary < 4.5 { |
| 707 | failures.push(format!( |
| 708 | "{appearance:?} {depth:?} PrimaryForeground on Primary: {on_primary:.2} < 4.5" |
| 709 | )); |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | assert!( |
| 714 | failures.is_empty(), |
| 715 | "contrast failures:\n{}", |
| 716 | failures.join("\n") |
| 717 | ); |
| 718 | } |
| 719 | |
| 720 | /// Foreground, Muted, Hint and Dim step down against every ground, in |
| 721 | /// every appearance and depth that paints token colors, and no two of |
| 722 | /// them (or the strong border) show the same color. |
| 723 | #[test] |
| 724 | fn the_ink_ladder_steps_down() { |
| 725 | let ladder = [Role::Foreground, Role::Muted, Role::Hint, Role::Dim]; |
| 726 | for (appearance, ground) in [ |
| 727 | (Appearance::Dark, Ground::Ocean), |
| 728 | (Appearance::Dark, Ground::Graphite), |
| 729 | (Appearance::Light, Ground::Ocean), |
| 730 | ] { |
| 731 | for depth in [ColorDepth::TrueColor, ColorDepth::Ansi256] { |
| 732 | let t = theme(depth, appearance).ground(ground); |
| 733 | for ground in [Role::Background, Role::Surface, Role::Selected] { |
| 734 | let bg = t.color(ground).unwrap(); |
| 735 | let ratios: Vec<f32> = ladder |
| 736 | .iter() |
| 737 | .map(|r| contrast_ratio(t.color(*r).unwrap(), bg).unwrap()) |
| 738 | .collect(); |
| 739 | assert!( |
| 740 | ratios.windows(2).all(|w| w[0] > w[1]), |
| 741 | "{appearance:?} {depth:?} on {ground:?}: {ratios:?}" |
| 742 | ); |
| 743 | } |
| 744 | let inks = [ |
| 745 | Role::Foreground, |
| 746 | Role::Muted, |
| 747 | Role::Hint, |
| 748 | Role::Dim, |
| 749 | Role::BorderStrong, |
| 750 | ]; |
| 751 | for (i, a) in inks.iter().enumerate() { |
| 752 | for b in &inks[i + 1..] { |
| 753 | if depth == ColorDepth::Ansi256 |
| 754 | || (*a, *b) != (Role::Dim, Role::BorderStrong) |
| 755 | { |
| 756 | assert_ne!( |
| 757 | t.color(*a), |
| 758 | t.color(*b), |
| 759 | "{appearance:?} {depth:?}: {a:?} and {b:?} look the same" |
| 760 | ); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | /// The diff tints are grounds that paint only where grounds paint, and |
| 769 | /// they differ from every other ground, so an added line never reads as |
| 770 | /// a selected row. |
| 771 | #[test] |
| 772 | fn diff_tints_are_distinct_grounds_that_drop_where_grounds_cannot_paint() { |
| 773 | let others = [ |
| 774 | Role::Sidebar, |
| 775 | Role::Background, |
| 776 | Role::Surface, |
| 777 | Role::Hover, |
| 778 | Role::Selected, |
| 779 | ]; |
| 780 | for appearance in [Appearance::Dark, Appearance::Light] { |
| 781 | for depth in [ColorDepth::TrueColor, ColorDepth::Ansi256] { |
| 782 | let t = theme(depth, appearance); |
| 783 | for tint in [Role::DiffAddedTint, Role::DiffRemovedTint] { |
| 784 | assert!(tint.is_ground() && tint.is_content_tint() && !tint.is_derived()); |
| 785 | for other in others { |
| 786 | assert!( |
| 787 | t.grounds_differ(tint, other), |
| 788 | "{appearance:?} {depth:?}: {tint:?} looks like {other:?}" |
| 789 | ); |
| 790 | } |
| 791 | } |
| 792 | assert!(t.grounds_differ(Role::DiffAddedTint, Role::DiffRemovedTint)); |
| 793 | } |
| 794 | for depth in [ColorDepth::Ansi16, ColorDepth::Monochrome] { |
| 795 | let t = theme(depth, appearance); |
| 796 | for tint in [Role::DiffAddedTint, Role::DiffRemovedTint] { |
| 797 | assert_eq!(t.bg(tint), Style::default(), "{appearance:?} {depth:?}"); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | let unknown = theme(ColorDepth::TrueColor, Appearance::Unknown); |
| 802 | assert_eq!(unknown.bg(Role::DiffAddedTint), Style::default()); |
| 803 | } |
| 804 | |
| 805 | /// Without color, the quiet inks recede by `DIM`, as `Muted` does. |
| 806 | #[test] |
| 807 | fn hint_and_dim_recede_without_color() { |
| 808 | for depth in [ColorDepth::Ansi16, ColorDepth::Monochrome] { |
| 809 | let t = theme(depth, Appearance::Dark); |
| 810 | for role in [Role::Muted, Role::Hint, Role::Dim] { |
| 811 | assert!( |
| 812 | t.fg(role).add_modifier.contains(Modifier::DIM), |
| 813 | "{depth:?} {role:?}" |
| 814 | ); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | #[test] |
| 820 | fn twenty_one_roles_one_of_them_derived() { |
| 821 | assert_eq!(Role::COUNT, 21); |
| 822 | assert_eq!(Role::ALL.len(), 21); |
| 823 | let derived: Vec<Role> = Role::ALL.into_iter().filter(|r| r.is_derived()).collect(); |
| 824 | assert_eq!(derived, [Role::Hint]); |
| 825 | for role in Role::ALL { |
| 826 | assert_eq!(role.token_name().is_none(), role.is_derived(), "{role:?}"); |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 |