summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-26 04:30:06 -0300
committerGabriel Schneider <[email protected]>2026-11-26 04:36:36 -0300
commit47952d7797b25f41a3e29ec0dc349564c0185fd4 (patch)
tree32fa77e7616743e6fef821e81cc4b2a5222b7580
parent0788ce68c4c43148d3d5fa173b4fe46aa10371c9 (diff)
downloadpardes-47952d7797b25f41a3e29ec0dc349564c0185fd4.tar.gz
pardes-47952d7797b25f41a3e29ec0dc349564c0185fd4.zip
gen_themes: the importer reads what the helix and zed sources already say (the themes programme's L4; DESIGN.md B7, R3, E1): HELIX: the text the theme draws code in, `variable` where it is a neutral ink (OKLab chroma under 0.04) that reads 4.5:1 on the page, else `ui.text` (github's muted #8b949e gives way to its #c9d1d9; github_dark_dimmed's 3.9:1 to 7.6:1); the tag the inactive status bar where the theme has one, the active one its focused tag where it is tone on tone, the tag's ink the status bar's (an inactive bar's is often faded, neonotte's #555973); the four severities from `diagnostic.*` (often an underline only, no fg) and the plain `error`/`warning`/`info`/`hint` scopes, error else the theme's own palette red (UX E3), never the terminal's guessed one; `added`/`deleted` from `diff.plus`/`diff.minus` where they are a green and a red (each a hue, within 60 degrees: a grey, a yellow deleted or a tritan blue cannot be told apart as R10's rows: ashen, wolf_alabaster's four, zenburn, github_light_tritanopia and modus_operandi_tritanopia failed the ring's diff check on them, and keep pardes's); the caret's line number from `ui.linenr.selected`; a program's sixteen from the theme's own palette where it names red, green, yellow, blue, magenta (or purple) and cyan (or aqua), their light or bright ones, black, white and a grey (else the page, the ink, the comment), else null for R14; ZED: the active tab the focused tag, error/warning/info/hint, created/deleted (the same green-and-red rule), the search match as drawn (its alpha over the editor's page, not dropped), the active line number; R3, THE IMPORT RULE (L1 left it to the importer: a theme carries no import flag): a status bar on the other side of light from its page is no tag: the tag is the page a 1.15:1 step off in its own hue (its light scaled), in the text; E1's import FIXES, a table in the generator (a name no vendored theme has is a build error): everforest_light's and hex_toxic's search hit, their own yellow as a ground their soft text reads on (#ffe699 1.14:1 off the page, the text 4.53:1; #2f3200 1.11:1, 4.27:1; R8 takes it at R0's 1.1:1 with 0.08 OKLab, L3); the generator does the little colour maths it needs (luminance, contrast, OKLab a/b, the step), the dependency-free host tool it was; across the 203 imports: 202 errors, 202 warnings, 196 infos, 198 hints, 178 added/deleted pairs, 195 caret line numbers, 59 focused tags, 13 hits of their own; RUN (TEST FIRST): tools/gen_themes.zig's own tests, a minimal toml or json each, now in core-test (build/tests.zig, on the host): "B7: the text is helix's `variable` ...", "B7: the tag is the inactive status bar, and the active one its focus where it is tone on tone", "R3, the import rule: ...", "B7: the severities from diagnostic.* and the plain scopes ...", "B7: added and deleted from diff.plus and diff.minus ... (a yellow deleted, a grey added: none)", "B7: a program's sixteen from the palette ...", "B7, zed: ... a search match as drawn over the page": on L3's generator none compiles (no field for any of them); the ring test with L4's six taken off its list: everforest_light and hex_toxic fail hit, github_dark_dimmed text, gruvbox_material_light_hard/_medium/_soft severities4; with the mapping the diff rows of the eight above failed until the green-and-red rule: 272 of 285 themes pass (266 at L3), 13 on the list (L5 1, L7 8, soft 4); colors.zig's neonotte84 golden re-pinned where B7 moves it: its match ink and current hit, the hits' amber, #e29e47 (the warning pardes derived) to #e59d3c (its own palette's yellow now its warning); core-test 1140/1313 (173 skipped: L3's 1133/1306 + the importer's 7; the full run's one failure the neonotte84 golden, re-pinned after it, its test alone passing); OUTSIDE my files: build/tests.zig (the importer's tests in core-test, 5 lines); no dump or digest change
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
-rw-r--r--build/tests.zig5
-rw-r--r--core/colors.zig13
-rw-r--r--tools/gen_themes.zig363
3 files changed, 361 insertions, 20 deletions
diff --git a/build/tests.zig b/build/tests.zig
index 8cf42acd..c35857f7 100644
--- a/build/tests.zig
+++ b/build/tests.zig
@@ -76,6 +76,11 @@ pub fn add(b: *std.Build, c: struct {
core_test_step.dependOn(&run_core_test.step);
if (core_match) |check| check.addArtifactArg(core_test);
}
+ // The theme importer's own tests (tools/gen_themes.zig): its mapping,
+ // rule by rule, on the machine that builds.
+ const gen_themes_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("tools/gen_themes.zig"), .target = b.graph.host, .optimize = .debug }), .filters = c.test_filters });
+ core_test_step.dependOn(&b.addRunArtifact(gen_themes_test).step);
+ if (core_match) |check| check.addArtifactArg(gen_themes_test);
// The terminal module's own tests (core/term/): the emulator without a core.
const run_term_test = units.add(b, b.addTest(.{ .root_module = c.parts.term, .filters = c.test_filters }), null);
b.step("term-test", "run the terminal module's own tests (core/term/)").dependOn(&run_term_test.step);
diff --git a/core/colors.zig b/core/colors.zig
index ed82b731..b089a1b9 100644
--- a/core/colors.zig
+++ b/core/colors.zig
@@ -2384,12 +2384,14 @@ test "neonotte84's and acme's inks on the plain tag, their reading mark and thei
const wants = [_]Want{
// 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
+ // over all seven grounds at once: #f1a271; L1's #e29e47 the warning
+ // pardes derived, its own palette's since B7, #e59d3c, the current
+ // hit's too); 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 } },
+ .{ .name = "neonotte84", .text = .{ 0xb5, 0xbf, 0xe3 }, .dim = .{ 0x70, 0x76, 0x8f }, .rule = .{ 0x4c, 0x50, 0x63 }, .key = .{ 0xf8, 0xde, 0xd9 }, .match = .{ 0xe5, 0x9d, 0x3c }, .mode = .{ 0x2b, 0x92, 0xbc }, .hit = .{ 0x4c, 0x30, 0x1a }, .hit_current = .{ 0xe5, 0x9d, 0x3c } },
// 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
@@ -2604,13 +2606,6 @@ fn ringFailures(th: *const Theme, c: *const Chrome) std.EnumSet(RingCheck) {
/// 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 }{
- // 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.
diff --git a/tools/gen_themes.zig b/tools/gen_themes.zig
index fd152aa6..c369b744 100644
--- a/tools/gen_themes.zig
+++ b/tools/gen_themes.zig
@@ -71,10 +71,22 @@ const Theme = struct {
line_number: Rgb,
rail: Rgb,
rail_thumb: Rgb,
- /// A message's error ink (colors.Chrome's severities): diagnostic.error's,
- /// else error's, else the theme's red (UX E3: neonotte's diagnostic.error
- /// is an underline only).
+ /// B7: what the sources say beyond the ten (core/theme_schema.zig's
+ /// overrides): the focused tag, the severities, a diff's hues, the hits,
+ /// the caret's line number. Null: derived (colors.Chrome.of).
+ tag_focus: ?Rgb = null,
+ tag_focus_text: ?Rgb = null,
+ /// A message's error ink: diagnostic.error's, else error's, else the
+ /// theme's own red (UX E3: neonotte's diagnostic.error is an underline
+ /// only).
@"error": ?Rgb = null,
+ warning: ?Rgb = null,
+ info: ?Rgb = null,
+ hint: ?Rgb = null,
+ added: ?Rgb = null,
+ deleted: ?Rgb = null,
+ hit: ?Rgb = null,
+ line_number_current: ?Rgb = null,
ansi: ?[16]Rgb,
};
@@ -104,6 +116,13 @@ pub fn main(init: std.process.Init) !void {
}
}
for (stems.items) |stem| try themes.append(arena, try helixTheme(arena, stem, resolve(arena, &tomls, stem, 8)));
+ for (themes.items) |*t| toneOnTone(t);
+ for (fixes) |f| {
+ const t = for (themes.items) |*t| {
+ if (std.mem.eql(u8, t.name, f.name)) break t;
+ } else std.process.fatal("tools/gen_themes.zig fixes {s}, which no vendored theme is", .{f.name});
+ t.hit = f.hit;
+ }
// sorted, so the ring's tail is the same on every machine whatever order
// the directory happened to be read in
std.mem.sort(Theme, themes.items, {}, byName);
@@ -304,6 +323,12 @@ fn helixTheme(arena: std.mem.Allocator, stem: []const u8, h: Helix) !Theme {
const bg = h.get("ui.background", .bg);
const ink = fg orelse ansi_default[7];
const page = bg orelse ansi_default[0];
+ // B7: the text is what the theme draws code in. Many set `ui.text` to a
+ // muted UI grey and code in `variable` (github's #8b949e and #c9d1d9):
+ // `variable` where it is a neutral ink that reads 4.5:1 on the page; a
+ // coloured one (base16's red) is an identifier's hue, not the text.
+ const text = if (h.get("variable", .fg)) |v| (if (chroma(v) < 0.04 and contrast(v, page) >= 4.5) v else fg) else fg;
+ const body = text orelse ink;
// The move box is the accent that says "here". helix's is the primary
// cursor's background — but a theme that draws its cursor by REVERSING
// video (nord, monokai) never names one, so the fallback is the loudest
@@ -333,22 +358,30 @@ fn helixTheme(arena: std.mem.Allocator, stem: []const u8, h: Helix) !Theme {
const sel_lum = @as(i32, sel[0]) * 3 + @as(i32, sel[1]) * 6 + sel[2];
const ink_lum = @as(i32, ink[0]) * 3 + @as(i32, ink[1]) * 6 + ink[2];
const page_lum = @as(i32, page[0]) * 3 + @as(i32, page[1]) * 6 + page[2];
- const sel_far = if (@abs(sel_lum - ink_lum) >= @abs(sel_lum - page_lum)) ink else page;
+ const sel_far = if (@abs(sel_lum - ink_lum) >= @abs(sel_lum - page_lum)) body else page;
// A theme that DID name a selection foreground meant it — 16 of the
// vendored ones do — except when it is the colour taken for the background
// one line up, which is the reverse-video case again: those two would paint
// the highlight in one colour on itself.
const named_ink = h.get("ui.selection", .fg);
+ // B7: the tag is the inactive status bar where the theme has one (107
+ // do), and the active one its focused tag where it is tone on tone.
+ const inactive = h.get("ui.statusline.inactive", .bg);
+ const tag = inactive orelse h.get("ui.statusline", .bg) orelse page;
+ const active = if (inactive != null) h.get("ui.statusline", .bg) else null;
+ const focused = if (active) |a| !std.mem.eql(u8, &a, &tag) and dark(a) == dark(tag) else false;
return .{
.name = try slug(arena, stem),
- // `palette` stays null: helix leaves the colors a PROGRAM emits alone,
- // and so do we.
.page = bg,
- .text = fg,
+ .text = text,
.selection = sel,
.selection_text = if (named_ink) |c| (if (std.mem.eql(u8, &c, &sel)) sel_far else c) else sel_far,
- .tag = h.get("ui.statusline", .bg) orelse page,
+ .tag = tag,
+ // the status bar's ink (an inactive bar's is often a faded one,
+ // neonotte's #555973, where pardes's tag holds words to run)
.tag_text = h.get("ui.statusline", .fg) orelse ink,
+ .tag_focus = if (focused) active else null,
+ .tag_focus_text = if (focused) h.get("ui.statusline", .fg) else null,
.accent = box,
.keyword = h.get("keyword", .fg) orelse ink,
.string = h.get("string", .fg) orelse ink,
@@ -362,11 +395,47 @@ fn helixTheme(arena: std.mem.Allocator, stem: []const u8, h: Helix) !Theme {
// anything else in the theme's greys) for the thumb riding it
.rail = h.get("ui.statusline", .bg) orelse page,
.rail_thumb = sel,
- .@"error" = h.first(&.{ .{ "diagnostic.error", .fg }, .{ "error", .fg } }) orelse h.color("red"),
- .ansi = null,
+ // B7: the severities from diagnostic.* (often an underline only,
+ // which names no fg) and the plain scopes; error, else the theme's
+ // own red (UX E3)
+ .@"error" = h.first(&.{ .{ "diagnostic.error", .fg }, .{ "error", .fg } }) orelse h.named(&.{"red"}),
+ .warning = h.first(&.{ .{ "diagnostic.warning", .fg }, .{ "warning", .fg } }),
+ .info = h.first(&.{ .{ "diagnostic.info", .fg }, .{ "info", .fg } }),
+ .hint = h.first(&.{ .{ "diagnostic.hint", .fg }, .{ "hint", .fg } }),
+ .added = diffPair(h.get("diff.plus", .fg), h.get("diff.minus", .fg))[0],
+ .deleted = diffPair(h.get("diff.plus", .fg), h.get("diff.minus", .fg))[1],
+ .line_number_current = h.get("ui.linenr.selected", .fg),
+ // B7: a program's sixteen from the theme's own palette where it names
+ // the six hues; else pardes derives them from its colours (R14)
+ .ansi = h.sixteen(page, ink, h.get("comment", .fg) orelse ink),
};
}
+/// DESIGN.md E1's fixes for an import: a colour of its own its source does
+/// not name, where the one pardes derives cannot meet a floor.
+const fixes = [_]struct { name: []const u8, hit: Rgb }{
+ // Its text reads 5.18:1 on its page: no ground 1.5:1 off leaves it its
+ // 4.5:1, and the derived amber stops short: its yellow (#dfa000) as a
+ // ground 1.14:1 off the page, 0.09 OKLab, the text 4.53:1 on it (R0, R8).
+ .{ .name = "everforest_light", .hit = .{ 0xff, 0xe6, 0x99 } },
+ // Its text 4.73:1: its toxic yellow (#c9d400) a ground 1.11:1 off the
+ // page, 0.09 OKLab, the text 4.27:1 on it, its floor (R0, R8).
+ .{ .name = "hex_toxic", .hit = .{ 0x2f, 0x32, 0x00 } },
+};
+
+/// R3, the import rule: a status bar on the other side of light from the
+/// page (merionette's red one on a dark page) is no tag: the tag is the page
+/// a 1.15 step off in its own hue, in the text (tone on tone).
+fn toneOnTone(t: *Theme) void {
+ const page = t.page orelse ansi_default[0];
+ if (dark(t.tag) == dark(page)) return;
+ t.tag = stepOff(page, 1.15);
+ t.tag_text = t.text orelse ansi_default[7];
+ t.tag_focus = null;
+ t.tag_focus_text = null;
+ t.rail = t.tag;
+}
+
/// helix lets a theme name one of the terminal's sixteen colors instead of an
/// RGB (`fg = "light-gray"`), and four of them — base16_terminal, term16_dark,
/// term16_light, ttox — are written that way from top to bottom. pardes's Theme
@@ -412,6 +481,38 @@ const Helix = struct {
return null;
}
+ /// The first of `names` the theme's own [palette] has (never the
+ /// terminal's sixteen it falls back to: those are a guess).
+ fn named(h: Helix, names: []const []const u8) ?Rgb {
+ for (names) |n| if (h.palette.contains(n)) return h.color(n);
+ return null;
+ }
+
+ /// B7: a program's sixteen from the palette's own red, green, yellow,
+ /// blue, magenta (or purple) and cyan (or aqua), their light or bright
+ /// ones (else the same), black (else the page), white (else the ink) and
+ /// a grey (else the comment); null unless all six hues are named.
+ fn sixteen(h: Helix, page: Rgb, ink: Rgb, comment: Rgb) ?[16]Rgb {
+ const hues = [6][2][]const u8{ .{ "red", "red" }, .{ "green", "green" }, .{ "yellow", "yellow" }, .{ "blue", "blue" }, .{ "magenta", "purple" }, .{ "cyan", "aqua" } };
+ var out: [16]Rgb = undefined;
+ var buf: [4][32]u8 = undefined;
+ for (hues, 1..) |pair, i| {
+ const n = if (h.palette.contains(pair[0])) pair[0] else pair[1];
+ out[i] = h.named(&.{n}) orelse return null;
+ out[i + 8] = h.named(&.{
+ std.fmt.bufPrint(&buf[0], "light-{s}", .{n}) catch unreachable,
+ std.fmt.bufPrint(&buf[1], "bright-{s}", .{n}) catch unreachable,
+ std.fmt.bufPrint(&buf[2], "light_{s}", .{n}) catch unreachable,
+ std.fmt.bufPrint(&buf[3], "bright_{s}", .{n}) catch unreachable,
+ }) orelse out[i];
+ }
+ out[0] = h.named(&.{"black"}) orelse page;
+ out[7] = h.named(&.{ "white", "light-gray", "light_gray" }) orelse ink;
+ out[8] = h.named(&.{ "gray", "grey", "bright-black", "bright_black" }) orelse comment;
+ out[15] = h.named(&.{ "bright-white", "bright_white", "light-white" }) orelse out[7];
+ return out;
+ }
+
/// `first`, skipping any candidate that comes out equal to the page. A
/// cursor or a selection painted in the page's own colour is a theme
/// drawing that highlight by REVERSING video (it names the cell it is about
@@ -564,8 +665,10 @@ fn readZed(arena: std.mem.Allocator, gpa: std.mem.Allocator, path: []const u8, t
// always a step off the background and never off the text.
.selection = z.need("element.selected"),
.selection_text = z.need("editor.foreground"),
+ // B7: the status bar the tag, the active tab its focus
.tag = z.need("status_bar.background"),
.tag_text = z.need("text.muted"),
+ .tag_focus = z.get("tab.active_background"),
// zed names its accent, so the move box does not have to be guessed
// out of the cursor the way it is for a helix theme
.accent = z.need("text.accent"),
@@ -577,7 +680,15 @@ fn readZed(arena: std.mem.Allocator, gpa: std.mem.Allocator, path: []const u8, t
.rail = z.need("scrollbar.thumb.border"),
.rail_thumb = z.need("scrollbar.thumb.background"),
// its status colour, else the palette's red (colors.zig)
- .@"error" = if (z.style.get("error") != null) z.need("error") else null,
+ .@"error" = z.get("error"),
+ .warning = z.get("warning"),
+ .info = z.get("info"),
+ .hint = z.get("hint"),
+ .added = diffPair(z.get("created"), z.get("deleted"))[0],
+ .deleted = diffPair(z.get("created"), z.get("deleted"))[1],
+ // a search match is a translucent wash over the page: as drawn
+ .hit = z.over("search.match_background", z.need("editor.background")),
+ .line_number_current = z.get("editor.active_line_number"),
.ansi = pal,
});
}
@@ -590,6 +701,24 @@ const Zed = struct {
path: []const u8,
style: std.json.ObjectMap,
+ fn get(z: Zed, key: []const u8) ?Rgb {
+ return if (z.style.get(key) != null) z.need(key) else null;
+ }
+
+ /// A colour with an alpha (`#rrggbbaa`) as it shows over `ground`.
+ fn over(z: Zed, key: []const u8, ground: Rgb) ?Rgb {
+ const s = switch (z.style.get(key) orelse return null) {
+ .string => |s| s,
+ else => std.process.fatal("{s}: style.{s} is not a color string", .{ z.path, key }),
+ };
+ const c = hex(z.path, s);
+ const d = if (s.len > 0 and s[0] == '#') s[1..] else s;
+ const alpha: f32 = if (d.len == 8) @as(f32, @floatFromInt(std.fmt.parseInt(u8, d[6..8], 16) catch std.process.fatal("{s}: \"{s}\" is not #rrggbbaa", .{ z.path, s }))) / 255 else 1;
+ var out: Rgb = undefined;
+ for (&out, c, ground) |*o, x, g| o.* = @intFromFloat(@round(@as(f32, @floatFromInt(x)) * alpha + @as(f32, @floatFromInt(g)) * (1 - alpha)));
+ return out;
+ }
+
fn need(z: Zed, key: []const u8) Rgb {
const v = z.style.get(key) orelse std.process.fatal("{s}: missing style.{s}", .{ z.path, key });
return switch (v) {
@@ -608,6 +737,79 @@ const Zed = struct {
}
};
+// ---- colour: the little of colors.zig's maths B7 and R3 need ----
+
+fn linear(c: u8) f32 {
+ const x = @as(f32, @floatFromInt(c)) / 255;
+ return if (x <= 0.04045) x / 12.92 else std.math.pow(f32, (x + 0.055) / 1.055, 2.4);
+}
+
+fn luminance(c: Rgb) f32 {
+ return 0.2126 * linear(c[0]) + 0.7152 * linear(c[1]) + 0.0722 * linear(c[2]);
+}
+
+fn contrast(a: Rgb, b: Rgb) f32 {
+ const x = luminance(a);
+ const y = luminance(b);
+ return (@max(x, y) + 0.05) / (@min(x, y) + 0.05);
+}
+
+/// colors.zig's isDark.
+fn dark(c: Rgb) bool {
+ return luminance(c) < 0.18;
+}
+
+/// A colour's OKLab a and b: where its hue points and how far from grey.
+fn ab(c: Rgb) [2]f32 {
+ const l = [3]f32{ linear(c[0]), linear(c[1]), linear(c[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 .{ 1.9779984951 * lm - 2.4285922050 * mm + 0.4505937099 * sm, 0.0259040371 * lm + 0.7827717662 * mm - 0.8086757660 * sm };
+}
+
+/// How far from grey a colour is: its OKLab chroma.
+fn chroma(c: Rgb) f32 {
+ const v = ab(c);
+ return @sqrt(v[0] * v[0] + v[1] * v[1]);
+}
+
+/// A hue (chroma 0.04) within 60 degrees of `hue` (OKLab).
+fn hueNear(c: Rgb, hue: f32) bool {
+ const v = ab(c);
+ const h = std.math.radiansToDegrees(std.math.atan2(v[1], v[0]));
+ const apart = @abs(@mod(h - hue + 540, 360) - 180);
+ return chroma(c) >= 0.04 and apart <= 60;
+}
+
+/// R10 draws a diff's rows in its added and deleted hues: a theme's own
+/// where they are a green and a red, else pardes's, its sixteen's (a grey, a
+/// yellow for deleted or a tritan blue cannot be told apart as rows: ashen,
+/// wolf_alabaster's, modus_operandi_tritanopia's).
+fn diffPair(plus: ?Rgb, minus: ?Rgb) [2]?Rgb {
+ const p = plus orelse return .{ null, null };
+ const m = minus orelse return .{ null, null };
+ return if (hueNear(p, 145) and hueNear(m, 25)) .{ p, m } else .{ null, null };
+}
+
+/// `page` a `step`:1 contrast off itself, lighter on a dark page, darker on
+/// a light one, its light scaled so its hue stays (a black page a grey).
+fn stepOff(page: Rgb, step: f32) Rgb {
+ const y = luminance(page);
+ const want = if (dark(page)) step * (y + 0.05) - 0.05 else (y + 0.05) / step - 0.05;
+ var out: Rgb = undefined;
+ for (&out, page) |*o, c| {
+ const l = if (y > 0) @min(linear(c) * want / y, 1) else want;
+ const e = if (l <= 0.0031308) l * 12.92 else 1.055 * std.math.pow(f32, l, 1.0 / 2.4) - 0.055;
+ o.* = @intFromFloat(@round(e * 255));
+ }
+ // the bytes' rounding short of the step: one more
+ while (contrast(out, page) < step) for (&out) |*o| {
+ o.* = if (dark(page)) o.* +| 1 else o.* -| 1;
+ };
+ return out;
+}
+
/// `#rrggbb`, helix's `#rgb` shorthand, or zed's `#rrggbbaa`. The alpha is
/// DROPPED: pardes paints opaque cells, and the only translucent colors we read
/// are the scrollbar's, where a dropped alpha reads as a slightly bolder bar and
@@ -625,3 +827,142 @@ fn hex(path: []const u8, s: []const u8) Rgb {
}
return out;
}
+
+// ---- tests: each of B7's rules on a minimal theme ----
+
+fn helixOf(arena: std.mem.Allocator, text: []const u8) !Theme {
+ var tomls: std.StringHashMapUnmanaged(Toml) = .empty;
+ try tomls.put(arena, "t", parseToml(arena, "t.toml", text));
+ var t = try helixTheme(arena, "t", resolve(arena, &tomls, "t", 8));
+ toneOnTone(&t);
+ return t;
+}
+
+test "B7: the text is helix's `variable` where it is a neutral ink that reads 4.5:1, else ui.text" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ // github's: ui.text a muted grey, code in a neutral variable
+ const github = try helixOf(a, "\"ui.background\" = { bg = \"#0d1117\" }\n\"ui.text\" = \"#8b949e\"\nvariable = \"#c9d1d9\"\n");
+ try std.testing.expectEqual(Rgb{ 0xc9, 0xd1, 0xd9 }, github.text.?);
+ // a coloured variable (base16's red) is an identifier's hue
+ const base16 = try helixOf(a, "\"ui.background\" = { bg = \"#181818\" }\n\"ui.text\" = \"#d8d8d8\"\nvariable = \"#ab4642\"\n");
+ try std.testing.expectEqual(Rgb{ 0xd8, 0xd8, 0xd8 }, base16.text.?);
+ // a neutral variable that does not read stays out
+ const faint = try helixOf(a, "\"ui.background\" = { bg = \"#181818\" }\n\"ui.text\" = \"#d8d8d8\"\nvariable = \"#444444\"\n");
+ try std.testing.expectEqual(Rgb{ 0xd8, 0xd8, 0xd8 }, faint.text.?);
+}
+
+test "B7: the tag is the inactive status bar, and the active one its focus where it is tone on tone" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const both = try helixOf(a, "\"ui.background\" = { bg = \"#1e1e2e\" }\n\"ui.text\" = \"#cdd6f4\"\n\"ui.statusline\" = { fg = \"#cdd6f4\", bg = \"#313244\" }\n\"ui.statusline.inactive\" = { fg = \"#a6adc8\", bg = \"#181825\" }\n");
+ try std.testing.expectEqual(Rgb{ 0x18, 0x18, 0x25 }, both.tag);
+ try std.testing.expectEqual(Rgb{ 0xcd, 0xd6, 0xf4 }, both.tag_text);
+ try std.testing.expectEqual(Rgb{ 0x31, 0x32, 0x44 }, both.tag_focus.?);
+ try std.testing.expectEqual(Rgb{ 0xcd, 0xd6, 0xf4 }, both.tag_focus_text.?);
+ // one status bar: the tag, its focus pardes's to derive
+ const one = try helixOf(a, "\"ui.background\" = { bg = \"#1e1e2e\" }\n\"ui.statusline\" = { fg = \"#cdd6f4\", bg = \"#313244\" }\n");
+ try std.testing.expectEqual(Rgb{ 0x31, 0x32, 0x44 }, one.tag);
+ try std.testing.expectEqual(null, one.tag_focus);
+ // an active bar on the other side of light is no focus
+ const loud = try helixOf(a, "\"ui.background\" = { bg = \"#1e1e2e\" }\n\"ui.statusline\" = { fg = \"#000000\", bg = \"#f0f0f0\" }\n\"ui.statusline.inactive\" = { fg = \"#a6adc8\", bg = \"#181825\" }\n");
+ try std.testing.expectEqual(null, loud.tag_focus);
+}
+
+test "R3, the import rule: a status bar on the other side of light from the page leaves the page a 1.15 step off as the tag, in the text" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const t = try helixOf(arena.allocator(), "\"ui.background\" = { bg = \"#1a1a1a\" }\n\"ui.text\" = \"#e0e0e0\"\n\"ui.statusline\" = { fg = \"#ffffff\", bg = \"#e05050\" }\n");
+ try std.testing.expect(dark(t.tag) and contrast(t.tag, .{ 0x1a, 0x1a, 0x1a }) >= 1.15 and contrast(t.tag, .{ 0x1a, 0x1a, 0x1a }) < 1.2);
+ try std.testing.expectEqual(Rgb{ 0xe0, 0xe0, 0xe0 }, t.tag_text);
+ try std.testing.expectEqual(t.tag, t.rail);
+ // and on paper, darker
+ const light = try helixOf(arena.allocator(), "\"ui.background\" = { bg = \"#f8f8f0\" }\n\"ui.text\" = \"#202020\"\n\"ui.statusline\" = { fg = \"#ffffff\", bg = \"#203040\" }\n");
+ try std.testing.expect(!dark(light.tag) and luminance(light.tag) < luminance(.{ 0xf8, 0xf8, 0xf0 }) and contrast(light.tag, .{ 0xf8, 0xf8, 0xf0 }) >= 1.15);
+}
+
+test "B7: the severities from diagnostic.* and the plain scopes, error else the theme's own red" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ // gruvbox_material's: diagnostic.* an underline only, the plain scopes named
+ const t = try helixOf(a, "\"diagnostic.error\" = { underline = { color = \"red\", style = \"curl\" } }\n\"diagnostic.warning\" = { fg = \"#d8a657\" }\nerror = \"red\"\nwarning = \"#111111\"\ninfo = \"blue\"\nhint = \"#a9b665\"\n[palette]\nred = \"#ea6962\"\nblue = \"#7daea3\"\n");
+ try std.testing.expectEqual(Rgb{ 0xea, 0x69, 0x62 }, t.@"error".?);
+ try std.testing.expectEqual(Rgb{ 0xd8, 0xa6, 0x57 }, t.warning.?);
+ try std.testing.expectEqual(Rgb{ 0x7d, 0xae, 0xa3 }, t.info.?);
+ try std.testing.expectEqual(Rgb{ 0xa9, 0xb6, 0x65 }, t.hint.?);
+ // none named: the theme's own red, never the terminal's guess; the rest pardes's
+ const bare = try helixOf(a, "[palette]\nred = \"#ff7ea2\"\n");
+ try std.testing.expectEqual(Rgb{ 0xff, 0x7e, 0xa2 }, bare.@"error".?);
+ try std.testing.expectEqual(null, bare.warning);
+ try std.testing.expectEqual(null, (try helixOf(a, "\"ui.text\" = \"#cccccc\"\n")).@"error");
+}
+
+test "B7: added and deleted from diff.plus and diff.minus, the caret's line number from ui.linenr.selected" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const t = try helixOf(arena.allocator(), "\"diff.plus\" = \"#9feb99\"\n\"diff.minus\" = { fg = \"#ff7ea2\" }\n\"ui.linenr.selected\" = { fg = \"#abb7ff\" }\n");
+ try std.testing.expectEqual(Rgb{ 0x9f, 0xeb, 0x99 }, t.added.?);
+ try std.testing.expectEqual(Rgb{ 0xff, 0x7e, 0xa2 }, t.deleted.?);
+ try std.testing.expectEqual(Rgb{ 0xab, 0xb7, 0xff }, t.line_number_current.?);
+ // wolf_alabaster's deleted is a yellow, ashen's added a grey: rows of
+ // them cannot be told apart, so pardes's green and red
+ const yellow = try helixOf(arena.allocator(), "\"diff.plus\" = \"#95cb82\"\n\"diff.minus\" = \"#dfdf8e\"\n");
+ try std.testing.expectEqual(null, yellow.added);
+ try std.testing.expectEqual(null, yellow.deleted);
+ try std.testing.expectEqual(null, (try helixOf(arena.allocator(), "\"diff.plus\" = \"#737373\"\n\"diff.minus\" = \"#b14242\"\n")).added);
+}
+
+test "B7: a program's sixteen from the palette where it names the six hues, purple and aqua for magenta and cyan" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const t = try helixOf(a, "\"ui.background\" = { bg = \"#282828\" }\n\"ui.text\" = \"#ebdbb2\"\ncomment = \"#928374\"\n[palette]\nred = \"#cc241d\"\ngreen = \"#98971a\"\nyellow = \"#d79921\"\nblue = \"#458588\"\npurple = \"#b16286\"\naqua = \"#689d6a\"\nlight-red = \"#fb4934\"\n");
+ const s = t.ansi.?;
+ try std.testing.expectEqual(Rgb{ 0xcc, 0x24, 0x1d }, s[1]);
+ try std.testing.expectEqual(Rgb{ 0xb1, 0x62, 0x86 }, s[5]);
+ try std.testing.expectEqual(Rgb{ 0x68, 0x9d, 0x6a }, s[6]);
+ try std.testing.expectEqual(Rgb{ 0xfb, 0x49, 0x34 }, s[9]);
+ try std.testing.expectEqual(s[2], s[10]);
+ try std.testing.expectEqual(Rgb{ 0x28, 0x28, 0x28 }, s[0]);
+ try std.testing.expectEqual(Rgb{ 0xeb, 0xdb, 0xb2 }, s[7]);
+ try std.testing.expectEqual(Rgb{ 0x92, 0x83, 0x74 }, s[8]);
+ // five of six: pardes derives them
+ try std.testing.expectEqual(null, (try helixOf(a, "[palette]\nred = \"#cc241d\"\ngreen = \"#98971a\"\nyellow = \"#d79921\"\nblue = \"#458588\"\npurple = \"#b16286\"\n")).ansi);
+}
+
+test "B7, zed: the status bar the tag, the active tab its focus, the status colours, a search match as drawn over the page" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var style: std.ArrayList(u8) = .empty;
+ const keys = [_][2][]const u8{
+ .{ "editor.background", "#282c33ff" }, .{ "editor.foreground", "#acb2beff" },
+ .{ "element.selected", "#454a56ff" }, .{ "status_bar.background", "#3b414dff" },
+ .{ "text.muted", "#a9afbcff" }, .{ "text.accent", "#74ade8ff" },
+ .{ "editor.line_number", "#4e5a5f" }, .{ "scrollbar.thumb.border", "#363c46ff" },
+ .{ "scrollbar.thumb.background", "#c8ccd44c" }, .{ "tab.active_background", "#282c33ff" },
+ .{ "error", "#d07277ff" }, .{ "warning", "#dec184ff" },
+ .{ "info", "#74ade8ff" }, .{ "hint", "#788ca6ff" },
+ .{ "created", "#a1c181ff" }, .{ "deleted", "#d07277ff" },
+ .{ "search.match_background", "#74ade866" }, .{ "editor.active_line_number", "#d0d4da" },
+ };
+ for (keys) |k| try style.print(a, "\"{s}\": \"{s}\", ", .{ k[0], k[1] });
+ inline for (ansi) |n| try style.print(a, "\"terminal.ansi.{s}\": \"#101010\", \"terminal.ansi.bright_{s}\": \"#202020\", ", .{ n, n });
+ const json = try std.fmt.allocPrint(a, "{{\"themes\": [{{\"name\": \"One Dark\", \"style\": {{ {s} \"syntax\": {{ \"keyword\": {{\"color\": \"#b477cf\"}}, \"string\": {{\"color\": \"#a1c181\"}}, \"number\": {{\"color\": \"#bf956a\"}}, \"comment\": {{\"color\": \"#5d636f\"}} }} }} }}]}}", .{style.items});
+ var out: std.ArrayList(Theme) = .empty;
+ try readZed(a, std.testing.allocator, "one.json", json, &out);
+ const t = out.items[0];
+ try std.testing.expectEqualStrings("one_dark_zed", t.name);
+ try std.testing.expectEqual(Rgb{ 0x3b, 0x41, 0x4d }, t.tag);
+ try std.testing.expectEqual(Rgb{ 0x28, 0x2c, 0x33 }, t.tag_focus.?);
+ try std.testing.expectEqual(Rgb{ 0xd0, 0x72, 0x77 }, t.@"error".?);
+ try std.testing.expectEqual(Rgb{ 0x78, 0x8c, 0xa6 }, t.hint.?);
+ try std.testing.expectEqual(Rgb{ 0xa1, 0xc1, 0x81 }, t.added.?);
+ try std.testing.expectEqual(Rgb{ 0xd0, 0x72, 0x77 }, t.deleted.?);
+ try std.testing.expectEqual(Rgb{ 0xd0, 0xd4, 0xda }, t.line_number_current.?);
+ // #74ade8 at 0x66 (40 %) over #282c33
+ try std.testing.expectEqual(Rgb{ 0x46, 0x60, 0x7b }, t.hit.?);
+}