diff options
Diffstat (limited to 'core/colors.zig')
| -rw-r--r-- | core/colors.zig | 348 |
1 files changed, 344 insertions, 4 deletions
diff --git a/core/colors.zig b/core/colors.zig index ef2d5aae..b2069465 100644 --- a/core/colors.zig +++ b/core/colors.zig @@ -287,8 +287,8 @@ pub const Theme = struct { 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, the - /// page tinted toward the search's ground, so it reads as the body's. + /// 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`. @@ -558,10 +558,11 @@ pub const ChromeTheme = struct { lineno: [3]u8, scroll_track: [3]u8, scroll_thumb: [3]u8, + roles: Roles, pub fn fromTheme(th: *const Theme) ChromeTheme { // The initial chrome is resolved at compile time: contrast takes pow. - @setEvalBranchQuota(200_000); + @setEvalBranchQuota(1_000_000); 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 @@ -570,6 +571,7 @@ pub const ChromeTheme = struct { const name_fg = th.tag_name_fg orelse (derivedNameTint(th, 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)); + const reading = readingOf(th); return .{ .tag_bg = th.tag_bg, .tag_fg = th.tag_fg, @@ -583,7 +585,7 @@ pub const ChromeTheme = struct { .empty_col = th.empty_col orelse separatorOf(th), .search_bg = th.search_bg orelse th.sel_bg, .search_fg = th.search_fg orelse th.sel_fg, - .tag_reading_bg = th.tag_reading_bg orelse mixOklab(th.bg orelse th.tag_bg, th.search_bg orelse th.sel_bg, 0.4), + .tag_reading_bg = reading, .diagnostic_error = th.diagnostic_error orelse (th.fg orelse th.tag_fg), .diagnostic_warning = th.diagnostic_warning orelse (th.fg orelse th.tag_fg), .diagnostic_info = th.diagnostic_info orelse (th.fg orelse th.tag_fg), @@ -595,10 +597,262 @@ pub const ChromeTheme = struct { .lineno = th.lineno, .scroll_track = th.scroll_track, .scroll_thumb = th.scroll_thumb, + .roles = .of(th, th.tag_bg, active_bg, reading), }; } }; +/// A tag's ground in the walk UI (UX DESIGN.md D4): the plain tag's, the +/// focused one's tint, a reading pane's tint, focused or not (its ink is +/// the focused tag's while it has the keyboard). +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 }; + +/// 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, + /// The notice row's (the tagline's ground): its dim words. + notice: Inks, + /// 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, + /// The letters a query matched: an ink, never a ground, 4.5:1 on every + /// tag ground and on the selection. + match: [3]u8, + /// The body's places and a preview's landing line, and the current + /// one (its matched span): grounds off the page, its ink on them 4.5:1. + hit: [3]u8, + hit_current: [3]u8, + /// A message's ink by its kind (Messages.ink): 4.5:1 on the tags and + /// the page. + err: [3]u8, + warn: [3]u8, + question: [3]u8, + hint: [3]u8, + + pub const text_floor: f32 = 4.5; + pub const dim_floor: f32 = 3; + pub const rule_floor: f32 = 1.8; + pub const rule_ceiling: f32 = 2.5; + pub const ink_floor: f32 = 4.5; + pub const hit_floor: f32 = 1.25; + pub const hit_current_floor: f32 = 1.6; + pub const reading_distance: f32 = 0.04; + + pub fn on(r: *const Roles, ground: TagGround) *const Inks { + return &r.tag[@backingInt(ground)]; + } + + pub fn selectedOn(r: *const Roles, ground: TagGround) *const Inks { + return &r.selected[@backingInt(ground)]; + } + + /// The selection's floor off a tag's ground: 1.4:1 on a dark tag, 1.25 + /// on a light one. + pub fn selectionFloor(ground: [3]u8) f32 { + 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 { + 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; + const texts = [4][3]u8{ plain_ink, focused_ink, plain_ink, focused_ink }; + var r: Roles = undefined; + for (&r.tag, &r.selected, grounds, texts) |*inks, *selected, ground, ink| { + inks.* = inksOn(ground, ink); + selected.* = inksOn(selectionOf(th.tag_sel_bg orelse th.sel_bg, ground, inks.text), inks.text); + } + r.notice = inksOn(th.tagline_bg orelse th.tag_bg, th.tagline_fg orelse th.tag_fg); + const tag_grounds = [_][3]u8{ plain, focused, reading, r.selected[0].ground, r.selected[1].ground, r.selected[2].ground, r.selected[3].ground }; + r.key = pushAway(th.box, &tag_grounds, ink_floor); + r.match = pushAway(matchHue(th), &tag_grounds, ink_floor); + const page = th.bg orelse null_page; + const body_ink = th.fg orelse th.tag_fg; + const dark = relativeLuminance(page) < 0.18; + // The theme's search ground where it stands off the page and the + // body reads on it; else the page a little way toward its hue (the + // warm accent's without one). The current one as far past the hit + // as 32 % is past 18 % (38 past 22 on a light page). + const hue = th.search_bg orelse th.num; + const own = th.search_bg != null and contrast(hue, page) >= hit_floor and contrast(body_ink, hue) >= ink_floor; + const at: f32 = if (own) 1 else if (dark) 0.18 else 0.22; + r.hit = if (own) hue else hitOf(page, hue, body_ink, at, hit_floor); + const past: f32 = if (dark) 0.32 / 0.18 else 0.38 / 0.22; + r.hit_current = hitOf(page, hue, body_ink, at * past, hit_current_floor); + // Never fainter than the hit, where the ink holds both short. + if (contrast(r.hit_current, page) < contrast(r.hit, page)) r.hit_current = r.hit; + const message_grounds = [_][3]u8{ plain, focused, reading, page }; + const ink = th.fg orelse th.tag_fg; + r.err = pushAway(th.diagnostic_error orelse ink, &message_grounds, ink_floor); + r.warn = pushAway(th.diagnostic_warning orelse ink, &message_grounds, ink_floor); + r.question = pushAway(th.diagnostic_info orelse ink, &message_grounds, ink_floor); + r.hint = pushAway(th.diagnostic_hint orelse ink, &message_grounds, ink_floor); + return r; + } +}; + +/// 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 { + const text = pushAway(ink, &.{ground}, Roles.text_floor); + return .{ .ground = ground, .text = text, .dim = toward(text, ground, 3.5), .rule = toward(text, ground, 2) }; +} + +/// `ink` mixed toward `ground` (OKLab) by hundredths as far as it still +/// reads at `floor` on it: found by halves (its contrast falls as it nears +/// the ground), seven mixes where a walk took up to a hundred (the first +/// frame's bodyChrome derives them too). +fn toward(ink: [3]u8, ground: [3]u8, floor: f32) [3]u8 { + const a = toOklab(ink); + const b = toOklab(ground); + var best = ink; + var lo: u32 = 0; + var hi: u32 = 101; + while (hi - lo > 1) { + const mid = (lo + hi) / 2; + const t = @as(f32, @floatFromInt(mid)) / 100; + const c = fromOklab(.{ a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t }); + if (contrast(c, ground) >= floor) { + lo = mid; + best = c; + } else hi = mid; + } + return best; +} + +/// `ink` pushed by fiftieths toward white or black, whichever reads better +/// on all its grounds, until it reads at `floor` on every one; where no ink +/// does (solarized's mid-grey tag), that end, the most any ink reaches. +fn pushAway(ink: [3]u8, grounds: []const [3]u8, floor: f32) [3]u8 { + const white: [3]u8 = @splat(255); + const black: [3]u8 = @splat(0); + if (leastContrast(ink, grounds) >= floor) return ink; + const end = if (leastContrast(white, grounds) >= leastContrast(black, grounds)) white else black; + const a = toOklab(ink); + const b = toOklab(end); + var step: f32 = 0; + while (step <= 50) : (step += 1) { + const t = step / 50; + const c = fromOklab(.{ a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t }); + if (leastContrast(c, grounds) >= floor) return c; + } + return end; +} + +fn leastContrast(ink: [3]u8, grounds: []const [3]u8) f32 { + var least: f32 = 21; + for (grounds) |g| least = @min(least, contrast(ink, g)); + return least; +} + +/// 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 { + 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; +} + +/// The selection a band's cursor row is drawn on over `ground`: the +/// theme's where it stands off it, else moved by hundredths toward the +/// text, then away from it, to the first that does with the text still at +/// 4.5:1 on it; where none does (a tag whose text has no contrast to +/// spare), the one furthest off it. +fn selectionOf(own: [3]u8, ground: [3]u8, text: [3]u8) [3]u8 { + var best = own; + var best_off: f32 = 0; + const away: [3]u8 = if (relativeLuminance(text) > relativeLuminance(own)) @splat(0) else @splat(255); + if (contrast(own, ground) >= Roles.selectionFloor(ground) and contrast(text, own) >= Roles.text_floor) return own; + const o = toOklab(own); + for ([2][3]u8{ text, away }) |end| { + const e = toOklab(end); + var step: f32 = 0; + while (step <= 100) : (step += 1) { + const t = step / 100; + const c = fromOklab(.{ o[0] + (e[0] - o[0]) * t, o[1] + (e[1] - o[1]) * t, o[2] + (e[2] - o[2]) * t }); + if (contrast(text, c) < Roles.text_floor) break; + const off = contrast(c, ground) / Roles.selectionFloor(ground); + if (off >= 1) return c; + if (off > best_off) { + best = c; + best_off = off; + } + } + } + return best; +} + +/// A ground off `page` toward `hue` at `t` (past 1, beyond the hue, as far +/// as the gamut goes), held where the body's `ink` reads on it at 4.5:1 +/// (or its own on the page, where that is less); moved on toward the ink +/// until it stands off the page at `floor`, as far as the ink allows. +fn hitOf(page: [3]u8, hue: [3]u8, ink: [3]u8, t: f32, floor: f32) [3]u8 { + const a = toOklab(page); + const b = toOklab(hue); + const ink_floor = @min(Roles.ink_floor, contrast(ink, page)); + var k = t; + var c = fromOklab(.{ a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k }); + while (contrast(ink, c) < ink_floor and k > 0) { + k -= t / 50; + c = fromOklab(.{ a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k }); + } + const start = c; + var step: f32 = 1; + while (contrast(c, page) < floor and step <= 50) : (step += 1) { + const next = mixOklab(start, ink, step / 100); + if (contrast(ink, next) < ink_floor) break; + c = next; + } + return c; +} + +/// A reading pane's tag (UX DESIGN.md D4): the theme's own where it stands +/// off the tag, else the tag shifted toward paper yellow by 0.04 in OKLab +/// and lighter by half that on a dark tag (darker on a light), so it reads +/// 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 { + 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 }); + const da = paper[1] - tag[1]; + const db = paper[2] - tag[2]; + const n = @max(@sqrt(da * da + db * db), 1e-6); + const sign: f32 = if (relativeLuminance(th.tag_bg) < 0.18) 1 else -1; + // Away from the text's lightness, where the first lean costs it. + const away: f32 = if (relativeLuminance(th.tag_fg) > relativeLuminance(th.tag_bg)) -1 else 1; + const inks = [2][3]u8{ th.tag_fg, th.tag_active_fg orelse th.tag_fg }; + var best = th.tag_bg; + var best_text: f32 = -21; + var reach = Roles.reading_distance; + while (reach <= 0.2) : (reach *= 1.25) { + for ([_]f32{ sign * 0.5, 0, away * 0.5, away, away * 1.5 }) |lean| { + const c = fromOklab(.{ tag[0] + lean * reach, tag[1] + da / n * reach, tag[2] + db / n * reach }); + if (oklabDistance(toOklab(c), tag) < Roles.reading_distance) continue; + // Each ink by its own floor: 7:1, or what it has on the tag. + var text: f32 = 21; + for (inks) |ink| text = @min(text, contrast(ink, c) - (@min(7, contrast(ink, th.tag_bg)) - 0.01)); + if (text >= 0) return c; + if (text > best_text) { + best = c; + best_text = text; + } + } + } + return best; +} + pub const initial_chrome = ChromeTheme.fromTheme(&themes[0]); fn nextThemeFileGeneration(p: *Pardes) u32 { @@ -1281,3 +1535,89 @@ test "every theme tints its names off the tag's ink, and they read on both groun } } } + +test "every theme's walk UI roles meet their floors on every ground they are drawn on, dark and light" { + // Where a theme's own colours leave no room for a floor, the most it + // allows: an ink's on a mid-grey tag (solarized's) is the better of + // white and black there; a ground's is where the ink it carries would + // drop under its floor. + const white: [3]u8 = @splat(255); + const black: [3]u8 = @splat(0); + const Reach = struct { + fn of(grounds: []const [3]u8) f32 { + return @max(leastContrast(white, grounds), leastContrast(black, grounds)); + } + }; + const a = std.testing.allocator; + const Check = struct { what: []const u8, got: f32, floor: f32 }; + var checks: std.ArrayList(Check) = .empty; + defer checks.deinit(a); + var failed: usize = 0; + for (&themes) |*th| { + checks.clearRetainingCapacity(); + const chrome = ChromeTheme.fromTheme(th); + const r = &chrome.roles; + const page = th.bg orelse null_page; + 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| { + 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) }); + try checks.append(a, .{ .what = "dim under text", .got = text - contrast(inks.dim, inks.ground), .floor = 0 }); + } + try checks.append(a, .{ .what = "key", .got = leastContrast(r.key, &tags), .floor = @min(Roles.ink_floor, Reach.of(&tags)) }); + try checks.append(a, .{ .what = "match", .got = leastContrast(r.match, &tags), .floor = @min(Roles.ink_floor, Reach.of(&tags)) }); + for (r.tag) |inks| { + try checks.append(a, .{ .what = "rule", .got = contrast(inks.rule, inks.ground), .floor = Roles.rule_floor }); + try checks.append(a, .{ .what = "rule ceiling", .got = Roles.rule_ceiling - contrast(inks.rule, inks.ground), .floor = 0 }); + } + for ([_][3]u8{ r.err, r.warn, r.question, r.hint }) |ink| + try checks.append(a, .{ .what = "message", .got = leastContrast(ink, &messages), .floor = @min(Roles.ink_floor, Reach.of(&messages)) }); + // A selection off its tag by 1.4:1 (1.25 light) with the text at + // 4.5:1 on it, where the text has that much over the tag. + for (r.tag, r.selected) |inks, selected| { + const floor = Roles.selectionFloor(inks.ground); + const room = contrast(inks.text, inks.ground) >= floor * Roles.text_floor * 1.03; + try checks.append(a, .{ .what = "selection", .got = contrast(selected.ground, inks.ground), .floor = if (room) floor else 0 }); + } + try checks.append(a, .{ .what = "reading", .got = oklabDistance(toOklab(chrome.tag_reading_bg), toOklab(th.tag_bg)), .floor = Roles.reading_distance }); + for ([_][3]u8{ th.tag_fg, th.tag_active_fg orelse th.tag_fg }) |ink| + try checks.append(a, .{ .what = "text on reading", .got = contrast(ink, chrome.tag_reading_bg), .floor = @min(7, contrast(ink, th.tag_bg)) - 0.02 }); + const ink_floor = @min(Roles.ink_floor, contrast(body_ink, page)); + for ([_][3]u8{ r.hit, r.hit_current }, [_]f32{ Roles.hit_floor, Roles.hit_current_floor }) |ground, floor| { + const on = contrast(body_ink, ground); + try checks.append(a, .{ .what = "hit", .got = contrast(ground, page), .floor = if (on < ink_floor + 0.3) 0 else floor }); + try checks.append(a, .{ .what = "ink on hit", .got = on, .floor = ink_floor - 0.01 }); + } + try checks.append(a, .{ .what = "current over hit", .got = contrast(r.hit_current, page) - contrast(r.hit, page), .floor = 0 }); + for (checks.items) |c| if (c.got < c.floor - 0.005) { + failed += 1; + std.debug.print("{s}: {s} {d:.2} under {d:.2}\n", .{ th.name, c.what, c.got, c.floor }); + }; + } + 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 }; + 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 = .{ 0x36, 0x28, 0x2a }, .hit_current = .{ 0x52, 0x38, 0x31 } }, + // 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 = .{ 0xec, 0xd8, 0xbb }, .hit_current = .{ 0xdd, 0xbc, 0x99 } }, + }; + 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 }; + 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 }); + return error.TestUnexpectedResult; + }; + } +} |
