diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-25 21:16:19 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-25 23:32:17 -0300 |
| commit | e960fb9a7d837c4e952a98890dc5ddf9c130d195 (patch) | |
| tree | 5c0be1e8a5a9cfd4637daf9d93b55c9f2bc47f61 /core | |
| parent | 1e8787e0b2eb3eaa4991a5160f6e5c35ba6f132e (diff) | |
| download | pardes-e960fb9a7d837c4e952a98890dc5ddf9c130d195.tar.gz pardes-e960fb9a7d837c4e952a98890dc5ddf9c130d195.zip | |
colors, theme_schema: the colour system's core (the themes programme's L1; DESIGN.md B2-B4, B6, G1', H): a theme says ten colours and the terminal's sixteen in names that say the role (`Theme`: page, text, tag, tag_text, accent, selection, keyword, string, number, comment, ansi, then B3's overrides and G1''s `mode`), and everything a surface draws is worked out from it once a theme change by one function, `Chrome.of` (R0-R16 by halves: `hold` moves an ink's lightness alone, `hued` steps a ground off another in a hue's tint), into one set, `Chrome`: the page's, the two tag grounds' (plain and focus, G1': the mode is a mark on the grip, `grip.mode`, never a ground) and the status row's inks, each held on its own ground (text, H's `word`, dim, rule, key, match, the four severities, a selection rebuilt there); the selection's faint and the three sweeps; hits and the solid current hit; added and deleted rows; lanes and churn; the grips (focus, ring, mode and its glyph, dirty, a column's grabbed and at rest); rules, rail, empty column; syntax, line numbers, names and the sixteen; read by `Pardes.chrome()`; H's tag: the plain tag lifted to a band 1.1:1 off the page (across it where the gamut ends) unless rules of 3:1 part panes (acme), the focused band a focus step off it and louder than it off the page, a mid-grey tag no ink reads on the page a step off, the full ink up to 7:1, command words toward the ground to 4.5:1; no surface reads `Chrome` yet: the L2 lines move them, so no frame moves here; THE SEAM: the theme files and every ThemeFile so far speak the old names (`LegacyTheme`, theme_schema.zig, the old struct verbatim): `fold` folds them as they are (`legacy_themes`) and renames them at comptime (`themes`, `Theme.fromLegacy`: one table of old and new names, the fields now always derived left out, a compile error for a field on either side without its line, so G6's rename stays one table to drop); `Pardes.theme()` hands the surfaces still reading a theme's own fields the old names' view (the ring's own, a ThemeFile's file itself, or a new-names file's view filled from its Chrome, `Theme.toLegacy`), and ChromeTheme, Roles and Grounds keep today's derivations on it, unchanged, beside the new set (`ChromeTheme.resolved`), until each surface moves; `custom_theme` keeps its type, so no dump or digest changes; a ThemeFile is read in the old names when it names `tag_bg` (the user's one_dark_zed_mod.zon, an older DumpThemes'), else in the new; DumpThemes writes the new; `initial_chrome` (comptime) goes: the chrome is resolved at the session's start (finishThemeInitialization, as before, into `theme_chrome`, the field `chrome()` reads it from), and each place that resolves one hands it to the body's cache (Core.bodyChrome), which made its own again at the first frame; B9's budget: the derivations convert to and from OKLab with no pow (`labOf`, `rgbOf`: `to_linear`, and the light where a byte's rounding turns; `toOklab`/`fromOklab` stay the old views' so none of their values moves), one ink's line toward its ground is walked once for its words, dim and rule, a status row that is the plain tag's takes its inks, and the name tint's search (today's, the old view's too) skips by luminance the candidates that cannot read before the bytes round them, and a source hue already tried, its answers the same over the whole ring (compared byte for byte); readings where DESIGN.md is silent or in tension, each in the report: `tag_name` for B3's `name` (the theme's own `name` holds its name), `@"error"`, `hold` wherever B4 says held (the prototype pushes tag, selection and ANSI inks toward white or black), the current hit the hue itself held (R8's words; the prototype held its rejected own ground), each tag ground's full ink its own (B1.3; hring holds one ink on both), a port's `column_grip` held on the page (B3; hring takes the number), the focus step tried lighter on a dark tag first, as the prototype does in effect (H's "away from the page" first would sink lagoon's and forge_soft's focused tags to #0b2722 and #020205), R3's status-bar swap left to the importer (L4: no import flag in a theme), B6's selection triple without key and match, no `filter[256]` (ghostty's, L2d) and no InactiveDim table (L2e); RUN (TEST FIRST): colors.zig "every theme in the ring meets every floor pardes owns ..." (every theme x the designer's checks, A8, with B4's per-ground inks, G1''s mark and H's words, band, loudest focus and column grip: the natives and dark held to the professional floors too; the original's text, syntax, comments, line numbers and sixteen asserted never moved): 261 of 285 pass, and the 24 on its list fail as it says: L3 5 (acme's ANSI 10 and 14; solarized_dark and _light, seoul256_light_254 and _255: a `hit`, `added`/`deleted` of their own), L4 6 (everforest_light, github_dark_dimmed, gruvbox_material_light_hard/_medium/_soft, hex_toxic), L5 1 (dark: its derived ANSI 8 at 3:1, under the pro 4.5), L7 8 (retired), and 4 no line fixes (dusk, lagoon, solarium, forge_soft: soft by design, their text under the pro 7:1: C1's ruling); "the whole ring resolves in 150 ms of CPU, no theme in over 0.5 ms (Debug)": 39 to 44 ms of CPU, the slowest theme 291 to 307 us (seoul256_dark_239, modus_vivendi), each the least of five (under a load of 8.7, before the speedups, it measured twice that: 871 us); "a theme file in the old names, a user's own or an older DumpThemes', and DumpThemes' now in the new names, load to the same Chrome" (one_dark_zed_mod.zon's shape copied into the test); the neonotte84 and acme goldens on Chrome, re-pinned where B4 moves them (text, dim, rule and key as D4's; the match the hits' amber on that ground alone, neonotte84 #e29e47, where D4's pushed #f1a271 over seven grounds at once, acme its number #a05200; the reading mark sky on the grip, #2b92bc and #1e8ab3, where D4 tinted the tag, #29271e, and acme's own #f3f0cc; the hit an amber ground 1.5:1 off the page, #4c301a and #ffc790; the current hit the hue itself, #e29e47 and #a05200, where D4 drew a 1.9:1 ground); the error ink red on the page and both tags; against C1's prototype over the ring's 285 (resolve.py, g1ring.py, hring.py; the R3 status-bar swap aside, L4's): the page's tokens (11,115 values) 84 % the same, 96 % within 2, 2 % over 6, each from a reading above (hold for push: ANSI 1 and 9 in 37 themes; the current hit in 46); with the prototype's own choices put back (an experiment), 17,081 of 20,520 the same and 59 over 6, at floors that a bisection and a 0.005 walk cross on different sides; H's tag grounds against hring.py the same or within 2 in 271 (plain) and 268 (focus) of 285, the others R3's status-bar swap (13, L4's) and monokai_pro_light_sun (its tag its page: lifted darker, hring lighter); G1's mark sky in 225, mint 50, violet 9, paper 1 (C1: 225, 50, 10); no frame moves: core-test 1115/1287 (172 skipped: +3, the ring, timing and loader tests; the two goldens rewritten in place), shell-test 1335/1338 (3 skipped: +3), pane-test 183/184 (1 skipped), behaviour-test 280/280 and --replay 20 seeds of 70 steps, 0 failures, process-test 0 of 147 failed (3 skipped, no golden moved), term-test 17/17, kitty-test 45/45, the gui unit-test 1938/1942 (4 skipped; its one failure the known BaselineHarnessMismatch); perfops against the installed tree (1e8787e0b2eb, its prefix built here) at 10 % a cell: PASS, 121 counts, the largest `open on small` +3.02 % (5,736,138 to 5,909,480 instructions: each cell boots a session, which now resolves its chrome at start where `initial_chrome` was comptime; +11.3 % before the body's cache took the start's chrome and create stopped resolving one of its own), the boots' setup +0.13 %, every operation within 0.01 %; the dump and the digest unchanged (`custom_theme` keeps its type, `theme_chrome` is local), so no fuzz or soak; load 2.0 to 10.2
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
Diffstat (limited to 'core')
| -rw-r--r-- | core/Core.zig | 27 | ||||
| -rw-r--r-- | core/ansi.zig | 2 | ||||
| -rw-r--r-- | core/body_layer.zig | 4 | ||||
| -rw-r--r-- | core/colors.zig | 1486 | ||||
| -rw-r--r-- | core/jj.zig | 10 | ||||
| -rw-r--r-- | core/lib_syntax.zig | 6 | ||||
| -rw-r--r-- | core/root.zig | 3 | ||||
| -rw-r--r-- | core/theme_schema.zig | 284 |
8 files changed, 1626 insertions, 196 deletions
diff --git a/core/Core.zig b/core/Core.zig index 8c946787..f9e32eec 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -47,7 +47,7 @@ const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET; const TOPBAR_H = pardes.TOPBAR_H; const TagHit = pardes.TagHit; const Text = pardes.panes.Text; -const Theme = pardes.Theme; +const LegacyTheme = pardes.LegacyTheme; const builtins = pardes.builtins; const colors = pardes.colors; const column_weight_unit = layout.column_weight_unit; @@ -78,7 +78,6 @@ const sel_slot = pardes.sel_slot; const syntax = pardes.syntax; const tagline = pardes.tagline; const test_api = pardes.test_api; -const themes = pardes.themes; const turn = &pardes.turn; const Views = pardes.views; @@ -346,21 +345,21 @@ locations_config: locations.Config = .{}, locations_cache: locations.Cache = .{}, tty_filter_palette: pardes.Term.FilterPalette = .{}, font_request_taken: bool = false, -custom_theme: ?Theme = null, +custom_theme: ?LegacyTheme = null, /// The theme the window wears while a picker of them (or `:theme `) /// previews one, by index, and its chrome (colors.previewTheme); the /// session's own theme untouched under it. theme_preview: ?u16 = null, -preview_chrome: ChromeTheme = colors.initial_chrome, +preview_chrome: ChromeTheme = undefined, /// bodyChrome's: the theme it was worked out for, and it. -body_chrome: ?struct { theme: Theme, chrome: ChromeTheme } = null, +body_chrome: ?struct { theme: LegacyTheme, chrome: ChromeTheme } = null, theme_file_path: config.Settings.Text(limits.host_path_cap) = .{}, /// The ThemeFile last loaded whole: what /ctl and DumpConfig name, and /// what a failed ThemeFile goes back to (colors.revertThemeFile). theme_file_loaded: config.Settings.Text(limits.host_path_cap) = .{}, theme_file_generation: u32 = 0, theme_file_pane: Pane.Id = .at(0), -chrome: colors.ChromeTheme = colors.initial_chrome, +theme_chrome: colors.ChromeTheme = undefined, /// Every drawn frame's timings (FrameStats: the Debug box, PARDES_FRAMES): /// its runner's (`Options.frames`), which its frontend writes as it draws /// and reads input, never the core's memory; a core with no runner (a @@ -523,7 +522,7 @@ pub const replica = .{ // A frame, and the timings that make it. "surface", "facts", "frames", "own_frames", - "chrome", + "theme_chrome", "frame_arena", "scratch", "needs_frame", "echo", // Drained by every pump before its digest; a replica's queues @@ -2162,16 +2161,22 @@ fn sizes(p: *const Pardes, pane: *const Pane) bool { return pane.sized_by == p.live; } -pub fn theme(p: *const Pardes) *const Theme { - if (p.theme_preview) |i| return &themes[i]; +pub fn theme(p: *const Pardes) *const LegacyTheme { + if (p.theme_preview) |i| return &colors.legacy_themes[i]; if (p.custom_theme) |*custom| return custom; - return &themes[p.settings.theme]; + return &colors.legacy_themes[p.settings.theme]; } /// The colours of the chrome: the theme's, set as it changes. pub fn chromeTheme(p: *const Pardes) *const ChromeTheme { if (p.theme_preview != null) return &p.preview_chrome; - return &p.chrome; + return &p.theme_chrome; +} + +/// Every colour a surface draws (colors.Chrome), the theme's or the one a +/// picker previews: worked out as the theme changes, never a frame. +pub fn chrome(p: *const Pardes) *const colors.Chrome { + return &p.chromeTheme().resolved; } /// The theme's own chrome, not a fade's (`theme()`'s, worked out once diff --git a/core/ansi.zig b/core/ansi.zig index 14553817..e4fbf5da 100644 --- a/core/ansi.zig +++ b/core/ansi.zig @@ -21,7 +21,7 @@ const BodyWalk = @import("body_layer.zig").BodyWalk; const EditBuffer = panes.Text.EditBuffer; /// The theme's colours as a terminal's paint takes them. -pub fn inkOf(theme: *const pardes.Theme) Term.Ink { +pub fn inkOf(theme: *const pardes.LegacyTheme) Term.Ink { return .{ .fg = theme.fg, .bg = theme.bg, diff --git a/core/body_layer.zig b/core/body_layer.zig index 5e23a415..07a2d98f 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -150,7 +150,7 @@ pub fn paintPointerSelection( /// The selection colour of each pointer button's sweep: the select button's /// and the look and exec buttons', each tinted toward its own accent. -pub fn buttonTints(th: *const colors.Theme) [3][3]u8 { +pub fn buttonTints(th: *const colors.LegacyTheme) [3][3]u8 { if (th.sweep_bg) |own| return own; const accent2 = if (std.mem.eql(u8, &th.num, &th.str)) th.comment else th.num; return .{ @@ -161,7 +161,7 @@ pub fn buttonTints(th: *const colors.Theme) [3][3]u8 { } /// The ink over each button's sweep. -pub fn buttonInks(th: *const colors.Theme) [3][3]u8 { +pub fn buttonInks(th: *const colors.LegacyTheme) [3][3]u8 { return th.sweep_fg orelse @splat(th.sel_fg); } diff --git a/core/colors.zig b/core/colors.zig index 61af2e4a..eefb2b67 100644 --- a/core/colors.zig +++ b/core/colors.zig @@ -1,6 +1,11 @@ -//! Colour themes: the Theme type, the curated and generated ring, chrome -//! colours, theme files, and switching themes at runtime. +//! Colour themes: the curated and generated ring, every colour a surface +//! draws worked out from a theme once (`Chrome`, DESIGN.md B4 and B6, the +//! themes programme's), theme files, and switching themes at runtime. +//! What a theme says is theme_schema.zig's. const pardes = @import("root.zig"); +const schema = @import("theme_schema.zig"); +pub const Theme = schema.Theme; +pub const LegacyTheme = schema.LegacyTheme; const tagline = @import("tagline.zig"); const panes = @import("panes.zig"); const std = @import("std"); @@ -58,8 +63,10 @@ pub fn relativeLuminance(rgb: [3]u8) f32 { } pub fn contrast(a: [3]u8, b: [3]u8) f32 { - const x = relativeLuminance(a); - const y = relativeLuminance(b); + return luminanceContrast(relativeLuminance(a), relativeLuminance(b)); +} + +fn luminanceContrast(x: f32, y: f32) f32 { return (@max(x, y) + 0.05) / (@min(x, y) + 0.05); } @@ -77,7 +84,10 @@ pub const separator_floor: f32 = 1.5; /// The page a theme with none (`dark`: the host's own) is judged against: /// the GUI's default ground, and a dark terminal's. pub const null_page: [3]u8 = .{ 18, 18, 18 }; -fn separatorOf(th: *const Theme) [3]u8 { +/// The ink of a theme with none, on a dark page and on a light one. +const null_text: [3]u8 = .{ 0xcc, 0xcc, 0xcc }; +const null_text_light: [3]u8 = .{ 0x22, 0x22, 0x22 }; +fn separatorOf(th: *const LegacyTheme) [3]u8 { const base = th.border orelse th.scroll_track; const page = th.bg orelse null_page; if (contrast(base, page) >= separator_floor) return base; @@ -98,10 +108,10 @@ fn separatorOf(th: *const Theme) [3]u8 { /// (acme: the grip's button says it) keeps none. pub const focus_tint_floor: f32 = 1.25; pub const focus_tint_floor_dark: f32 = 1.5; -pub fn focusTintFloor(th: *const Theme) f32 { +pub fn focusTintFloor(th: *const LegacyTheme) f32 { return if (relativeLuminance(th.bg orelse null_page) < 0.18) focus_tint_floor_dark else focus_tint_floor; } -fn focusTint(th: *const Theme) [3]u8 { +fn focusTint(th: *const LegacyTheme) [3]u8 { const tint = th.tag_active_bg orelse return th.tag_bg; const floor = focusTintFloor(th); if (contrast(tint, th.tag_bg) >= floor) return tint; @@ -184,27 +194,38 @@ const name_tint_chroma: f32 = 0.06; /// ink where that hue holds, the name reads on its ground (nameTintFloor) /// and it stands off the ink. Null when no colour of the theme will do. fn derivedNameTint(th: *const Theme, ink: [3]u8, ground: [3]u8) ?[3]u8 { - const pal = th.palette orelse @as([16][3]u8, @splat(th.kw)); - const sources = [_][3]u8{ th.kw, pal[4], pal[5], pal[6], pal[3], pal[2], pal[1], th.num, th.str, th.box } ++ - [_][3]u8{ th.column_box orelse th.num, th.box_dirty orelse th.num, th.sel_bg, th.search_bg orelse th.num } ++ - [_][3]u8{ th.diagnostic_info orelse th.num, th.diagnostic_hint orelse th.num, th.diagnostic_warning orelse th.num } ++ + const pal = th.ansi orelse @as([16][3]u8, @splat(th.keyword)); + const sources = [_][3]u8{ th.keyword, pal[4], pal[5], pal[6], pal[3], pal[2], pal[1], th.number, th.string, th.accent } ++ + [_][3]u8{ th.column_grip orelse th.number, th.dirty orelse th.number, th.selection, th.hit orelse th.number } ++ + [_][3]u8{ th.info orelse th.number, th.hint orelse th.number, th.warning orelse th.number } ++ // A theme with no hue of its own at all (flatwhite): acme's DMedblue's. [_][3]u8{.{ 0x00, 0x00, 0x99 }}; // Near the ink's lightness first, from any of the theme's hues; only // then further off it. - for ([2]f32{ 0.15, 1 }) |reach| for (sources) |source| { - if (nameTintFrom(toOklab(source), ink, ground, reach)) |tint| return tint; + // then further off it (the near steps already tried). + for ([2][2]f32{ .{ -1, 0.15 }, .{ 0.15, 1 } }) |steps| for (sources, 0..) |source, i| { + // A hue already tried finds nothing again. + const tried = for (sources[0..i]) |before| { + if (std.mem.eql(u8, &before, &source)) break true; + } else false; + if (tried) continue; + if (nameTintFrom(toOklab(source), ink, ground, steps[0], steps[1])) |tint| return tint; }; return null; } -fn nameTintFrom(source: [3]f32, ink: [3]u8, ground: [3]u8, reach: f32) ?[3]u8 { +/// A name's tint from `source`'s hue: the first, by its lightness's step +/// off the ink's (over `tried`, up to `reach`), then by the most chroma, +/// that reads on `ground` and stands off the ink. +fn nameTintFrom(source: [3]f32, ink: [3]u8, ground: [3]u8, tried: f32, reach: f32) ?[3]u8 { const chroma = @sqrt(source[1] * source[1] + source[2] * source[2]); if (chroma < name_tint_chroma) return null; const ink_lab = toOklab(ink); const floor = nameTintFloor(ink, ground); + const ground_y = relativeLuminance(ground); var step: f32 = 0; while (step <= reach) : (step += 0.01) { + if (step <= tried) continue; for ([2]f32{ 1, -1 }) |sign| { const lightness = ink_lab[0] + sign * step; if (lightness < 0 or lightness > 1) continue; @@ -212,10 +233,16 @@ fn nameTintFrom(source: [3]f32, ink: [3]u8, ground: [3]u8, reach: f32) ?[3]u8 { var want = @min(chroma, 0.12); while (want >= 0.04) : (want -= 0.01) { const lab: [3]f32 = .{ lightness, source[1] / chroma * want, source[2] / chroma * want }; - const in_gamut = for (oklabLinear(lab)) |v| { + const linear = oklabLinear(lab); + const in_gamut = for (linear) |v| { if (v < -0.001 or v > 1.001) break false; } else true; if (!in_gamut) continue; + // A certain miss by its luminance before the bytes round it + // (they move it by under 0.005) skips the conversion: the + // search's cost, never its answer. + const y = 0.2126 * std.math.clamp(linear[0], 0, 1) + 0.7152 * std.math.clamp(linear[1], 0, 1) + 0.0722 * std.math.clamp(linear[2], 0, 1); + if (@max(luminanceContrast(@max(y - 0.01, 0), ground_y), luminanceContrast(@min(y + 0.01, 1), ground_y)) < floor) continue; const tint = fromOklab(lab); if (contrast(tint, ground) < floor) continue; if (oklabDistance(toOklab(tint), ink_lab) < name_tint_distance) continue; @@ -239,82 +266,785 @@ pub fn mix(a: [3]u8, b: [3]u8) [3]u8 { return out; } -pub const Theme = struct { - name: []const u8, - bg: ?[3]u8, - fg: ?[3]u8, - sel_bg: [3]u8, - sel_fg: [3]u8, - tag_bg: [3]u8, - tag_fg: [3]u8, - box: [3]u8, // move box, focused pane - box_dim: [3]u8, // move box, everything else - kw: [3]u8, - str: [3]u8, - num: [3]u8, - comment: [3]u8, - lineno: [3]u8, - scroll_track: [3]u8, - scroll_thumb: [3]u8, - palette: ?[16][3]u8, +/// The derivations' OKLab, with no pow (B9's budget): a byte's linear +/// light from `to_linear`, and back to a byte by the light where its +/// rounding turns. `toOklab` and `fromOklab` (their f32 pow) stay the old +/// derivations' until those go, so no value they give moves. +fn labOf(rgb: [3]u8) [3]f32 { + const l = [3]f32{ to_linear[rgb[0]], to_linear[rgb[1]], to_linear[rgb[2]] }; + const lm = std.math.cbrt(0.4122214708 * l[0] + 0.5363325363 * l[1] + 0.0514459929 * l[2]); + const mm = std.math.cbrt(0.2119034982 * l[0] + 0.6806995451 * l[1] + 0.1073969566 * l[2]); + const sm = std.math.cbrt(0.0883024619 * l[0] + 0.2817188376 * l[1] + 0.6299787005 * l[2]); + return .{ + 0.2104542553 * lm + 0.7936177850 * mm - 0.0040720468 * sm, + 1.9779984951 * lm - 2.4285922050 * mm + 0.4505937099 * sm, + 0.0259040371 * lm + 0.7827717662 * mm - 0.8086757660 * sm, + }; +} + +fn rgbOf(lab: [3]f32) [3]u8 { + return bytesOf(oklabLinear(lab)); +} + +fn bytesOf(linear: [3]f32) [3]u8 { + var out: [3]u8 = undefined; + for (&out, linear) |*o, v| { + // The bytes whose turn the light has passed: by halves. + const x = std.math.clamp(v, 0, 1); + var lo: usize = 0; + var hi: usize = byte_turns.len; + while (lo < hi) { + const mid = (lo + hi) / 2; + if (byte_turns[mid] <= x) lo = mid + 1 else hi = mid; + } + o.* = @intCast(lo); + } + return out; +} + +/// The linear light at which a channel's byte turns from k to k + 1 (its +/// sRGB value k + 0.5 of 255). +const byte_turns: [255]f32 = blk: { + @setEvalBranchQuota(100_000); + var table: [255]f32 = undefined; + for (&table, 0..) |*out, k| { + const c = (@as(f64, @floatFromInt(k)) + 0.5) / 255; + out.* = @floatCast(if (c <= 0.04045) c / 12.92 else std.math.pow(f64, (c + 0.055) / 1.055, 2.4)); + } + break :blk table; +}; + +/// `toward` in the derivations' OKLab. +fn towardOf(ink: [3]u8, ground: [3]u8, floor: f32) [3]u8 { + var line: Line = .init(ink, ground); + return line.toward(floor); +} + +/// The way from an ink to a ground in OKLab, by hundredths, each point +/// worked out once: `toward` for the several floors one ground's inks take +/// along it (its words, dim and rule). +const Line = struct { + a: [3]f32, + b: [3]f32, + ink: [3]u8, + ground: [3]u8, + at: [101]?[3]u8 = @splat(null), + + fn init(ink: [3]u8, ground: [3]u8) Line { + return .{ .a = labOf(ink), .b = labOf(ground), .ink = ink, .ground = ground }; + } - // Pardes UI roles. Optional so existing ThemeFile documents and imported - // editor palettes keep working; ChromeTheme resolves their legacy defaults. - tag_active_bg: ?[3]u8 = null, - tag_active_fg: ?[3]u8 = null, - /// The workspace row (the tagline): an editor's status bar in its own - /// colours, where it has them; else the tag's. - tagline_bg: ?[3]u8 = null, - tagline_fg: ?[3]u8 = null, - /// Filename/output-name accent, also used by the Term command. - tag_name_fg: ?[3]u8 = null, - /// Active tint; absence preserves the shared name tint or active tag ink. - tag_active_name_fg: ?[3]u8 = null, - /// Column grips stay distinct from pane grips in both focus states. - column_box: ?[3]u8 = null, - column_box_dim: ?[3]u8 = null, - border: ?[3]u8 = null, - /// What an empty column shows under its tag; the border fill without it. - empty_col: ?[3]u8 = null, - search_bg: ?[3]u8 = null, - search_fg: ?[3]u8 = null, - diagnostic_error: ?[3]u8 = null, - diagnostic_warning: ?[3]u8 = null, - diagnostic_info: ?[3]u8 = null, - diagnostic_hint: ?[3]u8 = null, - // The current line number is a quiet foreground cue, not a tag-colored - // background block. Older themes fall back to their normal gutter ink. - lineno_active: ?[3]u8 = null, - /// A tag's selection, where it differs from the body's (acme's #9eeeee). - tag_sel_bg: ?[3]u8 = null, - /// The tag of a reading pane, one a jump steps (Trail.zig): unset, or - /// too near the tag to see, the tag toward paper yellow (readingOf). - tag_reading_bg: ?[3]u8 = null, - /// Each pointer button's sweep, select, exec and look (Mouse.Button - /// order), and the ink over it. Unset, tints of `sel_bg` in `sel_fg`. - sweep_bg: ?[3][3]u8 = null, - sweep_fg: ?[3][3]u8 = null, - /// A pixel shell's rules, in pixels (2 unset); the rule between a tag - /// and its body is always one. - rule_px: ?u8 = null, - /// A grip is acme's button: `box` filled when focused, else a ring of - /// `box_border` (unset: `box_dim`) round the tag's ground; `box_dirty` - /// (unset: `diagnostic_warning`, then `num`) fills it, in either ring, - /// while its file is unsaved. - box_border: ?[3]u8 = null, - box_dirty: ?[3]u8 = null, - /// The rule between a pane's tag and its body, one pixel, quieter than - /// the rules between panes (acme's #8888cc under its 2px black ones). - /// Unset, halfway between the tag's ground and the page. - tag_rule: ?[3]u8 = null, - /// A pixel shell's scroll column, the grip's button over the scrollbar, - /// in pixels at a 17px tagline, the thumb one narrower (acme's 12px - /// Scrollwid, the default). - rail_px: ?u8 = null, + /// The ink as far toward the ground as it still reads `floor`:1 on it + /// (by halves: its contrast falls as it nears the ground). + fn toward(l: *Line, floor: f32) [3]u8 { + var best = l.ink; + var lo: u32 = 0; + var hi: u32 = 101; + while (hi - lo > 1) { + const mid = (lo + hi) / 2; + const c = l.at[mid] orelse point: { + const t = @as(f32, @floatFromInt(mid)) / 100; + const c = rgbOf(.{ l.a[0] + (l.b[0] - l.a[0]) * t, l.a[1] + (l.b[1] - l.a[1]) * t, l.a[2] + (l.b[2] - l.a[2]) * t }); + l.at[mid] = c; + break :point c; + }; + if (contrast(c, l.ground) >= floor) { + lo = mid; + best = c; + } else hi = mid; + } + return best; + } }; +/// Whether a colour reads as a dark ground (pardes's one threshold). +fn isDark(rgb: [3]u8) bool { + return relativeLuminance(rgb) < 0.18; +} + +/// OKLab's lightness, chroma and hue (in degrees) of a colour. +fn lch(rgb: [3]u8) [3]f32 { + const lab = labOf(rgb); + const h = std.math.radiansToDegrees(std.math.atan2(lab[2], lab[1])); + return .{ lab[0], @sqrt(lab[1] * lab[1] + lab[2] * lab[2]), if (h < 0) h + 360 else h }; +} + +fn fromLch(l: f32, c: f32, h: f32) [3]u8 { + const r = std.math.degreesToRadians(h); + return rgbOf(.{ l, c * @cos(r), c * @sin(r) }); +} + +fn hueDistance(a: f32, b: f32) f32 { + const d = @abs(a - b); + return @min(d, 360 - d); +} + +fn inGamut(lab: [3]f32) bool { + return linearInGamut(oklabLinear(lab)); +} + +fn linearInGamut(linear: [3]f32) bool { + for (linear) |v| if (v < -0.001 or v > 1.001) return false; + return true; +} + +/// The colour at lightness `l` in the hue of (`a`, `b`), with as much of +/// that chroma, in fortieths, as the gamut holds there (found by halves). +fn clip(l: f32, a: f32, b: f32) [3]u8 { + const lightness = std.math.clamp(l, 0, 1); + const whole = oklabLinear(.{ lightness, a, b }); + if (linearInGamut(whole)) return bytesOf(whole); + var lo: f32 = 0; + var hi: f32 = 40; + var kept = oklabLinear(.{ lightness, 0, 0 }); + while (hi - lo > 1) { + const mid = @floor((lo + hi) / 2); + const m = oklabLinear(.{ lightness, a * mid / 40, b * mid / 40 }); + if (linearInGamut(m)) { + lo = mid; + kept = m; + } else hi = mid; + } + return bytesOf(kept); +} + +/// R0: `ink` held to `floor` on every one of `grounds` by its OKLab +/// lightness alone, toward whichever end reads better, its hue kept and its +/// chroma as the gamut allows there: a red stays red where `pushAway` +/// washes it toward white. Found by halves, eight mixes; where no +/// lightness reads, the end. +fn hold(ink: [3]u8, grounds: []const [3]u8, floor: f32) [3]u8 { + if (leastContrast(ink, grounds) >= floor) return ink; + const lab = labOf(ink); + const sign: f32 = if (leastContrast(@splat(255), grounds) >= leastContrast(@splat(0), grounds)) 1 else -1; + const reach = if (sign > 0) 1 - lab[0] else lab[0]; + var best = clip(lab[0] + sign * reach, lab[1], lab[2]); + if (leastContrast(best, grounds) < floor) return best; + var lo: f32 = 0; + var hi = reach; + for (0..8) |_| { + const mid = (lo + hi) / 2; + const c = clip(lab[0] + sign * mid, lab[1], lab[2]); + if (leastContrast(c, grounds) >= floor) { + hi = mid; + best = c; + } else lo = mid; + } + return best; +} + +/// R0: a ground `step`:1 off `ground` in lightness (lighter on a dark one), +/// carrying `chroma` of `hue`'s hue: tone says how strongly it asks, hue +/// what it is. Never so far that `ink` drops under `ink_floor` on it; as +/// far as 0.6 in lightness where the step is not reached. By halves. +fn hued(ground: [3]u8, hue: [3]u8, step: f32, chroma: f32, ink: ?[3]u8, ink_floor: f32) [3]u8 { + const g = labOf(ground); + const h = labOf(hue); + const n = @sqrt(h[1] * h[1] + h[2] * h[2]); + const a = g[1] + (if (n > 1e-6) h[1] / n else 0) * chroma; + const b = g[2] + (if (n > 1e-6) h[2] / n else 0) * chroma; + const sign: f32 = if (isDark(ground)) 1 else -1; + const start = clip(g[0], a, b); + if (ink) |i| if (contrast(i, start) < ink_floor) return start; + if (contrast(start, ground) >= step) return start; + // The least lightness that stands the step off, then, where the ink + // loses its floor there, the most the ink allows. + var lo: f32 = 0; + var hi: f32 = 0.6; + var at = clip(g[0] + sign * hi, a, b); + if (contrast(at, ground) >= step) for (0..8) |_| { + const mid = (lo + hi) / 2; + const c = clip(g[0] + sign * mid, a, b); + if (contrast(c, ground) >= step) { + hi = mid; + at = c; + } else lo = mid; + }; + const i = ink orelse return at; + if (contrast(i, at) >= ink_floor) return at; + var keep = start; + lo = 0; + for (0..8) |_| { + const mid = (lo + hi) / 2; + const c = clip(g[0] + sign * mid, a, b); + if (contrast(i, c) >= ink_floor) { + lo = mid; + keep = c; + } else hi = mid; + } + return keep; +} + +/// The six hues a terminal's palette names (R14), in its order from 1: +/// red, green, yellow, blue, magenta, cyan, as OKLab angles. +const ansi_hues = [6]f32{ 25, 145, 95, 255, 330, 195 }; + +/// A canonical hue at the text's lightness (eased into a readable band), +/// as much chroma as the gamut holds up to 0.13, held 4.5:1 on the page. +fn canonical(hue: f32, page: [3]u8, text: [3]u8) [3]u8 { + const lt = lch(text)[0]; + const l = if (isDark(page)) std.math.clamp(lt, 0.72, 0.84) else std.math.clamp(lt, 0.40, 0.52); + const r = std.math.degreesToRadians(hue); + var c: f32 = 0.13; + while (c > 0.02 and !inGamut(.{ l, c * @cos(r), c * @sin(r) })) c -= 0.01; + return hold(fromLch(l, c, hue), &.{page}, Chrome.text_floor); +} + +/// The theme's own colour nearest a hue (within 30 degrees, with 0.06 of +/// chroma or more), or null. +fn nearestOwn(hue: f32, own: []const ?[3]u8) ?[3]u8 { + var best: ?[3]u8 = null; + var best_d: f32 = 31; + for (own) |o| { + const c = o orelse continue; + const v = lch(c); + const d = hueDistance(v[2], hue); + if (v[1] >= 0.06 and d <= 30 and d < best_d) { + best = c; + best_d = d; + } + } + return best; +} + +/// R14: a theme with no sixteen of its own gets them from its own hues +/// where it has them, else canonical ones at its text's lightness, each +/// held 4.5:1 on the page, a bright one 0.06 further from it; 0 the tag, +/// 7 and 8 the text toward the page to 7:1 and 3:1, 15 the text. +fn derivedAnsi(th: *const Theme, page: [3]u8, text: [3]u8) [16][3]u8 { + const own = [_]?[3]u8{ th.keyword, th.string, th.number, th.accent, th.@"error", th.warning, th.info, th.hint, th.added, th.deleted }; + const step: f32 = if (isDark(page)) 0.06 else -0.06; + var pal: [16][3]u8 = undefined; + for (ansi_hues, 1..) |hue, i| { + const c = hold(nearestOwn(hue, &own) orelse canonical(hue, page, text), &.{page}, Chrome.text_floor); + pal[i] = c; + const v = lch(c); + const l = std.math.clamp(v[0] + step, 0, 1); + const r = std.math.degreesToRadians(v[2]); + const bright = if (inGamut(.{ l, v[1] * @cos(r), v[1] * @sin(r) })) fromLch(l, v[1], v[2]) else c; + pal[8 + i] = hold(bright, &.{page}, Chrome.text_floor); + } + pal[0] = th.tag; + pal[7] = towardOf(text, page, 7); + pal[8] = towardOf(text, page, 3); + pal[15] = text; + return pal; +} + +/// Every colour a surface draws, worked out from a theme once (DESIGN.md +/// B4 R0-R16; B6 the set): a theme change or a preview resolves it, never a +/// frame, a row or a cell. Each ink is resolved per ground it is drawn on +/// (B1.3), never held to one floor across all of them. +pub const Chrome = struct { + /// The body's page and its inks. + page: Inks, + /// A tag's two grounds, plain and focused, in every mode (`Ground`; + /// R5', G1': the mode is a mark on the grip, never a ground). + tag: [2]Inks, + /// The notice row's (H: the window row and the column tags are drawn + /// on the page, in `page.word`). + status: Inks, + /// The theme leaves page, text and the sixteen to the host terminal + /// (`dark`): a terminal draws its own; a window, `null_page`. + host_page: bool, + /// Where the selection is drawn faintly: a second range, hover, a + /// selecting prompt's dim. + selection_secondary: [3]u8, + /// The pointer buttons' sweeps: select, exec, look (acme's meanings). + sweeps: [3]Theme.Sweep, + /// Search: a hit's ground, and the current one's ground and ink. + hit: [3]u8, + hit_current: [3]u8, + hit_current_text: [3]u8, + add: Diff, + del: Diff, + /// The Changes layer's lanes, a pin each (`lanes_len` of them), and + /// churn's eight steps from cool to hot. + lanes: [8][3]u8, + lanes_len: u8, + heat: [8][3]u8, + grip: Grip, + rule: [3]u8, + tag_rule: [3]u8, + rail: [3]u8, + rail_thumb: [3]u8, + empty_column: [3]u8, + /// A syntax class's ink, by syntax.Syn: none (the text), keyword, + /// string, number, comment. + syntax: [5][3]u8, + line_number: [3]u8, + line_number_current: [3]u8, + /// A name's tint on the plain tag and on the focused one. + name: [2][3]u8, + ansi: [16][3]u8, + rule_px: u8, + rail_px: u8, + + /// The inks one ground carries (B1.3): its text (the full ink: the + /// user's, a path, `Keep`), word (computed command words: the text + /// toward the ground to 4.5:1, H), dim (hints, meta, forward steps), + /// rule (spines), key (the accent: keys, `▶`, the walked word), match + /// (a query's letters), the four severities, and a selection rebuilt + /// on it with its own. + pub const Inks = struct { + ground: [3]u8, + text: [3]u8, + word: [3]u8, + dim: [3]u8, + rule: [3]u8, + key: [3]u8, + match: [3]u8, + severity: [4][3]u8, + selection: Selection, + }; + pub const Selection = struct { ground: [3]u8, text: [3]u8, dim: [3]u8 }; + pub const Ground = enum(u1) { plain, focus }; + pub const Severity = enum(u2) { @"error", warning, info, hint }; + /// A diff's rows: a row's ground, its changed words', a faint line's, + /// and its marker. + pub const Diff = struct { row: [3]u8, emph: [3]u8, line: [3]u8, ink: [3]u8 }; + /// The grips: focused, its ring at rest, a reading pane's mark (its + /// ring; a terminal's cell) and that mark as a glyph on a focused grip, + /// unsaved, a column's grabbed and at rest, and the ink on any fill. + pub const Grip = struct { focus: [3]u8, ring: [3]u8, mode: [3]u8, mode_glyph: [3]u8, dirty: [3]u8, column: [3]u8, column_rest: [3]u8, ink: [3]u8 }; + + // The floors (B4's table). + pub const text_floor: f32 = 4.5; + /// A tag's full ink, where its gamut allows (H). + pub const tag_text_floor: f32 = 7; + /// The plain tag off the page (H: K'5.7). + pub const band_floor: f32 = 1.1; + pub const dim_floor: f32 = 3.5; + pub const rule_ink: f32 = 2; + pub const focus_dark: f32 = 1.5; + pub const focus_light: f32 = 1.25; + pub const grip_floor: f32 = 3; + pub const ring_floor: f32 = 2; + pub const selection_dark: f32 = 1.4; + pub const selection_light: f32 = 1.25; + pub const hit_floor: f32 = 1.5; + pub const mode_floor: f32 = 3; + pub const severity_apart: f32 = 0.06; + pub const row_step: f32 = 1.15; + pub const emph_step: f32 = 1.45; + pub const line_step: f32 = 1.06; + pub const separator_lift: f32 = 1.6; + + pub fn on(c: *const Chrome, g: Ground) *const Inks { + return &c.tag[@backingInt(g)]; + } + + pub fn of(th: *const Theme) Chrome { + var c: Chrome = undefined; + // R1, R2: one page for every derivation and frontend. + const page = th.page orelse null_page; + const dark = isDark(page); + const text = th.text orelse if (dark) null_text else null_text_light; + c.host_page = th.page == null; + // R14. + c.ansi = th.ansi orelse derivedAnsi(th, page, text); + const ansi = &c.ansi; + + // R11's rule base (the theme's own tag: a lifted one is no rule). + const rule_base = th.rule orelse th.rail orelse th.tag; + c.rule = if (contrast(rule_base, page) >= separator_floor) rule_base else hold(rule_base, &.{page}, separator_lift); + + // R3, R4 (H, G1'): the tag's two grounds, focus by tone alone. The + // plain tag a band 1.1:1 off the page (it is all that parts two + // panes in a terminal), unless rules of 3:1 part them (acme's). + var tag = th.tag; + if (contrast(tag, page) < band_floor and contrast(c.rule, page) < 3) tag = liftedOff(tag, page); + var own_text = th.tag_text; + var own_focus_text = th.tag_focus_text orelse th.tag_text; + // The focused band the loudest at the top: a focus step off the + // tag, ending further off the page; a theme's own where it does. + var focus_floor = if (isDark(tag)) focus_dark else focus_light; + var focus = focusOf(th.tag_focus, tag, page, focus_floor); + // A tag no ink reads on at 4.5:1 beside its focus (a mid-grey), or + // one with no focus step louder than it, takes the page a 1.15 step + // off, and the text. + if (focus == null or @max(leastContrast(@splat(255), &.{ tag, focus.? }), leastContrast(@splat(0), &.{ tag, focus.? })) < text_floor) { + tag = hued(page, page, 1.15, 0, null, 0); + own_text = text; + own_focus_text = text; + focus_floor = if (isDark(tag)) focus_dark else focus_light; + focus = stepOff(tag, awayFrom(tag, page), tag, focus_floor, null) orelse tag; + } + const focused = focus.?; + const grounds = [2][3]u8{ tag, focused }; + // The full ink up to 7:1 on each, 4.5 at least; the words drawn in + // it toward the ground (`Inks.word`). + const tag_text = hold(own_text, &.{tag}, tag_text_floor); + const focus_text = hold(own_focus_text, &.{focused}, tag_text_floor); + const status = th.workspace orelse tag; + const status_text = hold(th.workspace_text orelse tag_text, &.{status}, text_floor); + + // R7: the selection, lifted where it stands too near the page. + const sel_floor = if (dark) selection_dark else selection_light; + const own_sel = th.selection; + const sel = if (contrast(own_sel, page) < sel_floor - 0.005) hued(page, own_sel, sel_floor, chromaOf(own_sel), null, 0) else own_sel; + const sel_text = hold(th.selection_text orelse text, &.{sel}, text_floor); + c.selection_secondary = mixOklab(page, sel, 0.5); + + // R9: the severities on the page, each told from the text and the + // ones before it; then on every tag ground and the status row's. + const candidates = [4][6]?[3]u8{ + .{ th.@"error", ansi[1], canonical(ansi_hues[0], page, text), null, null, null }, + .{ th.warning, ansi[3], canonical(ansi_hues[2], page, text), ansi[11], null, null }, + .{ th.info, ansi[4], ansi[6], canonical(ansi_hues[3], page, text), canonical(ansi_hues[5], page, text), null }, + .{ th.hint, ansi[6], ansi[2], canonical(ansi_hues[5], page, text), canonical(ansi_hues[1], page, text), canonical(ansi_hues[4], page, text) }, + }; + const severity = severitiesOn(page, text, candidates, null); + + // R8: the hits' hue and grounds. + const ink_floor = @min(text_floor, contrast(text, page) * 0.9); + const own_hit: ?[3]u8 = if (th.hit) |own| + (if (contrast(own, page) >= hit_floor - 0.01 and contrast(text, own) >= ink_floor - 0.01) own else null) + else + null; + var hue = severity[1]; + if (own_hit) |own| { + hue = own; + c.hit = own; + } else { + // Amber, unless the selection is already that hue: then the + // number's, then the accent's. + const s = lch(sel); + for ([3][3]u8{ severity[1], th.number, th.accent }) |cand| { + if (!(hueDistance(lch(cand)[2], s[2]) < 30 and s[1] > 0.03)) { + hue = cand; + break; + } + } + // A dark yellow is olive: amber on a dark page. + const v = lch(hue); + if (dark and v[2] > 80 and v[2] < 115) hue = fromLch(0.75, @max(v[1], 0.1), 70); + c.hit = hued(page, hue, hit_floor, if (dark) 0.07 else 0.10, text, ink_floor); + } + // The current one: the hue itself as its ground, the page as its + // ink (vim's IncSearch): room for it in every theme. + c.hit_current = hold(hue, &.{page}, text_floor); + c.hit_current_text = page; + + // Every ground's inks; on the tags and the status row, the + // tag's selection rebuilt on each where it stands too near it. + c.page = groundInks(th, page, text, sel, sel_text, severity, hue); + const own_tag_sel = th.tag_selection orelse own_sel; + for ([3]*Inks{ &c.tag[0], &c.tag[1], &c.status }, [3][3]u8{ tag, focused, status }, [3][3]u8{ tag_text, focus_text, status_text }, 0..) |inks, ground, ink, i| { + // A status row the plain tag's own (no status bar of its own). + if (i == 2 and std.mem.eql(u8, &ground, &tag) and std.mem.eql(u8, &ink, &tag_text)) { + inks.* = c.tag[0]; + continue; + } + const floor = if (isDark(ground)) selection_dark else selection_light; + const tag_sel = if (contrast(own_tag_sel, ground) >= floor - 0.005 and contrast(ink, own_tag_sel) >= text_floor) + own_tag_sel + else + hued(ground, own_tag_sel, floor, chromaOf(own_tag_sel), ink, text_floor); + inks.* = groundInks(th, ground, ink, tag_sel, hold(ink, &.{tag_sel}, text_floor), severitiesOn(ground, ink, candidates, severity), hue); + } + + c.sweeps = th.sweeps orelse .{ + .{ .ground = sel, .text = sel_text }, + sweepOf(page, severity[0], sel_floor, text), + sweepOf(page, th.added orelse ansi[2], sel_floor, text), + }; + + // R6, R5', H: the grips. + c.grip.focus = hold(th.accent, &.{focused}, grip_floor); + c.grip.ring = th.grip_ring orelse towardOf(hold(th.accent, &.{tag}, ring_floor), tag, ring_floor); + c.grip.mode = if (th.mode) |own| hold(own, &grounds, mode_floor) else modeOf(&grounds, .{ c.grip.focus, severity[1], severity[0] }); + c.grip.mode_glyph = hold(c.grip.mode, &.{c.grip.focus}, mode_floor); + c.grip.dirty = hold(th.dirty orelse severity[1], &grounds, grip_floor); + // A column's grip on the page (H: the column row has no band; a + // port's own, made for its tag, held there too), grabbed 3:1, at + // rest a step quieter, 2:1. + const column = th.column_grip orelse th.number; + c.grip.column = hold(column, &.{page}, grip_floor); + c.grip.column_rest = towardOf(hold(column, &.{page}, ring_floor), page, ring_floor); + const fills = [5][3]u8{ c.grip.focus, c.grip.dirty, c.grip.column, c.grip.column_rest, c.grip.mode }; + c.grip.ink = hold(if (leastContrast(page, &fills) >= leastContrast(tag_text, &fills)) page else tag_text, &fills, text_floor); + + // R10: added and deleted. + c.add = diffOf(page, text, th.added orelse ansi[2], dark); + c.del = diffOf(page, text, th.deleted orelse ansi[1], dark); + + // R11: the frame. + c.tag_rule = th.tag_rule orelse mixOklab(tag, page, 0.5); + c.rail = th.rail orelse tag; + c.rail_thumb = th.rail_thumb orelse c.tag[0].rule; + c.empty_column = th.empty_column orelse c.rule; + c.rule_px = th.rule_px orelse 2; + c.rail_px = th.rail_px orelse 12; + + // The original's own, never moved (B1.5); R12, R13. + c.syntax = .{ text, th.keyword, th.string, th.number, th.comment }; + c.line_number = th.line_number orelse towardOf(text, page, 3); + c.line_number_current = th.line_number_current orelse towardOf(text, page, text_floor); + // A name keeps its hue focused: the plain tag's tint at the focused + // ink's lightness, else the search on that ground. + c.name[0] = th.tag_name orelse derivedNameTint(th, tag_text, tag) orelse tag_text; + const named = labOf(c.name[0]); + c.name[1] = nameTintFrom(named, focus_text, focused, -1, 0.15) orelse nameTintFrom(named, focus_text, focused, 0.15, 1) orelse + derivedNameTint(th, focus_text, focused) orelse focus_text; + + // R10: the lanes (the accents but green and red, hue-sorted) and + // churn's steps from the line numbers' ink to the deleted hue. + c.lanes_len = lanesOf(.{ ansi[3], ansi[4], ansi[5], ansi[6], th.number, th.keyword }, .{ th.deleted orelse ansi[1], th.added orelse ansi[2] }, &c.lanes); + for (&c.heat, 1..) |*h, k| h.* = mixOklab(c.line_number, th.deleted orelse ansi[1], @as(f32, @floatFromInt(k)) / 8); + return c; + } +}; + +fn chromaOf(rgb: [3]u8) f32 { + return lch(rgb)[1]; +} + +/// Which way in lightness is away from the page: up from a page under +/// `c`, down from one over it; up on a dark page where they are level. +fn awayFrom(c: [3]u8, page: [3]u8) f32 { + const l = labOf(c)[0]; + const p = labOf(page)[0]; + if (@abs(l - p) < 0.005) return if (isDark(page)) 1 else -1; + return if (l > p) 1 else -1; +} + +/// `c` moved by its lightness alone (`clip`, its hue kept), `sign` up or +/// down, by the least that reads `floor`:1 off `off` and, where `past` is +/// given, ends beyond that lightness; by halves, up to 0.75 in lightness; +/// null where none does. +fn stepOff(c: [3]u8, sign: f32, off: [3]u8, floor: f32, past: ?f32) ?[3]u8 { + const lab = labOf(c); + const reach: f32 = 0.75; + var best = clip(lab[0] + sign * reach, lab[1], lab[2]); + if (!steppedOff(best, sign, off, floor, past)) return null; + var lo: f32 = 0; + var hi = reach; + for (0..8) |_| { + const mid = (lo + hi) / 2; + const m = clip(lab[0] + sign * mid, lab[1], lab[2]); + if (steppedOff(m, sign, off, floor, past)) { + hi = mid; + best = m; + } else lo = mid; + } + return best; +} + +fn steppedOff(c: [3]u8, sign: f32, off: [3]u8, floor: f32, past: ?f32) bool { + if (contrast(c, off) < floor) return false; + const p = past orelse return true; + return sign * (labOf(c)[0] - p) > 0; +} + +/// H: a plain tag too near the page lifted to a band off it, further from +/// it where the gamut has room, else across it (a white tag on cream). +fn liftedOff(tag: [3]u8, page: [3]u8) [3]u8 { + const away = awayFrom(tag, page); + const p = labOf(page)[0]; + return stepOff(tag, away, page, Chrome.band_floor, p) orelse stepOff(tag, -away, page, Chrome.band_floor, p) orelse tag; +} + +/// R4 under H: the focused tag a focus step off the plain one and further +/// off the page than it: the theme's own where it is that, on the tag's +/// side of light; else the tag moved the step lighter (darker on a light +/// tag), else the other way: away from the page or past it, whichever +/// stands louder (a tag sunk under its page rises above it, catppuccin's; +/// a soft theme's never sinks to black); null where neither does. +fn focusOf(own: ?[3]u8, tag: [3]u8, page: [3]u8, floor: f32) ?[3]u8 { + if (own) |o| if (louder(o, tag, page, floor) and isDark(o) == isDark(tag)) return o; + const lighter: f32 = if (isDark(tag)) 1 else -1; + for ([2]f32{ lighter, -lighter }) |sign| { + const f = stepOff(tag, sign, tag, floor, null) orelse continue; + if (louder(f, tag, page, floor)) return f; + } + return null; +} + +fn louder(focus: [3]u8, tag: [3]u8, page: [3]u8, floor: f32) bool { + return contrast(focus, tag) >= floor - 0.005 and contrast(focus, page) > contrast(tag, page); +} + +/// The mode's hues (R5', G1'): sky, mint, violet, paper, rose. +const mode_hues = [5]f32{ 230, 165, 300, 95, 350 }; + +/// R5': a reading pane's mark, quiet but findable: the first mode hue 40 +/// degrees or more from the hues a grip already says (focus, unsaved, an +/// error), else the one furthest from them, at chroma 0.11 as the gamut +/// allows, at the least lightness off the two tags' mean (lighter on a +/// dark tag) that reads 3:1 on both. By halves. +fn modeOf(grounds: *const [2][3]u8, said: [3][3]u8) [3]u8 { + var hues: [3]f32 = undefined; + var n: usize = 0; + for (said) |c| { + const v = lch(c); + if (v[1] < 0.04) continue; + hues[n] = v[2]; + n += 1; + } + var hue = mode_hues[0]; + var furthest: f32 = -1; + for (mode_hues) |h| { + var apart: f32 = 180; + for (hues[0..n]) |taken| apart = @min(apart, hueDistance(h, taken)); + if (apart >= 40) { + hue = h; + break; + } + if (apart > furthest) { + hue = h; + furthest = apart; + } + } + const a = 0.11 * @cos(std.math.degreesToRadians(hue)); + const b = 0.11 * @sin(std.math.degreesToRadians(hue)); + const mean = (labOf(grounds[0])[0] + labOf(grounds[1])[0]) / 2; + const sign: f32 = if (isDark(grounds[0])) 1 else -1; + var lo: f32 = 0; + var hi: f32 = 0.8; + var best = clip(mean + sign * hi, a, b); + if (leastContrast(best, grounds) < Chrome.mode_floor) return best; + for (0..8) |_| { + const mid = (lo + hi) / 2; + const c = clip(mean + sign * mid, a, b); + if (leastContrast(c, grounds) >= Chrome.mode_floor) { + hi = mid; + best = c; + } else lo = mid; + } + return best; +} + +/// R9: the four severities on one ground: each the first candidate that, +/// held 4.5:1 there, stands 0.06 off the ground's text and the ones before +/// it, else the one that stands furthest; on a ground other than the page, +/// the page's own tried first. +fn severitiesOn(ground: [3]u8, text: [3]u8, candidates: [4][6]?[3]u8, page: ?[4][3]u8) [4][3]u8 { + var out: [4][3]u8 = undefined; + var taken: [5][3]f32 = undefined; + taken[0] = labOf(text); + // A colour two lists name (cyan, a canonical hue) is held once: up to + // the 28 the lists hold, each with its held ink and that ink's OKLab. + var seen: [28]struct { from: [3]u8, held: [3]u8, lab: [3]f32 } = undefined; + var n_seen: usize = 0; + for (&out, candidates, 0..) |*severity, own, k| { + var best = own[1].?; + var best_d: f32 = -1; + const first: ?[3]u8 = if (page) |p| p[k] else null; + for ([1]?[3]u8{first} ++ own) |candidate| { + const from = candidate orelse continue; + const at = for (seen[0..n_seen], 0..) |was, i| { + if (std.mem.eql(u8, &was.from, &from)) break i; + } else at: { + const held = hold(from, &.{ground}, Chrome.text_floor); + seen[n_seen] = .{ .from = from, .held = held, .lab = labOf(held) }; + n_seen += 1; + break :at n_seen - 1; + }; + const held = seen[at].held; + const lab = seen[at].lab; + var d: f32 = 2; + for (taken[0 .. k + 1]) |t| d = @min(d, oklabDistance(lab, t)); + if (d >= Chrome.severity_apart) { + best = held; + break; + } + if (d > best_d) { + best = held; + best_d = d; + } + } + severity.* = best; + taken[k + 1] = labOf(best); + } + return out; +} + +/// The inks one ground carries (`Chrome.Inks`). +fn groundInks(th: *const Theme, ground: [3]u8, text: [3]u8, sel: [3]u8, sel_text: [3]u8, severity: [4][3]u8, hue: [3]u8) Chrome.Inks { + var line: Line = .init(text, ground); + return .{ + .ground = ground, + .text = text, + .word = if (contrast(text, ground) <= Chrome.text_floor) text else line.toward(Chrome.text_floor), + .dim = line.toward(Chrome.dim_floor), + .rule = line.toward(Chrome.rule_ink), + .key = hold(th.accent, &.{ground}, Chrome.text_floor), + .match = hold(hue, &.{ground}, Chrome.text_floor), + .severity = severity, + .selection = .{ + .ground = sel, + .text = sel_text, + .dim = towardOf(sel_text, sel, Chrome.dim_floor), + }, + }; +} + +/// R7: an exec or look sweep: the selection's step off the page in its +/// meaning's hue (acme's #aa0000 exec, #006600 look), its ink 4.5:1 on it. +fn sweepOf(page: [3]u8, hue: [3]u8, floor: f32, text: [3]u8) Theme.Sweep { + const ground = hued(page, hue, floor, if (isDark(page)) 0.07 else 0.10, text, Chrome.text_floor); + return .{ .ground = ground, .text = hold(text, &.{ground}, Chrome.text_floor) }; +} + +/// R10: a diff's rows in `hue`: a row 1.15:1 off the page, its changed +/// words 1.45:1, a faint line 1.06:1, never so far the text drops under +/// its floor; the marker the hue held 4.5:1 on the row and the words. +fn diffOf(page: [3]u8, text: [3]u8, hue: [3]u8, dark: bool) Chrome.Diff { + const floor = @min(Chrome.text_floor, contrast(text, page) * 0.9); + const row = hued(page, hue, Chrome.row_step, if (dark) 0.05 else 0.06, text, floor); + const emph = hued(page, hue, Chrome.emph_step, if (dark) 0.08 else 0.10, text, floor); + return .{ .row = row, .emph = emph, .line = hued(page, hue, Chrome.line_step, 0.025, null, 0), .ink = hold(hue, &.{ row, emph }, Chrome.text_floor) }; +} + +/// The Changes layer's lanes (jj.zig's rule, once a theme): the accents but +/// the two that mean written and taken away, in hue order from the top, +/// each 15 degrees or more from the one kept before it. +fn lanesOf(accents: [6][3]u8, taken: [2][3]u8, out: *[8][3]u8) u8 { + var sorted: [6][3]u8 = undefined; + var n: usize = 0; + for (accents) |c| { + // A grey is no accent; one near written or taken away means that. + if (hsvChroma(c) < 48 or hsvApart(c, taken[0]) < 15 or hsvApart(c, taken[1]) < 15) continue; + sorted[n] = c; + n += 1; + } + std.sort.insertion([3]u8, sorted[0..n], {}, struct { + fn above(_: void, a: [3]u8, b: [3]u8) bool { + return hsvHue(a) > hsvHue(b); + } + }.above); + out.* = @splat(@splat(0)); + var kept: u8 = 0; + for (sorted[0..n]) |c| { + if (kept > 0 and hsvApart(c, out[kept - 1]) < 15) continue; + out[kept] = c; + kept += 1; + } + return kept; +} + +fn hsvChroma(c: [3]u8) u8 { + return @max(c[0], c[1], c[2]) - @min(c[0], c[1], c[2]); +} + +fn hsvHue(c: [3]u8) f32 { + const r: f32 = @floatFromInt(c[0]); + const g: f32 = @floatFromInt(c[1]); + const b: f32 = @floatFromInt(c[2]); + const hi = @max(r, g, b); + const d = hi - @min(r, g, b); + if (d == 0) return 0; + const h = if (hi == r) @mod((g - b) / d, 6) else if (hi == g) (b - r) / d + 2 else (r - g) / d + 4; + return 60 * h; +} + +fn hsvApart(a: [3]u8, b: [3]u8) f32 { + return hueDistance(hsvHue(a), hsvHue(b)); +} + /// The ink a syntax class is drawn in; a byte of none keeps its text's own. -pub fn synInk(th: *const Theme, sy: @import("syntax.zig").Syn) ?[3]u8 { +pub fn synInk(th: *const LegacyTheme, sy: @import("syntax.zig").Syn) ?[3]u8 { return switch (sy) { .none => null, .keyword => th.kw, @@ -336,7 +1066,7 @@ pub const Grounds = struct { pub const Ground = struct { row: [3]u8, emph: [3]u8, line: [3]u8, ink: [3]u8 }; - pub fn of(th: *const Theme) Grounds { + pub fn of(th: *const LegacyTheme) Grounds { const page = th.bg orelse th.tag_bg; const ink = th.fg orelse th.tag_fg; const green: [3]u8 = if (th.palette) |pal| pal[2] else .{ 0x2e, 0xa0, 0x43 }; @@ -452,13 +1182,15 @@ const curated = struct { pub const dark = @import("themes/dark.zig"); }; -fn fold(comptime C: type) [@typeInfo(C).@"struct".decl_names.len]Theme { +/// A struct of theme files as themes in the old names: each file's +/// `theme`, a field it leaves out its default. +fn fold(comptime C: type) [@typeInfo(C).@"struct".decl_names.len]LegacyTheme { comptime { @setEvalBranchQuota(400000); - var out: [@typeInfo(C).@"struct".decl_names.len]Theme = undefined; - const theme_info = @typeInfo(Theme).@"struct"; + var out: [@typeInfo(C).@"struct".decl_names.len]LegacyTheme = undefined; + const theme_info = @typeInfo(LegacyTheme).@"struct"; for (@typeInfo(C).@"struct".decl_names, 0..) |d, i| { - var t: Theme = undefined; + var t: LegacyTheme = undefined; const source = @field(C, d).theme; for (theme_info.field_names, theme_info.field_types, theme_info.field_attrs) |name, T, attrs| { @field(t, name) = if (@hasField(@TypeOf(source), name)) @@ -474,7 +1206,18 @@ fn fold(comptime C: type) [@typeInfo(C).@"struct".decl_names.len]Theme { } } -pub const themes = fold(curated) ++ fold(@import("generated_themes")); +/// The ring in the old names: what `Pardes.theme()` hands the surfaces +/// that still read a theme's own fields, until they read `Chrome` (the +/// themes programme's L2 lines). +pub const legacy_themes = fold(curated) ++ fold(@import("generated_themes")); +/// The ring: pardes's own (`native_count` of them), then the imported. +/// The files still speak the old names: renamed here, at comptime. +pub const themes: [legacy_themes.len]Theme = blk: { + @setEvalBranchQuota(400000); + var out: [legacy_themes.len]Theme = undefined; + for (&out, legacy_themes) |*t, old| t.* = .fromLegacy(old); + break :blk out; +}; /// The first `native_count` of `themes` are pardes's own; the rest imported. pub const native_count = @typeInfo(curated).@"struct".decl_names.len; @@ -559,18 +1302,27 @@ pub const ChromeTheme = struct { scroll_track: [3]u8, scroll_thumb: [3]u8, roles: Roles, + /// Everything B4 derives (`Chrome`), what the surfaces move to; the + /// fields above and `roles` are the old derivations' values, kept for + /// the surfaces that still read them until they do (the themes + /// programme's L2 lines), so no frame moves before its surface does. + resolved: Chrome, + + /// A theme in the old names' chrome. + pub fn fromTheme(th: *const LegacyTheme) ChromeTheme { + return .of(&.fromLegacy(th.*), th); + } - pub fn fromTheme(th: *const Theme) ChromeTheme { - // The initial chrome is resolved at compile time: contrast takes pow. - @setEvalBranchQuota(1_000_000); + /// A theme's chrome, from the theme and its old names' view. + pub fn of(theme: *const Theme, th: *const LegacyTheme) ChromeTheme { const active_bg = focusTint(th); const active_fg = th.tag_active_fg orelse th.tag_fg; // A name is always tinted: a theme that names no tint gets one of // its own hues, on each ground. One that names only the plain tint // keeps it when focused too. - const name_fg = th.tag_name_fg orelse (derivedNameTint(th, th.tag_fg, th.tag_bg) orelse th.tag_fg); + const name_fg = th.tag_name_fg orelse (derivedNameTint(theme, th.tag_fg, th.tag_bg) orelse th.tag_fg); const active_name_fg = th.tag_active_name_fg orelse - (th.tag_name_fg orelse (derivedNameTint(th, active_fg, active_bg) orelse active_fg)); + (th.tag_name_fg orelse (derivedNameTint(theme, active_fg, active_bg) orelse active_fg)); const reading = readingOf(th); return .{ .tag_bg = th.tag_bg, @@ -599,6 +1351,7 @@ pub const ChromeTheme = struct { .scroll_track = th.scroll_track, .scroll_thumb = th.scroll_thumb, .roles = .of(th, th.tag_bg, active_bg, reading), + .resolved = .of(theme), }; } }; @@ -611,17 +1364,17 @@ pub const TagGround = enum { plain, focused, reading, reading_focused }; /// The inks drawn on one ground: labels and rows (`text`, 4.5:1 at least), /// hints, meta and forward steps (`dim`, 3.5:1, never under 3), and the /// spines and the rules between steps (`rule`, 2:1). -pub const Inks = struct { ground: [3]u8, text: [3]u8, dim: [3]u8, rule: [3]u8 }; +pub const RoleInks = struct { ground: [3]u8, text: [3]u8, dim: [3]u8, rule: [3]u8 }; /// The walk UI's colour roles (UX DESIGN.md D4), worked out from a theme /// once, with its chrome (never a frame), each held to its floor on every /// ground it is drawn on: a tag's four (`tag`) and a band's cursor row on /// each of them (`selected`, its ground that tag's selection). pub const Roles = struct { - tag: [4]Inks, - selected: [4]Inks, + tag: [4]RoleInks, + selected: [4]RoleInks, /// The notice row's (the tagline's ground): its dim words. - notice: Inks, + notice: RoleInks, /// Key cells, a level's or a row's key letters, `▶`, `•`, a target's /// keys: 4.5:1 on every tag ground and on the selection. key: [3]u8, @@ -648,11 +1401,11 @@ pub const Roles = struct { pub const hit_current_floor: f32 = 1.9; pub const reading_distance: f32 = 0.04; - pub fn on(r: *const Roles, ground: TagGround) *const Inks { + pub fn on(r: *const Roles, ground: TagGround) *const RoleInks { return &r.tag[@backingInt(ground)]; } - pub fn selectedOn(r: *const Roles, ground: TagGround) *const Inks { + pub fn selectedOn(r: *const Roles, ground: TagGround) *const RoleInks { return &r.selected[@backingInt(ground)]; } @@ -662,7 +1415,7 @@ pub const Roles = struct { return if (relativeLuminance(ground) < 0.18) 1.4 else 1.25; } - fn of(th: *const Theme, plain: [3]u8, focused: [3]u8, reading: [3]u8) Roles { + fn of(th: *const LegacyTheme, plain: [3]u8, focused: [3]u8, reading: [3]u8) Roles { const grounds = [4][3]u8{ plain, focused, reading, reading }; const plain_ink = th.tag_fg; const focused_ink = th.tag_active_fg orelse th.tag_fg; @@ -703,7 +1456,7 @@ pub const Roles = struct { /// A tag's text, dim and rule inks on `ground`, from its ink (pushed to the /// text floor where it has less). -fn inksOn(ground: [3]u8, ink: [3]u8) Inks { +fn inksOn(ground: [3]u8, ink: [3]u8) RoleInks { const text = pushAway(ink, &.{ground}, Roles.text_floor); return .{ .ground = ground, .text = text, .dim = toward(text, ground, 3.5), .rule = toward(text, ground, 2) }; } @@ -758,7 +1511,7 @@ fn leastContrast(ink: [3]u8, grounds: []const [3]u8) f32 { /// The match's hue: the search's ink where it is a hue of its own (a port's /// is often the page's or the tag's, set for the search's ground), else the /// theme's warm accent. -fn matchHue(th: *const Theme) [3]u8 { +fn matchHue(th: *const LegacyTheme) [3]u8 { const own = th.search_fg orelse return th.num; const lab = toOklab(own); return if (@sqrt(lab[1] * lab[1] + lab[2] * lab[2]) >= name_tint_chroma) own else th.num; @@ -841,7 +1594,7 @@ fn hitOf(page: [3]u8, hue: [3]u8, ink: [3]u8, t: f32, floor: f32) [3]u8 { /// as a page; then less light, or darker, where its text would lose /// contrast, and further where the bytes cannot show so small a step (a /// near-black tag). -fn readingOf(th: *const Theme) [3]u8 { +fn readingOf(th: *const LegacyTheme) [3]u8 { if (th.tag_reading_bg) |own| if (oklabDistance(toOklab(own), toOklab(th.tag_bg)) >= Roles.reading_distance) return own; const tag = toOklab(th.tag_bg); const paper = toOklab(.{ 0xdc, 0xc8, 0x78 }); @@ -872,8 +1625,6 @@ fn readingOf(th: *const Theme) [3]u8 { return best; } -pub const initial_chrome = ChromeTheme.fromTheme(&themes[0]); - fn nextThemeFileGeneration(p: *Pardes) u32 { p.theme_file_generation +%= 1; if (p.theme_file_generation == 0) p.theme_file_generation = 1; @@ -1009,26 +1760,34 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8) bool { var arena: std.heap.ArenaAllocator = .init(p.gpa); defer arena.deinit(); var diagnostics: std.zon.parse.Diagnostics = undefined; - var parsed = std.zon.parse.fromSlice(Theme, .{ .gpa = p.gpa, .arena = arena.allocator(), .source = source, .diagnostics = &diagnostics }) catch |err| { - if (err != error.ParseZon) themeFileError(p, request, err) else { - // Which line, and what the parser wanted there. - var why: [200]u8 = undefined; - themeFileFailure(p, request, pardes.Messages.zonWhy(&diagnostics, &why)); - } - return false; - }; - if (parsed.name.len == 0 or parsed.name.len > 255) { + const options: std.zon.parse.Options = .{ .gpa = p.gpa, .arena = arena.allocator(), .source = source, .diagnostics = &diagnostics }; + // A file in the old names (every one written before 2026-11-25: a + // user's own, an older DumpThemes') is read in them and renamed; its + // old names' view is the file itself. + var theme: Theme = undefined; + var view: LegacyTheme = undefined; + if (inOldNames(source)) { + view = std.zon.parse.fromSlice(LegacyTheme, options) catch |err| return zonFailed(p, request, err, &diagnostics); + theme = .fromLegacy(view); + } else { + theme = std.zon.parse.fromSlice(Theme, options) catch |err| return zonFailed(p, request, err, &diagnostics); + const r = Chrome.of(&theme); + view = theme.toLegacy(.{ .sel_fg = r.page.selection.text, .lineno = r.line_number, .scroll_track = r.rail, .scroll_thumb = r.rail_thumb, .box_dim = r.grip.ring }); + } + if (theme.name.len == 0 or theme.name.len > 255) { themeFileError(p, request, error.InvalidThemeName); return false; } - parsed.name = p.gpa.dupe(u8, parsed.name) catch |err| { + view.name = p.gpa.dupe(u8, theme.name) catch |err| { themeFileError(p, request, err); return false; }; + theme.name = view.name; - p.chrome = ChromeTheme.fromTheme(&parsed); + p.theme_chrome = .of(&theme, &view); + p.body_chrome = .{ .theme = view, .chrome = p.theme_chrome }; const old = p.custom_theme; - p.custom_theme = parsed; + p.custom_theme = view; if (old) |theme_value| p.gpa.free(theme_value.name); _ = p.theme_file_loaded.set(request.path); _ = p.scratch.reset(.{ .retain_with_limit = pardes.memory.limits.scratch_kept }); @@ -1036,10 +1795,40 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8) bool { return true; } +/// A theme file the parser refused: said with its line and what the parser +/// wanted there, never a bare `parse zon`. +fn zonFailed(p: *Pardes, request: ThemeFileRequest, err: anyerror, diagnostics: *const std.zon.parse.Diagnostics) bool { + if (err != error.ParseZon) themeFileError(p, request, err) else { + var why: [200]u8 = undefined; + themeFileFailure(p, request, pardes.Messages.zonWhy(diagnostics, &why)); + } + return false; +} + +/// Whether a theme file is in the old names (`LegacyTheme`): every one +/// names `tag_bg`, which the new names have not. +fn inOldNames(source: [:0]const u8) bool { + var tokens: std.zig.Tokenizer = .init(source); + var after_period = false; + while (true) { + const t = tokens.next(); + switch (t.tag) { + .eof => return false, + .identifier => if (after_period and std.mem.eql(u8, source[t.loc.start..t.loc.end], "tag_bg")) return true, + else => {}, + } + after_period = t.tag == .period; + } +} + +/// The session's theme's chrome, at its start (Pardes.init, a Restore): +/// never one the window previews (`previewTheme`); the body's copy of it +/// (Core.bodyChrome's, keyed by the theme) made with it, not again at the +/// first frame. pub fn finishThemeInitialization(p: *Pardes) void { - // The session's own theme's: never one the window previews - // (`previewTheme`). - p.chrome = ChromeTheme.fromTheme(if (p.custom_theme) |*custom| custom else &themes[p.settings.theme]); + const theme = if (p.custom_theme) |*custom| custom else &legacy_themes[p.settings.theme]; + p.theme_chrome = if (p.custom_theme != null) .fromTheme(theme) else .of(&themes[p.settings.theme], theme); + p.body_chrome = .{ .theme = theme.*, .chrome = p.theme_chrome }; } /// Themes near `name`, a line of them, for a Theme given a name there is @@ -1119,14 +1908,16 @@ pub fn previewTheme(p: *Pardes, index: ?usize) void { if (i >= themes.len) return; if (p.theme_preview) |had| if (had == i) return; p.theme_preview = @intCast(i); - p.preview_chrome = ChromeTheme.fromTheme(&themes[i]); + p.preview_chrome = .of(&themes[i], &legacy_themes[i]); + p.body_chrome = .{ .theme = legacy_themes[i], .chrome = p.preview_chrome }; } pub fn setThemeIndex(p: *Pardes, index: usize) void { if (index >= themes.len or (p.custom_theme == null and p.theme_file_path.get().len == 0 and index == @as(usize, p.settings.theme))) return; - p.chrome = ChromeTheme.fromTheme(&themes[index]); + p.theme_chrome = .of(&themes[index], &legacy_themes[index]); + p.body_chrome = .{ .theme = legacy_themes[index], .chrome = p.theme_chrome }; if (p.custom_theme) |theme_value| { p.custom_theme = null; p.gpa.free(theme_value.name); @@ -1141,7 +1932,7 @@ pub fn setThemeIndex(p: *Pardes, index: usize) void { } test "every native theme's separators stand off its page, and its focus tint off its tag" { - for (themes[0..native_count]) |*theme| { + for (legacy_themes[0..native_count]) |*theme| { const chrome = ChromeTheme.fromTheme(theme); const page = theme.bg orelse null_page; try std.testing.expect(contrast(chrome.border, page) >= separator_floor); @@ -1160,7 +1951,7 @@ test "every native theme's separators stand off its page, and its focus tint off test "every faithful port meets pardes's chrome floors in its own colours, with nothing left to derive" { const first = comptime indexOfTheme("lapis") + 1; const last = comptime indexOfTheme("helix"); - for (themes[first..last]) |*th| { + for (legacy_themes[first..last]) |*th| { const chrome = ChromeTheme.fromTheme(th); const page = th.bg.?; // Written out already adjusted: the run-time lift and push leave @@ -1181,8 +1972,8 @@ test "every faithful port meets pardes's chrome floors in its own colours, with } test "a derived separator or tint keeps the theme's own when it already stands off" { - const forge = &themes[ - for (themes[0..native_count], 0..) |t, i| { + const forge = &legacy_themes[ + for (legacy_themes[0..native_count], 0..) |t, i| { if (std.mem.eql(u8, t.name, "forge")) break i; } else unreachable ]; @@ -1228,9 +2019,9 @@ test "seoul256's twelve backgrounds ring in their order, after Catppuccin and be "seoul256_light_253", "seoul256_light_254", "seoul256_light_255", "seoul256_light_256", }; const first = indexOfTheme("seoul256_dark_233"); - for (names, themes[first..][0..names.len]) |name, t| try std.testing.expectEqualStrings(name, t.name); - try std.testing.expectEqualStrings("catppuccin_mocha", themes[first - 1].name); - try std.testing.expectEqualStrings("zenbones_light", themes[first + names.len].name); + for (names, legacy_themes[first..][0..names.len]) |name, t| try std.testing.expectEqualStrings(name, t.name); + try std.testing.expectEqualStrings("catppuccin_mocha", legacy_themes[first - 1].name); + try std.testing.expectEqualStrings("zenbones_light", legacy_themes[first + names.len].name); } test "a theme hint drops whole names to fit, never cutting one" { @@ -1298,8 +2089,8 @@ test "ThemeFile parses ZON atomically and Theme returns to the compiled ring" { var custom = themes[0]; custom.name = "mine"; - custom.bg = .{ 1, 2, 3 }; - custom.tag_bg = .{ 4, 5, 6 }; + custom.page = .{ 1, 2, 3 }; + custom.tag = .{ 4, 5, 6 }; var encoded: std.Io.Writer.Allocating = .init(std.testing.allocator); defer encoded.deinit(); try std.zon.stringify.serialize(custom, .{ .whitespace = true }, &encoded.writer); @@ -1317,7 +2108,7 @@ test "ThemeFile parses ZON atomically and Theme returns to the compiled ring" { try std.testing.expectEqual(@as(?[3]u8, .{ 1, 2, 3 }), p.theme().bg); setThemeIndex(p, 0); - try std.testing.expectEqualStrings(themes[0].name, p.theme().name); + try std.testing.expectEqualStrings(legacy_themes[0].name, p.theme().name); try std.testing.expect(themeFileRequest(p, request_effect.generation) == null); const stop = while (p.nextEffect()) |effect| switch (effect) { .theme_file => |theme_file| break theme_file, @@ -1329,7 +2120,7 @@ test "ThemeFile parses ZON atomically and Theme returns to the compiled ring" { test "native themes keep text and UI roles readable on their own surfaces" { const names = [_][]const u8{ "orchard", "dusk", "ink", "paper", "daybreak", "atelier", "forge", "lagoon", "solarium", "spectrum", "harvest", "clay", "forge_black", "forge_soft", "orchard_black" }; try std.testing.expectEqual(names.len, native_theme_count); - for (themes[0..names.len], names) |th, name| { + for (legacy_themes[0..names.len], names) |th, name| { try std.testing.expectEqualStrings(name, th.name); const chrome = ChromeTheme.fromTheme(&th); const minimum: f32 = if (std.mem.eql(u8, name, "ink") or std.mem.eql(u8, name, "daybreak")) 7 else 4.5; @@ -1364,7 +2155,7 @@ test "native themes keep text and UI roles readable on their own surfaces" { } test "native filename tints change hue without changing perceived lightness" { - for (themes[0..native_theme_count]) |th| { + for (legacy_themes[0..native_theme_count]) |th| { try std.testing.expect(th.tag_name_fg != null and th.tag_active_name_fg != null); const chrome = ChromeTheme.fromTheme(&th); const pairs = [2][2][3]u8{ @@ -1389,7 +2180,7 @@ test "native filename tints change hue without changing perceived lightness" { } test "native dark palettes choose near-black or deliberately soft contrast" { - for (themes[0..native_theme_count]) |th| { + for (legacy_themes[0..native_theme_count]) |th| { if (contrast(th.bg.?, .{ 0, 0, 0 }) > 10) continue; // light paper const soft = std.mem.eql(u8, th.name, "dusk") or std.mem.eql(u8, th.name, "lagoon") or std.mem.eql(u8, th.name, "solarium") or std.mem.eql(u8, th.name, "forge_soft"); @@ -1417,7 +2208,7 @@ test "line numbers stay quiet without a current-line tag background" { const pane = try p.setTestFile("first\nsecond\n"); pane.body.cur_pinned = true; pane.body.cur_row = 0; - var custom = themes[0]; + var custom = legacy_themes[0]; custom.name = try gpa.dupe(u8, "quiet-gutter"); custom.lineno = .{ 70, 70, 70 }; custom.lineno_active = .{ 95, 95, 95 }; @@ -1469,7 +2260,7 @@ test "legacy ThemeFile documents inherit Pardes UI roles without new fields" { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); var diagnostics: std.zon.parse.Diagnostics = undefined; - const parsed = try std.zon.parse.fromSlice(Theme, .{ .gpa = std.testing.allocator, .arena = arena.allocator(), .source = source, .diagnostics = &diagnostics }); + const parsed = try std.zon.parse.fromSlice(LegacyTheme, .{ .gpa = std.testing.allocator, .arena = arena.allocator(), .source = source, .diagnostics = &diagnostics }); try std.testing.expectEqual(@as(?[3]u8, null), parsed.tag_active_bg); try std.testing.expectEqual(@as(?[3]u8, null), parsed.tag_name_fg); try std.testing.expectEqual(@as(?[3]u8, null), parsed.tag_active_name_fg); @@ -1488,7 +2279,7 @@ test "legacy ThemeFile documents inherit Pardes UI roles without new fields" { } test "column grip colors remain distinct from pane grips across native themes" { - for (themes[0..native_theme_count]) |th| { + for (legacy_themes[0..native_theme_count]) |th| { try std.testing.expect(th.column_box != null and th.column_box_dim != null); const chrome = ChromeTheme.fromTheme(&th); for ([_][2][3]u8{ @@ -1503,7 +2294,7 @@ test "column grip colors remain distinct from pane grips across native themes" { try std.testing.expect(distance >= 30 * 30); } } - var custom = themes[0]; + var custom = legacy_themes[0]; custom.column_box = .{ 90, 130, 170 }; custom.column_box_dim = null; var chrome = ChromeTheme.fromTheme(&custom); @@ -1515,13 +2306,13 @@ test "column grip colors remain distinct from pane grips across native themes" { } test "custom filename tints preserve optional active fallback precedence" { - var theme = themes[0]; + var theme = legacy_themes[0]; theme.tag_name_fg = null; theme.tag_active_name_fg = null; var chrome = ChromeTheme.fromTheme(&theme); // Neither named: each derived, on its own ground. - try std.testing.expectEqual(derivedNameTint(&theme, theme.tag_fg, theme.tag_bg).?, chrome.tag_name_fg); - try std.testing.expectEqual(derivedNameTint(&theme, theme.tag_active_fg.?, chrome.tag_active_bg).?, chrome.tag_active_name_fg); + try std.testing.expectEqual(derivedNameTint(&.fromLegacy(theme), theme.tag_fg, theme.tag_bg).?, chrome.tag_name_fg); + try std.testing.expectEqual(derivedNameTint(&.fromLegacy(theme), theme.tag_active_fg.?, chrome.tag_active_bg).?, chrome.tag_active_name_fg); theme.tag_name_fg = .{ 120, 130, 140 }; chrome = ChromeTheme.fromTheme(&theme); try std.testing.expectEqual(theme.tag_name_fg.?, chrome.tag_name_fg); @@ -1532,12 +2323,12 @@ test "custom filename tints preserve optional active fallback precedence" { try std.testing.expectEqual(theme.tag_active_name_fg.?, chrome.tag_active_name_fg); theme.tag_name_fg = null; chrome = ChromeTheme.fromTheme(&theme); - try std.testing.expectEqual(derivedNameTint(&theme, theme.tag_fg, theme.tag_bg).?, chrome.tag_name_fg); + try std.testing.expectEqual(derivedNameTint(&.fromLegacy(theme), theme.tag_fg, theme.tag_bg).?, chrome.tag_name_fg); try std.testing.expectEqual(theme.tag_active_name_fg.?, chrome.tag_active_name_fg); } test "every theme tints its names off the tag's ink, and they read on both grounds" { - for (&themes) |*th| { + for (&legacy_themes) |*th| { const chrome = ChromeTheme.fromTheme(th); const pairs = [2][3][3]u8{ .{ chrome.tag_fg, chrome.tag_name_fg, chrome.tag_bg }, @@ -1572,7 +2363,7 @@ test "every theme's walk UI roles meet their floors on every ground they are dra var checks: std.ArrayList(Check) = .empty; defer checks.deinit(a); var failed: usize = 0; - for (&themes) |*th| { + for (&legacy_themes) |*th| { checks.clearRetainingCapacity(); const chrome = ChromeTheme.fromTheme(th); const r = &chrome.roles; @@ -1580,7 +2371,7 @@ test "every theme's walk UI roles meet their floors on every ground they are dra const body_ink = th.fg orelse th.tag_fg; const tags = [_][3]u8{ r.tag[0].ground, r.tag[1].ground, r.tag[2].ground, r.selected[0].ground, r.selected[1].ground, r.selected[2].ground, r.selected[3].ground }; const messages = [_][3]u8{ r.tag[0].ground, r.tag[1].ground, r.tag[2].ground, page }; - for (r.tag ++ r.selected ++ [1]Inks{r.notice}) |inks| { + for (r.tag ++ r.selected ++ [1]RoleInks{r.notice}) |inks| { const text = contrast(inks.text, inks.ground); try checks.append(a, .{ .what = "text", .got = text, .floor = @min(Roles.text_floor, Reach.of(&.{inks.ground})) }); try checks.append(a, .{ .what = "dim", .got = contrast(inks.dim, inks.ground), .floor = @min(Roles.dim_floor, text) }); @@ -1621,39 +2412,388 @@ test "every theme's walk UI roles meet their floors on every ground they are dra try std.testing.expectEqual(0, failed); } -test "neonotte84's and acme's roles are the ones UX derived (DESIGN.md D4, tools/roles.py)" { - const Want = struct { name: []const u8, text: ?[3]u8, dim: [3]u8, rule: [3]u8, key: [3]u8, match: [3]u8, reading: [3]u8, hit: [3]u8, hit_current: [3]u8 }; - // The hit grounds past UX's 18 % / 32 % mix, to 1.5:1 and 1.9:1 off the - // page (UX E7 7b). +test "neonotte84's and acme's inks on the plain tag, their reading mark and their hits are B4's (DESIGN.md B4, D4's goldens re-pinned where B4 moves them)" { + const Want = struct { name: []const u8, text: [3]u8, dim: [3]u8, rule: [3]u8, key: [3]u8, match: [3]u8, mode: [3]u8, hit: [3]u8, hit_current: [3]u8 }; const wants = [_]Want{ - .{ .name = "neonotte84", .text = .{ 0xb5, 0xbf, 0xe3 }, .dim = .{ 0x70, 0x76, 0x8f }, .rule = .{ 0x4c, 0x50, 0x63 }, .key = .{ 0xf8, 0xde, 0xd9 }, .match = .{ 0xf1, 0xa2, 0x71 }, .reading = .{ 0x29, 0x27, 0x1e }, .hit = .{ 0x47, 0x32, 0x2e }, .hit_current = .{ 0x5f, 0x3f, 0x33 } }, - // acme's key and match a little darker than UX's #5c5c8d and - // #974d00 (which hold on its tag and its #9eeeee): a reading pane's - // band's selection is pushed off the reading ground (#9eeeee is - // 1.12:1 off #f3f0cc), and they read on that one too. - .{ .name = "acme", .text = null, .dim = .{ 0x7a, 0x86, 0x86 }, .rule = .{ 0xa8, 0xb7, 0xb7 }, .key = .{ 0x55, 0x55, 0x83 }, .match = .{ 0x8f, 0x48, 0x00 }, .reading = .{ 0xf3, 0xf0, 0xcc }, .hit = .{ 0xe6, 0xcd, 0xae }, .hit_current = .{ 0xd9, 0xb4, 0x90 } }, + // Text, dim, rule and key as D4 derived them; the match the hits' + // amber held on this ground alone (D4's pushed neonotte84's number + // over all seven grounds at once: #f1a271); the reading mark sky + // on the grip, 3:1 on both tags (D4 tinted the whole tag #29271e); + // the hit an amber ground 1.5:1 off the page (D4's mixed the + // number in: #47322e) and the current one that amber itself, solid + // (D4's a 1.9:1 ground, #5f3f33). + .{ .name = "neonotte84", .text = .{ 0xb5, 0xbf, 0xe3 }, .dim = .{ 0x70, 0x76, 0x8f }, .rule = .{ 0x4c, 0x50, 0x63 }, .key = .{ 0xf8, 0xde, 0xd9 }, .match = .{ 0xe2, 0x9e, 0x47 }, .mode = .{ 0x2b, 0x92, 0xbc }, .hit = .{ 0x4c, 0x30, 0x1a }, .hit_current = .{ 0xe2, 0x9e, 0x47 } }, + // acme's key and match on its plain tag alone, never held to its + // #9eeeee selection and its reading ground at once (D4's #555583, + // #8f4800): its #8888cc darkened to 4.5:1, its number as it is; the + // hits in its number's hue (its selection sits on the warning's): + // a ground 1.5:1 off the page (D4's #e6cdae), the current one the + // number itself (D4's #d9b490); the reading mark sky on the grip + // (its #f3f0cc reading tag goes with the mode ground, G1'). + .{ .name = "acme", .text = .{ 0, 0, 0 }, .dim = .{ 0x7a, 0x86, 0x86 }, .rule = .{ 0xa8, 0xb7, 0xb7 }, .key = .{ 0x6e, 0x6d, 0xaf }, .match = .{ 0xa0, 0x52, 0x00 }, .mode = .{ 0x1e, 0x8a, 0xb3 }, .hit = .{ 0xff, 0xc7, 0x90 }, .hit_current = .{ 0xa0, 0x52, 0x00 } }, }; for (wants) |want| { - const chrome = ChromeTheme.fromTheme(&themes[themeIndex(want.name).?]); - const plain = chrome.roles.on(.plain); - const got = [_][3]u8{ plain.text, plain.dim, plain.rule, chrome.roles.key, chrome.roles.match, chrome.tag_reading_bg, chrome.roles.hit, chrome.roles.hit_current }; - const expected = [_][3]u8{ want.text orelse plain.text, want.dim, want.rule, want.key, want.match, want.reading, want.hit, want.hit_current }; + const c = Chrome.of(&themes[themeIndex(want.name).?]); + const plain = c.on(.plain); + const got = [_][3]u8{ plain.text, plain.dim, plain.rule, plain.key, plain.match, c.grip.mode, c.hit, c.hit_current }; + const expected = [_][3]u8{ want.text, want.dim, want.rule, want.key, want.match, want.mode, want.hit, want.hit_current }; for (got, expected, 0..) |g, e, i| for (g, e) |x, y| if (@abs(@as(i16, x) - y) > 2) { - std.debug.print("{s} role {d}: {x} for {x}\n", .{ want.name, i, g, e }); + std.debug.print("{s} ink {d}: {x} for {x}\n", .{ want.name, i, g, e }); return error.TestUnexpectedResult; }; } } -test "the err role falls back from diagnostic.error's ink to error's, then to the theme's red (UX E3): neonotte84's (its diagnostic.error an underline only) is its red, acme's its palette's" { +test "the error ink is a theme's own, else its red (UX E3): neonotte84's (its diagnostic.error an underline only) its red, acme's its palette's, on the page and every tag ground" { const neonotte = &themes[themeIndex("neonotte84").?]; // neonotte.toml: "error" = { fg = "red" }, its palette's red #ff7ea2. - try std.testing.expectEqual([3]u8{ 0xff, 0x7e, 0xa2 }, neonotte.diagnostic_error.?); + try std.testing.expectEqual([3]u8{ 0xff, 0x7e, 0xa2 }, neonotte.@"error".?); for ([_][]const u8{ "neonotte84", "acme" }) |name| { - const chrome = ChromeTheme.fromTheme(&themes[themeIndex(name).?]); - const err = chrome.roles.err; - // Red: its red channel well over the others, on the page and the tags. - try std.testing.expect(err[0] > err[1] + 40 and err[0] > err[2] + 40); + const c = Chrome.of(&themes[themeIndex(name).?]); + for ([1]Chrome.Inks{c.page} ++ c.tag) |inks| { + const err = inks.severity[@backingInt(Chrome.Severity.@"error")]; + try std.testing.expect(err[0] > err[1] + 40 and err[0] > err[2] + 40); + } + } + try std.testing.expectEqual([3]u8{ 0xaa, 0x22, 0x22 }, Chrome.of(&themes[themeIndex("acme").?]).page.severity[0]); +} + +/// What the ring is held to (DESIGN.md A8, B4's floors): a state pardes +/// draws that must read (each check not `pro`), or the professional floor +/// (`pro`: natives and `dark` are held to it too). The `original` checks +/// are a theme's own colours: a port's or an import's are reported, never +/// held against it, except the sixteen where pardes derives them. +const RingCheck = enum { + text, + text7, + syntax, + syntax45, + comment, + comment45, + line_number, + selection, + selection_pro, + hit, + hit_current, + selection_hit, + tag_text, + focus, + focus_text, + tone, + grip_mode, + grip, + grip_pro, + grip_ring, + rule, + status_text, + words, + loudest, + band, + column_grip, + tag_text7, + severities, + severities4, + error_red, + diff, + ansi, + ansi_pro, + inks, + + fn pro(c: RingCheck) bool { + return switch (c) { + .text7, .syntax45, .comment45, .line_number, .selection_pro, .selection_hit, .tone, .grip_pro, .grip_ring, .severities4, .error_red, .ansi_pro, .tag_text7 => true, + else => false, + }; + } + + fn original(c: RingCheck) bool { + return switch (c) { + .text, .text7, .syntax, .syntax45, .comment, .comment45, .line_number, .ansi, .ansi_pro => true, + else => false, + }; + } +}; + +/// The checks `th`'s chrome fails. +fn ringFailures(th: *const Theme, c: *const Chrome) std.EnumSet(RingCheck) { + var f: std.EnumSet(RingCheck) = .empty; + const de = struct { + fn of(a: [3]u8, b: [3]u8) f32 { + return oklabDistance(toOklab(a), toOklab(b)); + } + }.of; + const page = c.page.ground; + const ink = c.page.text; + const dark = isDark(page); + const text = contrast(ink, page); + f.setPresent(.text, text < 4.5); + f.setPresent(.text7, text < 7); + const syntax = @min(contrast(th.keyword, page), contrast(th.string, page), contrast(th.number, page)); + f.setPresent(.syntax, syntax < 3); + f.setPresent(.syntax45, syntax < 4.5); + f.setPresent(.comment, contrast(th.comment, page) < 3); + f.setPresent(.comment45, contrast(th.comment, page) < 4.5); + const line = contrast(c.line_number, page); + f.setPresent(.line_number, line < 2 or line > 4.5); + const sel = c.page.selection; + f.setPresent(.selection, contrast(sel.ground, page) < 1.2 or contrast(sel.text, sel.ground) < 4.5); + f.setPresent(.selection_pro, contrast(sel.ground, page) < (if (dark) Chrome.selection_dark else Chrome.selection_light) - 0.005); + const hit_off = contrast(c.hit, page); + f.setPresent(.hit, !((hit_off >= 1.49 or (hit_off >= 1.1 and de(c.hit, page) >= 0.08)) and contrast(ink, c.hit) >= @min(4.5, text * 0.9) - 0.05)); + f.setPresent(.hit_current, !((contrast(c.hit_current, c.hit) >= 1.12 or de(c.hit_current, c.hit) >= 0.05) and contrast(c.hit_current_text, c.hit_current) >= 4.49)); + f.setPresent(.selection_hit, de(sel.ground, c.hit) < 0.05); + const tag = c.on(.plain); + const focus = c.on(.focus); + const focus_floor = if (isDark(tag.ground)) Chrome.focus_dark else Chrome.focus_light; + f.setPresent(.tag_text, contrast(tag.text, tag.ground) < 4.49); + f.setPresent(.focus, contrast(focus.ground, tag.ground) < focus_floor - 0.01); + f.setPresent(.focus_text, contrast(focus.text, focus.ground) < 4.49); + f.setPresent(.tone, isDark(tag.ground) != dark or isDark(focus.ground) != dark); + // R5': the reading mark 3:1 on both tags, off the resting ring, and + // its hue told from the grip's others where a mode hue is. + var said: [3]f32 = undefined; + var n_said: usize = 0; + for ([3][3]u8{ c.grip.focus, c.page.severity[1], c.page.severity[0] }) |s| if (lch(s)[1] >= 0.04) { + said[n_said] = lch(s)[2]; + n_said += 1; + }; + const apart = struct { + fn of(h: f32, taken: []const f32) f32 { + var least: f32 = 180; + for (taken) |t| least = @min(least, hueDistance(h, t)); + return least; + } + }.of; + const room = for (mode_hues) |h| { + if (apart(h, said[0..n_said]) >= 40) break true; + } else false; + f.setPresent(.grip_mode, @min(contrast(c.grip.mode, tag.ground), contrast(c.grip.mode, focus.ground)) < 2.99 or + de(c.grip.mode, c.grip.ring) < 0.10 or (room and apart(lch(c.grip.mode)[2], said[0..n_said]) < 40 - 1)); + const grip = contrast(c.grip.focus, focus.ground); + f.setPresent(.grip, grip < 2 and de(c.grip.focus, focus.ground) < 0.2); + f.setPresent(.grip_pro, grip < 2.99); + f.setPresent(.grip_ring, contrast(c.grip.ring, tag.ground) < 1.5); + f.setPresent(.rule, contrast(c.rule, page) < 1.49); + f.setPresent(.status_text, contrast(c.status.text, c.status.ground) < 4.49); + // H: the words at 4.5:1 on both tags and the page (or the full ink, + // where it has no more); the full ink up to 7:1; the focused band the + // loudest; the plain tag a band off the page, or rules of 3:1; the + // column grip 2:1 at rest, 3:1 grabbed. + for ([3]*const Chrome.Inks{ tag, focus, &c.page }) |g| { + if (contrast(g.word, g.ground) < @min(4.49, contrast(g.text, g.ground) - 0.01)) f.insert(.words); + } + f.setPresent(.loudest, contrast(focus.ground, page) <= contrast(tag.ground, page)); + f.setPresent(.band, contrast(tag.ground, page) < Chrome.band_floor - 0.005 and contrast(c.rule, page) < 3); + const rest = contrast(c.grip.column_rest, page); + const grabbed = contrast(c.grip.column, page); + f.setPresent(.column_grip, rest < 1.99 or grabbed < 2.99 or grabbed <= rest); + f.setPresent(.tag_text7, @min(contrast(tag.text, tag.ground), contrast(focus.text, focus.ground)) < 6.99); + var apart3: f32 = 2; + var apart4: f32 = 2; + for (c.page.severity, 0..) |a, i| { + for (c.page.severity[i + 1 ..], i + 1..) |b, j| { + if (j < 3) apart3 = @min(apart3, de(a, b)); + apart4 = @min(apart4, de(a, b)); + } + if (i < 3) apart3 = @min(apart3, de(a, ink)); + apart4 = @min(apart4, de(a, ink)); + } + f.setPresent(.severities, apart3 < Chrome.severity_apart); + f.setPresent(.severities4, apart4 < Chrome.severity_apart); + const err = lch(c.page.severity[0]); + f.setPresent(.error_red, !(err[1] >= 0.08 and (err[2] <= 50 or err[2] >= 330))); + f.setPresent(.diff, @min(contrast(c.add.row, page), contrast(c.del.row, page)) < 1.1 or de(c.add.row, c.del.row) < 0.05); + var ansi_least: f32 = 21; + for (c.ansi[1..], 1..) |a, i| { + ansi_least = @min(ansi_least, contrast(a, page)); + if (contrast(a, page) < @as(f32, if (i == 8) 2 else 3)) f.insert(.ansi); + } + f.setPresent(.ansi_pro, ansi_least < 4.5); + // Every ink on every ground it is drawn on (B1.3): its floor, or the + // most any ink reaches there. + const reach = struct { + fn of(g: [3]u8) f32 { + return @max(contrast(@splat(255), g), contrast(@splat(0), g)) - 0.01; + } + }.of; + for ([1]Chrome.Inks{c.page} ++ c.tag ++ [1]Chrome.Inks{c.status}, 0..) |g, n| { + const t = contrast(g.text, g.ground); + var low = (n > 0 and t < @min(4.5, reach(g.ground))) or contrast(g.dim, g.ground) < @min(3, t) - 0.01; + for ([2][3]u8{ g.key, g.match } ++ g.severity) |i| low = low or contrast(i, g.ground) < @min(4.5, reach(g.ground)); + low = low or contrast(g.selection.text, g.selection.ground) < @min(4.5, reach(g.selection.ground)); + if (low) f.insert(.inks); + } + return f; +} + +/// The themes that fail a check they are held to until the line that fixes +/// them lands (DESIGN.md E1, F): the list shrinks as each does, and a theme +/// on it that passes fails the test, so it is taken off. +const ring_expected = [_]struct { []const u8, []const u8 }{ + // L3 (core/themes): the overrides DESIGN.md E1 names. + .{ "acme", "L3: its own sixteen where pardes's picks read under 3:1 (ANSI 10, 14)" }, + .{ "solarized_dark", "L3: `hit` and `added`/`deleted` of its own" }, + .{ "solarized_light", "L3: `hit` of its own" }, + .{ "seoul256_light_254", "L3: `hit` of its own" }, + .{ "seoul256_light_255", "L3: `hit` of its own" }, + // L4 (tools/gen_themes.zig): B7's mapping, or an import's override. + .{ "everforest_light", "L4: `hit` of its own" }, + .{ "github_dark_dimmed", "L4: its text from helix's `variable`" }, + .{ "gruvbox_material_light_hard", "L4: `info`/`hint` from its own scopes" }, + .{ "gruvbox_material_light_medium", "L4: `info`/`hint` from its own scopes" }, + .{ "gruvbox_material_light_soft", "L4: `info`/`hint` from its own scopes" }, + .{ "hex_toxic", "L4: `hit` and `added`/`deleted` of its own" }, + // L5: dark's own sixteen (its derived ANSI 8 is 3:1, under the pro 4.5). + .{ "dark", "L5: its designed ANSI (DESIGN.md C)" }, + // L7: retired. + .{ "seoul256_dark_238", "L7: retired" }, + .{ "seoul256_dark_239", "L7: retired" }, + .{ "seoul256_light_252", "L7: retired" }, + .{ "seoul256_light_253", "L7: retired" }, + .{ "hex_lavender", "L7: retired" }, + .{ "hex_poison", "L7: retired" }, + .{ "hex_steel", "L7: retired" }, + .{ "ingrid", "L7: retired" }, + // No line: the soft natives' text is 4.5-7:1 by design (the native + // test below), under the professional 7:1. C1's ruling. + .{ "dusk", "soft by design: text under the pro 7:1" }, + .{ "lagoon", "soft by design: text under the pro 7:1" }, + .{ "solarium", "soft by design: text under the pro 7:1" }, + .{ "forge_soft", "soft by design: text under the pro 7:1" }, +}; + +test "every theme in the ring meets every floor pardes owns (DESIGN.md B4), natives and dark the professional ones too; the original's own colours never moved" { + const lapis = comptime indexOfTheme("lapis"); + const dark = comptime indexOfTheme("dark"); + const helix = comptime indexOfTheme("helix"); + var passing: usize = 0; + var wrong: usize = 0; + for (&themes, 0..) |*th, i| { + const c = Chrome.of(th); + // The original's own (B1.5): never moved. + for ([4][3]u8{ th.keyword, th.string, th.number, th.comment }, c.syntax[1..]) |own, got| try std.testing.expectEqual(own, got); + if (th.text) |own| try std.testing.expectEqual(own, c.page.text); + if (th.page) |own| try std.testing.expectEqual(own, c.page.ground); + if (th.ansi) |own| try std.testing.expectEqual(own, c.ansi); + if (th.line_number) |own| try std.testing.expectEqual(own, c.line_number); + const ours = i <= lapis or i == dark or i == helix; + const failures = ringFailures(th, &c); + var held: std.EnumSet(RingCheck) = .empty; + var it = failures.iterator(); + while (it.next()) |check| { + if (check.pro() and !(i <= lapis or i == dark)) continue; + if (check.original() and !ours and !(std.mem.startsWith(u8, @tagName(check), "ansi") and th.ansi == null)) continue; + held.insert(check); + } + const expected = for (ring_expected) |e| { + if (std.mem.eql(u8, e[0], th.name)) break true; + } else false; + if (held.count() == 0) passing += 1; + if (held.count() > 0 and !expected) { + wrong += 1; + std.debug.print("{s} fails:", .{th.name}); + var failed = held.iterator(); + while (failed.next()) |check| std.debug.print(" {s}", .{@tagName(check)}); + std.debug.print("\n", .{}); + } + if (held.count() == 0 and expected) { + wrong += 1; + std.debug.print("{s} passes now: take it off ring_expected\n", .{th.name}); + } + } + std.debug.print("ring floors: {d} of {d} themes pass; {d} on the expected list\n", .{ passing, themes.len, ring_expected.len }); + try std.testing.expectEqual(0, wrong); +} + +test "the whole ring resolves in 150 ms of CPU, no theme in over 0.5 ms (Debug)" { + const io = std.testing.io; + var total: i96 = 0; + var worst: i96 = 0; + var worst_at: usize = 0; + for (&themes, 0..) |*th, i| { + // The least of five: a theme's own cost, not a page fault's. + var least: i96 = std.math.maxInt(i96); + for (0..5) |_| { + const start = std.Io.Clock.cpu_thread.now(io); + var c = Chrome.of(th); + std.mem.doNotOptimizeAway(&c); + least = @min(least, start.durationTo(std.Io.Clock.cpu_thread.now(io)).toNanoseconds()); + } + total += least; + if (least > worst) { + worst = least; + worst_at = i; + } + } + std.debug.print("ring resolved in {d} us, the slowest {s} in {d} us\n", .{ @divTrunc(total, 1000), themes[worst_at].name, @divTrunc(worst, 1000) }); + try std.testing.expect(total < 150 * std.time.ns_per_ms); + try std.testing.expect(worst <= std.time.ns_per_ms / 2); +} + +test "a theme file in the old names, a user's own or an older DumpThemes', and DumpThemes' now in the new names, load to the same Chrome" { + @setEvalBranchQuota(20000); + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .config_dir = "/tmp/pardes-config" }); + defer p.deinit(); + // ~/.config/pardes/themes/one_dark_zed_mod.zon's shape: the seventeen + // fields the old names required, no others. + const own = + \\.{ + \\ .name = "one_dark_zed", + \\ .bg = .{ 0, 0, 0 }, + \\ .fg = .{ 172, 178, 190 }, + \\ .sel_bg = .{ 69, 74, 86 }, + \\ .sel_fg = .{ 172, 178, 190 }, + \\ .tag_bg = .{ 59, 65, 77 }, + \\ .tag_fg = .{ 169, 175, 188 }, + \\ .box = .{ 116, 173, 232 }, + \\ .box_dim = .{ 70, 75, 87 }, + \\ .kw = .{ 180, 119, 207 }, + \\ .str = .{ 161, 193, 129 }, + \\ .num = .{ 191, 149, 106 }, + \\ .comment = .{ 93, 99, 111 }, + \\ .lineno = .{ 78, 90, 95 }, + \\ .scroll_track = .{ 54, 60, 70 }, + \\ .scroll_thumb = .{ 200, 204, 212 }, + \\ .palette = .{ + \\ .{ 40, 44, 52 }, .{ 224, 108, 117 }, .{ 152, 195, 121 }, .{ 229, 192, 123 }, + \\ .{ 97, 175, 239 }, .{ 198, 120, 221 }, .{ 86, 182, 194 }, .{ 171, 178, 191 }, + \\ .{ 99, 109, 131 }, .{ 234, 133, 139 }, .{ 170, 213, 129 }, .{ 255, 216, 133 }, + \\ .{ 133, 193, 255 }, .{ 211, 152, 235 }, .{ 110, 213, 222 }, .{ 250, 250, 250 }, + \\ }, + \\} + \\ + ; + requestThemeFile(p, 0, "themes/one_dark_zed_mod.zon"); + try std.testing.expect(loadThemeFile(p, p.theme_file_generation, own)); + const want = p.chrome().*; + // The old names' view is the file itself, unset fields their defaults. + try std.testing.expectEqual([3]u8{ 70, 75, 87 }, p.theme().box_dim); + try std.testing.expectEqual(@as(?[3]u8, null), p.theme().tag_active_bg); + var old = p.theme().*; + old.name = "one_dark_zed"; + try std.testing.expectEqual(Chrome.of(&.fromLegacy(old)), want); + // An older DumpThemes' shape: every old field, the unset ones null. + var older: std.Io.Writer.Allocating = .init(gpa); + defer older.deinit(); + try std.zon.stringify.serialize(old, .{ .whitespace = true }, &older.writer); + try std.testing.expect(std.mem.indexOf(u8, older.written(), ".tag_active_bg = null") != null); + // DumpThemes' now: the new names. + var now: std.Io.Writer.Allocating = .init(gpa); + defer now.deinit(); + try std.zon.stringify.serialize(Theme.fromLegacy(old), .{ .whitespace = true }, &now.writer); + try std.testing.expect(std.mem.indexOf(u8, now.written(), ".tag_bg") == null and std.mem.indexOf(u8, now.written(), ".page = ") != null); + for ([2][]const u8{ older.written(), now.written() }) |file| { + requestThemeFile(p, 0, "themes/again.zon"); + try std.testing.expect(loadThemeFile(p, p.theme_file_generation, file)); + try std.testing.expectEqualStrings("one_dark_zed", p.theme().name); + try std.testing.expectEqual(want, p.chrome().*); + // What the old names' readers see of it is what they saw of the file. + for ([_][3]u8{ p.theme().sel_fg, p.theme().tag_bg, p.theme().box, p.theme().lineno, p.theme().scroll_track }, [_][3]u8{ old.sel_fg, old.tag_bg, old.box, old.lineno, old.scroll_track }) |got, was| + try std.testing.expectEqual(was, got); } - try std.testing.expectEqual([3]u8{ 0xaa, 0x22, 0x22 }, ChromeTheme.fromTheme(&themes[themeIndex("acme").?]).diagnostic_error); + // A file in the new names with a typo is refused in the new names' words. + requestThemeFile(p, 0, "themes/bad.zon"); + try std.testing.expect(!loadThemeFile(p, p.theme_file_generation, ".{ .name = \"x\", .page = null, .txet = null }")); + try std.testing.expectEqualStrings("one_dark_zed", p.theme().name); } diff --git a/core/jj.zig b/core/jj.zig index 696bd659..63488b8c 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -2255,7 +2255,7 @@ pub const Inks = struct { added: [3]u8, taken: [3]u8 }; /// The diff's own two colours: the theme's green and its red (the grounds' /// and the changed words' hues), each pushed toward the ink until it reads /// 3:1 on the page (or nearly as the ink does, where the ink reads less). -pub fn laneInks(th: *const pardes.Theme) Inks { +pub fn laneInks(th: *const pardes.LegacyTheme) Inks { var inks: [2][3]u8 = .{ if (th.palette) |c| c[2] else .{ 0x2e, 0xa0, 0x43 }, themeRed(th) }; const page = th.bg orelse th.tag_bg; for (&inks) |*ink| { @@ -2270,7 +2270,7 @@ pub fn laneInks(th: *const pardes.Theme) Inks { /// and red), in hue order from the top, each a hue `hue_apart` or more from /// the one kept before it (doom one: magenta, blue, yellow; cyan and /// orange go). Distinct colours, never one between two. -pub fn laneColours(th: *const pardes.Theme, buf: *[8][3]u8) []const [3]u8 { +pub fn laneColours(th: *const pardes.LegacyTheme, buf: *[8][3]u8) []const [3]u8 { const pal = th.palette; const grounds = [2][3]u8{ themeRed(th), if (pal) |c| c[2] else .{ 0x2e, 0xa0, 0x43 } }; const accents: [6][3]u8 = if (pal) |c| .{ c[3], c[4], c[5], c[6], th.num, th.kw } else .{ th.kw, th.num, th.str, th.box, th.column_box orelse th.box, th.comment }; @@ -2334,14 +2334,14 @@ pub fn paint(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u1 } /// The theme's red: what a change took away; churn's top. -pub fn themeRed(th: *const pardes.Theme) [3]u8 { +pub fn themeRed(th: *const pardes.LegacyTheme) [3]u8 { return if (th.palette) |c| c[1] else .{ 0xcf, 0x22, 0x2e }; } /// A count's colour on churn's gradient: smooth from the line numbers' /// grey to the theme's red, on a log scale of the count against the /// hottest; null for none. -fn heatColour(th: *const pardes.Theme, count: u16, hottest: u16) ?[3]u8 { +fn heatColour(th: *const pardes.LegacyTheme, count: u16, hottest: u16) ?[3]u8 { if (count == 0 or hottest == 0) return null; const t = @log(@as(f32, @floatFromInt(count)) + 1) / @log(@as(f32, @floatFromInt(hottest)) + 1); return pardes.colors.mixOklab(th.lineno, themeRed(th), t); @@ -2918,7 +2918,7 @@ test "Changes draws a lane a pinned change after the line number, nested ones a } test "a diff's two lane inks in every theme: its green and its red, never one colour, each read on the page" { - for (&pardes.colors.themes) |*th| { + for (&pardes.colors.legacy_themes) |*th| { const inks = laneInks(th); errdefer std.debug.print("{s}: added {any} taken {any}\n", .{ th.name, inks.added, inks.taken }); try testing.expect(!std.meta.eql(inks.added, inks.taken)); diff --git a/core/lib_syntax.zig b/core/lib_syntax.zig index 759720ec..fa6812ee 100644 --- a/core/lib_syntax.zig +++ b/core/lib_syntax.zig @@ -102,13 +102,13 @@ pub const NodeWalk = syntax.NodeWalk; pub const Span = syntax.Span; pub const walkNodes = syntax.walkNodes; -pub const Theme = colors.Theme; +pub const Theme = colors.LegacyTheme; /// Every theme: pardes's own, then those imported from helix and zed. The /// first is the default. -pub const themes: []const Theme = &colors.themes; +pub const themes: []const Theme = &colors.legacy_themes; /// The theme named `name`, whatever its case. pub fn theme(name: []const u8) ?*const Theme { - return &colors.themes[colors.themeIndex(name) orelse return null]; + return &colors.legacy_themes[colors.themeIndex(name) orelse return null]; } /// A theme's page and its text's ink as a file pane has them: its own, or /// its tag's for the one theme with none (`dark`, a terminal's own). diff --git a/core/root.zig b/core/root.zig index 65fefb37..11cb5058 100644 --- a/core/root.zig +++ b/core/root.zig @@ -37,6 +37,7 @@ pub const Stream = @import("Stream.zig"); pub const Handover = @import("Handover.zig"); pub const colors = @import("colors.zig"); pub const Theme = colors.Theme; +pub const LegacyTheme = colors.LegacyTheme; pub const themes = colors.themes; pub const ChromeTheme = colors.ChromeTheme; /// The screen types live in surface.zig; every shell names them through here. @@ -763,7 +764,7 @@ test "what core/ asks of the machine is where the list says, with why: a new lin /// state did not make it diverged a client: Pane.notices, then Tables' /// reading, 2026-11-19). A file, its names, the reason. const kept_out = [_]struct { []const u8, []const u8, []const u8 }{ - .{ "Core.zig", "surface facts frames own_frames chrome frame_arena scratch echo undrawn", "the frame's own and the views drawn here: written by frames (and chrome by a theme's step), read by frames and the runner; no step reads them" }, + .{ "Core.zig", "surface facts frames own_frames theme_chrome frame_arena scratch echo undrawn", "the frame's own and the views drawn here: written by frames (and chrome by a theme's step), read by frames and the runner; no step reads them" }, .{ "Core.zig", "needs_frame", "whether to draw: steps set it, the loop reads it; no step's outcome reads it" }, .{ "Core.zig", "effects effects_head effects_len in_q in_head in_len opts dump_out dump_export tape", "this process's queues, options and tape, drained or set by its host" }, .{ "Core.zig", "shell_rows", "a terminal's rows as read here, swept at every update; the terminal's own digest covers its active area, not its history (Term.zig `calls`)" }, diff --git a/core/theme_schema.zig b/core/theme_schema.zig new file mode 100644 index 00000000..788cf3bf --- /dev/null +++ b/core/theme_schema.zig @@ -0,0 +1,284 @@ +//! What a theme says (DESIGN.md B2, B3: the themes programme's v2 schema), +//! and the shape every theme had before it (`LegacyTheme`), with the +//! mechanical map between the two: the theme files in core/themes and the +//! generated ones are written in the old names until their own lines move +//! them, and a ThemeFile written in them (a user's own, DumpThemes' output +//! of an older build) loads forever. colors.zig derives everything a +//! surface draws from a `Theme` (colors.Chrome.of). +const std = @import("std"); + +/// A theme: ten colours, the syntax four among them, and the terminal's +/// sixteen (B2), each named for its role; then what a faithful port, acme +/// or lapis says of its own where the derived one would not be its (B3), +/// each derived when absent (colors.Chrome.of, rule by rule). +pub const Theme = struct { + name: []const u8, + /// The body's ground and ink; null leaves them to the host terminal (a + /// window: colors.null_page and #cccccc). + page: ?[3]u8, + text: ?[3]u8, + /// A pane's tag at rest, and its ink. + tag: [3]u8, + tag_text: [3]u8, + /// The theme's one accent: the focused grip, keys, the walked word, + /// links, the sam fence. + accent: [3]u8, + /// The selection's ground (its ink is derived, or `selection_text`). + selection: [3]u8, + keyword: [3]u8, + string: [3]u8, + number: [3]u8, + comment: [3]u8, + /// The terminal's sixteen, and the semantic hues: 1 red (error, + /// deleted), 2 green (added), 3 yellow (warning, hits), 4 blue (info), 6 + /// cyan (hint). Null: derived from the theme's own hues (R14). + ansi: ?[16][3]u8, + + // The overrides (B3). + selection_text: ?[3]u8 = null, + /// The focused tag's ground and ink (R4: else the tag a focus step off). + tag_focus: ?[3]u8 = null, + tag_focus_text: ?[3]u8 = null, + /// The mark on a reading pane's grip (R5', DESIGN.md G1': else sky, + /// mint, violet, paper or rose, the first told from the grip's other + /// hues, 3:1 on the tags). + mode: ?[3]u8 = null, + /// A name's tint in a tag (R13: else a hue of the theme's own at the + /// tag ink's lightness). + tag_name: ?[3]u8 = null, + /// The workspace row, an editor's status bar where it has one. + workspace: ?[3]u8 = null, + workspace_text: ?[3]u8 = null, + line_number: ?[3]u8 = null, + line_number_current: ?[3]u8 = null, + /// A search hit's ground (R8: else the page toward amber). + hit: ?[3]u8 = null, + /// The severities' inks (R9: else the ANSI red, yellow, blue, cyan). + @"error": ?[3]u8 = null, + warning: ?[3]u8 = null, + info: ?[3]u8 = null, + hint: ?[3]u8 = null, + /// What a diff adds and deletes (R10: else ANSI 2 and 1). + added: ?[3]u8 = null, + deleted: ?[3]u8 = null, + /// The rules between panes and columns, the scroll column and its + /// thumb, what an empty column shows, the rule under a tag (R11). + rule: ?[3]u8 = null, + rail: ?[3]u8 = null, + rail_thumb: ?[3]u8 = null, + empty_column: ?[3]u8 = null, + tag_rule: ?[3]u8 = null, + /// A grip's ring at rest, its fill while its file is unsaved, and a + /// column's grip (R6). + grip_ring: ?[3]u8 = null, + dirty: ?[3]u8 = null, + column_grip: ?[3]u8 = null, + /// A tag's selection where it is not the body's (acme's #9eeeee), and + /// each pointer button's sweep, select, exec and look (R7). + tag_selection: ?[3]u8 = null, + sweeps: ?[3]Sweep = null, + /// A pixel shell's rules and scroll column, in pixels (2 and 12 unset). + rule_px: ?u8 = null, + rail_px: ?u8 = null, + + pub const Sweep = struct { ground: [3]u8, text: [3]u8 }; + + /// A theme in the old names, in the new: each field renamed, the ones + /// now always derived (`dropped`) left out, the sweeps' two lists one. + pub fn fromLegacy(old: LegacyTheme) Theme { + var out: Theme = undefined; + inline for (renames) |r| @field(out, r[1]) = @field(old, r[0]); + out.sweeps = if (old.sweep_bg) |grounds| sweeps: { + var s: [3]Sweep = undefined; + for (&s, grounds, 0..) |*sweep, ground, i| sweep.* = .{ .ground = ground, .text = if (old.sweep_fg) |inks| inks[i] else old.sel_fg }; + break :sweeps s; + } else null; + out.mode = null; + out.added = null; + out.deleted = null; + return out; + } + + /// The old names' view of a theme, for what still reads them: the + /// fields the new names leave to the derivation (`derived`) filled in + /// from it; `added`, `deleted` and `mode`, which the old names have no + /// field for, left out. + pub fn toLegacy(th: *const Theme, derived: Derived) LegacyTheme { + var out: LegacyTheme = undefined; + inline for (renames) |r| { + const from = @field(th, r[1]); + const same = comptime @FieldType(LegacyTheme, r[0]) == @TypeOf(from); + @field(out, r[0]) = if (same) from else from orelse @field(derived, r[0]); + } + inline for (dropped) |name| @field(out, name) = @field(derived, name); + if (th.sweeps) |sweeps| { + out.sweep_bg = undefined; + out.sweep_fg = undefined; + for (sweeps, &out.sweep_bg.?, &out.sweep_fg.?) |s, *ground, *ink| { + ground.* = s.ground; + ink.* = s.text; + } + } else { + out.sweep_bg = null; + out.sweep_fg = null; + } + return out; + } + + /// What `toLegacy` takes from the derivation: the old names' required + /// fields the new ones make optional, and the ones they leave out. + pub const Derived = struct { + sel_fg: [3]u8, + lineno: [3]u8, + scroll_track: [3]u8, + scroll_thumb: [3]u8, + box_dim: [3]u8, + column_box_dim: ?[3]u8 = null, + tag_active_name_fg: ?[3]u8 = null, + search_fg: ?[3]u8 = null, + tag_reading_bg: ?[3]u8 = null, + }; +}; + +/// Old name, new name: the mechanical layer (DESIGN.md G6). Without the +/// rename this table, `LegacyTheme` and the two maps go. +const renames = [_][2][]const u8{ + .{ "name", "name" }, + .{ "bg", "page" }, + .{ "fg", "text" }, + .{ "tag_bg", "tag" }, + .{ "tag_fg", "tag_text" }, + .{ "box", "accent" }, + .{ "sel_bg", "selection" }, + .{ "kw", "keyword" }, + .{ "str", "string" }, + .{ "num", "number" }, + .{ "comment", "comment" }, + .{ "palette", "ansi" }, + .{ "sel_fg", "selection_text" }, + .{ "tag_active_bg", "tag_focus" }, + .{ "tag_active_fg", "tag_focus_text" }, + .{ "tag_name_fg", "tag_name" }, + .{ "tagline_bg", "workspace" }, + .{ "tagline_fg", "workspace_text" }, + .{ "lineno", "line_number" }, + .{ "lineno_active", "line_number_current" }, + .{ "search_bg", "hit" }, + .{ "diagnostic_error", "error" }, + .{ "diagnostic_warning", "warning" }, + .{ "diagnostic_info", "info" }, + .{ "diagnostic_hint", "hint" }, + .{ "border", "rule" }, + .{ "scroll_track", "rail" }, + .{ "scroll_thumb", "rail_thumb" }, + .{ "empty_col", "empty_column" }, + .{ "tag_rule", "tag_rule" }, + .{ "box_border", "grip_ring" }, + .{ "box_dirty", "dirty" }, + .{ "column_box", "column_grip" }, + .{ "tag_sel_bg", "tag_selection" }, + .{ "rule_px", "rule_px" }, + .{ "rail_px", "rail_px" }, +}; + +/// The old fields the new names derive always (B8), and the reading tag's +/// ground, which is the tag's now (G1'). +const dropped = [_][]const u8{ "box_dim", "column_box_dim", "tag_active_name_fg", "search_fg", "tag_reading_bg" }; + +// Every old field is renamed, dropped, or a sweep's; every new one renamed +// to, the sweeps, or new (`mode`, `added`, `deleted`): a field added to either +// without its line here is a compile error. +comptime { + @setEvalBranchQuota(20_000); + for (@typeInfo(LegacyTheme).@"struct".field_names) |f| { + const known = for (renames) |r| { + if (std.mem.eql(u8, r[0], f)) break true; + } else for (dropped) |d| { + if (std.mem.eql(u8, d, f)) break true; + } else std.mem.eql(u8, f, "sweep_bg") or std.mem.eql(u8, f, "sweep_fg"); + if (!known) @compileError("LegacyTheme." ++ f ++ " has no new name"); + } + for (@typeInfo(Theme).@"struct".field_names) |f| { + const known = for (renames) |r| { + if (std.mem.eql(u8, r[1], f)) break true; + } else std.mem.eql(u8, f, "sweeps") or std.mem.eql(u8, f, "mode") or std.mem.eql(u8, f, "added") or std.mem.eql(u8, f, "deleted"); + if (!known) @compileError("Theme." ++ f ++ " has no old name"); + } +} + +/// A theme as every theme was written up to 2026-11-25: the files in +/// core/themes and the generated ones (folded through it at comptime: +/// colors.fold), and every ThemeFile in it (`.tag_bg` names it). +pub const LegacyTheme = struct { + name: []const u8, + bg: ?[3]u8, + fg: ?[3]u8, + sel_bg: [3]u8, + sel_fg: [3]u8, + tag_bg: [3]u8, + tag_fg: [3]u8, + box: [3]u8, // move box, focused pane + box_dim: [3]u8, // move box, everything else + kw: [3]u8, + str: [3]u8, + num: [3]u8, + comment: [3]u8, + lineno: [3]u8, + scroll_track: [3]u8, + scroll_thumb: [3]u8, + palette: ?[16][3]u8, + + // Pardes UI roles. Optional so existing ThemeFile documents and imported + // editor palettes keep working; ChromeTheme resolves their legacy defaults. + tag_active_bg: ?[3]u8 = null, + tag_active_fg: ?[3]u8 = null, + /// The workspace row (the tagline): an editor's status bar in its own + /// colours, where it has them; else the tag's. + tagline_bg: ?[3]u8 = null, + tagline_fg: ?[3]u8 = null, + /// Filename/output-name accent, also used by the Term command. + tag_name_fg: ?[3]u8 = null, + /// Active tint; absence preserves the shared name tint or active tag ink. + tag_active_name_fg: ?[3]u8 = null, + /// Column grips stay distinct from pane grips in both focus states. + column_box: ?[3]u8 = null, + column_box_dim: ?[3]u8 = null, + border: ?[3]u8 = null, + /// What an empty column shows under its tag; the border fill without it. + empty_col: ?[3]u8 = null, + search_bg: ?[3]u8 = null, + search_fg: ?[3]u8 = null, + diagnostic_error: ?[3]u8 = null, + diagnostic_warning: ?[3]u8 = null, + diagnostic_info: ?[3]u8 = null, + diagnostic_hint: ?[3]u8 = null, + // The current line number is a quiet foreground cue, not a tag-colored + // background block. Older themes fall back to their normal gutter ink. + lineno_active: ?[3]u8 = null, + /// A tag's selection, where it differs from the body's (acme's #9eeeee). + tag_sel_bg: ?[3]u8 = null, + /// The tag of a reading pane, one a jump steps (Trail.zig): unset, or + /// too near the tag to see, the tag toward paper yellow (readingOf). + tag_reading_bg: ?[3]u8 = null, + /// Each pointer button's sweep, select, exec and look (Mouse.Button + /// order), and the ink over it. Unset, tints of `sel_bg` in `sel_fg`. + sweep_bg: ?[3][3]u8 = null, + sweep_fg: ?[3][3]u8 = null, + /// A pixel shell's rules, in pixels (2 unset); the rule between a tag + /// and its body is always one. + rule_px: ?u8 = null, + /// A grip is acme's button: `box` filled when focused, else a ring of + /// `box_border` (unset: `box_dim`) round the tag's ground; `box_dirty` + /// (unset: `diagnostic_warning`, then `num`) fills it, in either ring, + /// while its file is unsaved. + box_border: ?[3]u8 = null, + box_dirty: ?[3]u8 = null, + /// The rule between a pane's tag and its body, one pixel, quieter than + /// the rules between panes (acme's #8888cc under its 2px black ones). + /// Unset, halfway between the tag's ground and the page. + tag_rule: ?[3]u8 = null, + /// A pixel shell's scroll column, the grip's button over the scrollbar, + /// in pixels at a 17px tagline, the thumb one narrower (acme's 12px + /// Scrollwid, the default). + rail_px: ?u8 = null, +}; |
