diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/gen_themes.zig | 522 |
1 files changed, 438 insertions, 84 deletions
diff --git a/tools/gen_themes.zig b/tools/gen_themes.zig index b9a9adb0..79a23a19 100644 --- a/tools/gen_themes.zig +++ b/tools/gen_themes.zig @@ -25,7 +25,9 @@ //! acme of its own; the file is otherwise untouched. So are `monokai`, //! `ayu_dark` and `nord` as `<name>_helix.toml`, kept (unlike helix's other //! copies of themes ported here) because helix themes inherit them; those -//! `inherits` lines name the renamed file. Two themes answering to +//! `inherits` lines name the renamed file. A retired theme a kept one +//! inherits from (ayu_dark_helix, hex_steel, nord_helix: DESIGN.md E) sits in +//! vendor/themes/base/, passed as `base=<path>`: read, never rung. Two themes answering to //! one name is a bug (`Theme <name>` would silently pick one), so core/colors.zig //! asserts the ring's names are distinct at comptime. //! @@ -57,26 +59,37 @@ const Rgb = [3]u8; /// code) — core/colors.zig's `fold` is what turns it into a real Theme. const Theme = struct { name: []const u8, - bg: ?Rgb, - fg: ?Rgb, - sel_bg: Rgb, - sel_fg: Rgb, - tag_bg: Rgb, - tag_fg: Rgb, - box: Rgb, - box_dim: Rgb, - kw: Rgb, - str: Rgb, - num: Rgb, + page: ?Rgb, + text: ?Rgb, + selection: Rgb, + selection_text: Rgb, + tag: Rgb, + tag_text: Rgb, + accent: Rgb, + keyword: Rgb, + string: Rgb, + number: Rgb, comment: Rgb, - lineno: Rgb, - scroll_track: Rgb, - scroll_thumb: Rgb, - /// A message's error ink (colors.Roles.err): diagnostic.error's, else - /// error's, else the theme's red (UX E3: neonotte's diagnostic.error is - /// an underline only). - diagnostic_error: ?Rgb = null, - palette: ?[16]Rgb, + line_number: Rgb, + rail: Rgb, + rail_thumb: Rgb, + /// 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, }; pub fn main(init: std.process.Init) !void { @@ -92,12 +105,16 @@ pub fn main(init: std.process.Init) !void { // and the parent may be later in argv (or inherit something itself). var tomls: std.StringHashMapUnmanaged(Toml) = .empty; var stems: std.ArrayList([]const u8) = .empty; - for (args[2..]) |src| { + for (args[2..]) |arg| { + // `base=` a retired theme kept themes inherit from: read, never rung + // (vendor/themes/base/) + const base = std.mem.startsWith(u8, arg, "base="); + const src = if (base) arg["base=".len..] else arg; const text = try std.Io.Dir.cwd().readFileAlloc(io, src, arena, .limited(8 << 20)); if (std.mem.endsWith(u8, src, ".toml")) { const stem = std.fs.path.stem(src); try tomls.put(arena, stem, parseToml(arena, src, text)); - try stems.append(arena, stem); + if (!base) try stems.append(arena, stem); } else if (std.mem.endsWith(u8, src, ".json")) { try readZed(arena, gpa, src, text, &themes); } else { @@ -105,9 +122,20 @@ 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); + // the directory happened to be read in: a family at a time (a name's + // first word, two themes or more), the lone ones after every family, so + // the picker heads each family once (colors.zig `import_heads`) + var families: std.StringHashMapUnmanaged(u32) = .empty; + for (themes.items) |t| (try families.getOrPutValue(arena, family(t.name), 0)).value_ptr.* += 1; + std.mem.sort(Theme, themes.items, &families, byFamily); var dir = try std.Io.Dir.cwd().openDir(io, args[1], .{}); defer dir.close(io); @@ -132,7 +160,14 @@ fn write(io: std.Io, dir: std.Io.Dir, arena: std.mem.Allocator, stem: []const u8 try file.writeStreamingAll(io, text); } -fn byName(_: void, a: Theme, b: Theme) bool { +fn family(name: []const u8) []const u8 { + return name[0 .. std.mem.indexOfScalar(u8, name, '_') orelse name.len]; +} + +fn byFamily(families: *const std.StringHashMapUnmanaged(u32), a: Theme, b: Theme) bool { + const lone_a = families.get(family(a.name)).? < 2; + const lone_b = families.get(family(b.name)).? < 2; + if (lone_a != lone_b) return lone_b; return std.mem.order(u8, a.name, b.name) == .lt; } @@ -142,22 +177,22 @@ fn emit(w: *std.Io.Writer, t: Theme) !void { const info = @typeInfo(Theme).@"struct"; inline for (info.field_names, info.field_types) |name, T| { if (comptime std.mem.eql(u8, name, "name")) continue; - if (comptime std.mem.eql(u8, name, "palette")) continue; - try w.print(" .{s} = ", .{name}); + if (comptime std.mem.eql(u8, name, "ansi")) continue; + try w.print(" .{f} = ", .{std.zig.fmtId(name)}); if (comptime @typeInfo(T) == .optional) { if (@field(t, name)) |c| try rgb(w, c) else try w.writeAll("null"); } else try rgb(w, @field(t, name)); try w.writeAll(",\n"); } - if (t.palette) |p| { - try w.writeAll(" .palette = .{\n"); + if (t.ansi) |p| { + try w.writeAll(" .ansi = .{\n"); for (p) |c| { try w.writeAll(" "); try rgb(w, c); try w.writeAll(",\n"); } try w.writeAll(" },\n"); - } else try w.writeAll(" .palette = null,\n"); + } else try w.writeAll(" .ansi = null,\n"); try w.writeAll("};\n"); } @@ -270,42 +305,47 @@ fn resolve(arena: std.mem.Allocator, tomls: *const std.StringHashMapUnmanaged(To return .{ .path = t.path, .keys = keys, .palette = palette }; } -/// A resolved helix theme mapped onto pardes's fourteen fields. +/// A resolved helix theme mapped onto pardes's fields (core/theme_schema.zig). /// /// EVERY FALLBACK HERE IS HELIX'S OWN RULE, which is why none of them is fatal: /// an unstyled scope wears `ui.text`, an unpainted cell wears `ui.background`, /// and a theme that styles neither is saying "wear whatever the terminal is /// already wearing" (the base16/term16/transparent themes exist precisely to do -/// that). pardes spells that last step as a null bg/fg, so those two pass -/// straight through — but the other twelve fields are RGB and cannot be null, so +/// that). pardes spells that last step as a null page/text, so those two pass +/// straight through — but the other fields are RGB and cannot be null, so /// they need a concrete stand-in for "the terminal's", and that is the default /// palette's ink and page below. Field by field: /// -/// bg ui.background.bg, else null = the host terminal's own cell -/// fg ui.text.fg, else null = ditto -/// box ui.cursor.primary.bg, else ui.cursor.bg, else keyword.fg, +/// page ui.background.bg, else null = the host terminal's own cell +/// text ui.text.fg, else null = ditto +/// accent ui.cursor.primary.bg, else ui.cursor.bg, else keyword.fg, /// else ink — skipping any of them that IS the page (see /// firstNotPage) -/// box_dim, -/// scroll_thumb, -/// sel_bg ui.selection.bg, else ui.selection.primary.bg, else +/// rail_thumb, +/// selection ui.selection.bg, else ui.selection.primary.bg, else /// ui.selection.fg, same page-skipping, else page. No selection /// colour anywhere means the author drew no selection either, -/// and a dim box the colour of the page is the honest rendering -/// of that — no theme in the vendored set actually gets there -/// sel_fg ui.selection.fg, unless that IS sel_bg, else whichever of -/// ink and page the highlight is farther from -/// tag_bg, -/// scroll_track ui.statusline.bg, else page -/// tag_fg ui.statusline.fg, else ink -/// kw/str/comment their scope's fg, else ink (helix: unstyled = ui.text) -/// num constant.numeric.fg, else constant.fg, else ink -/// lineno ui.linenr.fg, else ink +/// and a selection the colour of the page is the honest +/// rendering of that — no theme in the vendored set gets there +/// selection_text ui.selection.fg, unless that IS the selection, else +/// whichever of ink and page the highlight is farther from +/// tag, +/// rail ui.statusline.bg, else page +/// tag_text ui.statusline.fg, else ink +/// keyword/string/comment their scope's fg, else ink (helix: unstyled = ui.text) +/// number constant.numeric.fg, else constant.fg, else ink +/// line_number ui.linenr.fg, else ink fn helixTheme(arena: std.mem.Allocator, stem: []const u8, h: Helix) !Theme { const fg = h.get("ui.text", .fg); 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 @@ -335,41 +375,84 @@ 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. - .bg = bg, - .fg = fg, - .sel_bg = sel, - .sel_fg = if (named_ink) |c| (if (std.mem.eql(u8, &c, &sel)) sel_far else c) else sel_far, - .tag_bg = h.get("ui.statusline", .bg) orelse page, - .tag_fg = h.get("ui.statusline", .fg) orelse ink, - .box = box, - .box_dim = sel, - .kw = h.get("keyword", .fg) orelse ink, - .str = h.get("string", .fg) orelse ink, + .page = bg, + .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 = 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, // most themes only colour `constant`; the ones that split out numbers // are being more specific, so they win - .num = h.first(&.{ .{ "constant.numeric", .fg }, .{ "constant", .fg } }) orelse ink, + .number = h.first(&.{ .{ "constant.numeric", .fg }, .{ "constant", .fg } }) orelse ink, .comment = h.get("comment", .fg) orelse ink, - .lineno = h.get("ui.linenr", .fg) orelse ink, + .line_number = h.get("ui.linenr", .fg) orelse ink, // the scrollbar is chrome, so it takes the chrome colors: the tag bar's // own background for the track, the selection (one step brighter than // anything else in the theme's greys) for the thumb riding it - .scroll_track = h.get("ui.statusline", .bg) orelse page, - .scroll_thumb = sel, - .diagnostic_error = h.first(&.{ .{ "diagnostic.error", .fg }, .{ "error", .fg } }) orelse h.color("red"), - .palette = null, + .rail = h.get("ui.statusline", .bg) orelse page, + .rail_thumb = sel, + // 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 @@ -415,6 +498,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 @@ -555,8 +670,8 @@ fn readZed(arena: std.mem.Allocator, gpa: std.mem.Allocator, path: []const u8, t // name rather than a suffix that appears only on the eight that // happen to clash today .name = try std.fmt.allocPrint(arena, "{s}_zed", .{try slug(arena, (v.get("name") orelse std.process.fatal("{s}: a variant has no `name`", .{path})).string)}), - .bg = z.need("editor.background"), - .fg = z.need("editor.foreground"), + .page = z.need("editor.background"), + .text = z.need("editor.foreground"), // zed's TEXT selection is `players[0].selection`, and it is a 24% // alpha over the page — the colour on screen is the composite, not // the value. pardes paints opaque cells and `hex` drops alpha, so @@ -565,24 +680,33 @@ fn readZed(arena: std.mem.Allocator, gpa: std.mem.Allocator, path: []const u8, t // selected" fill, which is the question actually being asked. The // ink then stays the editor's own text colour, because that fill is // always a step off the background and never off the text. - .sel_bg = z.need("element.selected"), - .sel_fg = z.need("editor.foreground"), - .tag_bg = z.need("status_bar.background"), - .tag_fg = z.need("text.muted"), + .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 - .box = z.need("text.accent"), - .box_dim = z.need("border"), - .kw = z.syn(syntax, "keyword"), - .str = z.syn(syntax, "string"), - .num = z.syn(syntax, "number"), + .accent = z.need("text.accent"), + .keyword = z.syn(syntax, "keyword"), + .string = z.syn(syntax, "string"), + .number = z.syn(syntax, "number"), .comment = z.syn(syntax, "comment"), - .lineno = z.need("editor.line_number"), - .scroll_track = z.need("scrollbar.thumb.border"), - .scroll_thumb = z.need("scrollbar.thumb.background"), + .line_number = z.need("editor.line_number"), + .rail = z.need("scrollbar.thumb.border"), + .rail_thumb = z.need("scrollbar.thumb.background"), // its status colour, else the palette's red (colors.zig) - .diagnostic_error = if (z.style.get("error") != null) z.need("error") else null, - .palette = pal, + .@"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, }); } } @@ -594,6 +718,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) { @@ -612,6 +754,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 @@ -629,3 +844,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.?); +} |
