From 0f3803afd6596391e4c105a45274c04024d9e4c5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 00:28:27 -0300 Subject: colors: the sRGB-to-linear table is colors' (to_linear), and relativeLuminance reads it where it took a pow per channel, so draw's own luminance and contrast go and its blend reads colors.to_linear; the tests, all 113 tty snapshots and the window's 17 golden frames (gui-golden, check) come out the same Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/draw.zig | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) (limited to 'src/draw.zig') diff --git a/src/draw.zig b/src/draw.zig index 5b8377e0..b0fede3b 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -367,28 +367,7 @@ fn inactiveDim(p: *const Pardes) u8 { } fn darkPage(p: *const Pardes) bool { - return if (p.theme().bg) |bg| luminance(bg) < 0.18 else true; -} - -/// sRGB channel to linear light. -const linear: [256]f32 = blk: { - @setEvalBranchQuota(100_000); - var table: [256]f32 = undefined; - for (&table, 0..) |*out, i| { - const c = @as(f64, @floatFromInt(i)) / 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; -}; - -fn luminance(rgb: [3]u8) f32 { - return 0.2126 * linear[rgb[0]] + 0.7152 * linear[rgb[1]] + 0.0722 * linear[rgb[2]]; -} - -fn contrast(a: [3]u8, b: [3]u8) f32 { - const x = luminance(a); - const y = luminance(b); - return (@max(x, y) + 0.05) / (@min(x, y) + 0.05); + return if (p.theme().bg) |bg| colors.relativeLuminance(bg) < 0.18 else true; } /// Pane `id`'s text, `amount` (0 to 1) of the way to its ground, on the grid @@ -445,14 +424,14 @@ const Memo = struct { if (memo.keys[slot]) |held| if (std.mem.eql(u8, &held, &key)) return memo.values[slot]; // Contrast falls as the ink nears its paper: the most of `amount` // that keeps the pair at its own contrast or 4.5, whichever is lower. - const floor = @min(contrast(ink, paper), 4.5); + const floor = @min(colors.contrast(ink, paper), 4.5); var t = amount; - if (contrast(blend(ink, paper, t), paper) < floor) { + if (colors.contrast(blend(ink, paper, t), paper) < floor) { var lo: f32 = 0; var hi: f32 = t; for (0..8) |_| { const mid = (lo + hi) / 2; - if (contrast(blend(ink, paper, mid), paper) >= floor) lo = mid else hi = mid; + if (colors.contrast(blend(ink, paper, mid), paper) >= floor) lo = mid else hi = mid; } t = lo; } @@ -487,7 +466,7 @@ fn fadeStyle(style: *CellStyle, default_fg: ?[3]u8, default_bg: ?[3]u8, palette: fn blend(from: [3]u8, to: [3]u8, t: f32) [3]u8 { var out: [3]u8 = undefined; for (&out, from, to) |*o, a, b| { - const l = linear[a] * (1 - t) + linear[b] * t; + const l = colors.to_linear[a] * (1 - t) + colors.to_linear[b] * t; o.* = encoded[@intFromFloat(@round(std.math.clamp(l, 0, 1) * (encoded.len - 1)))]; } return out; @@ -1457,7 +1436,7 @@ test "an unfocused grip shows on its tag in the grid, in every native theme" { const s = try p.render(arena.allocator()); const r = p.rects[0]; const grip = s.at(r.x, p.tagTop(p.panes[0].?, r)).style.bg.rgb; - const ratio = contrast(grip, p.chromeTheme().tag_bg); + const ratio = colors.contrast(grip, p.chromeTheme().tag_bg); // ponytail: dark and helix have always had a faint grip (1.2:1); flagged, not changed here. const floor: f32 = if (std.mem.eql(u8, theme.name, "dark") or std.mem.eql(u8, theme.name, "helix")) 1.2 else 1.5; if (ratio < floor) std.debug.print("{s}: grip {d:.2}\n", .{ theme.name, ratio }); @@ -1774,7 +1753,7 @@ fn glyphContrast(p: *Pardes, s: *Surface, id: usize, g: u8) f32 { if (cell.style.dim) for (&fg) |*channel| { channel.* = @intCast(@as(u16, channel.*) * 6 / 10); }; - return contrast(fg, bg); + return colors.contrast(fg, bg); } } unreachable; @@ -1853,8 +1832,8 @@ test "a dim never takes a pair below its own contrast or 4.5" { }; var memo: Memo = .{}; fadeStyle(&style, null, null, null, 1, &memo); - const after = contrast(style.fg.rgb, style.bg.rgb); - try std.testing.expect(after >= @min(contrast(ink, bg), 4.5) - 2e-2); + const after = colors.contrast(style.fg.rgb, style.bg.rgb); + try std.testing.expect(after >= @min(colors.contrast(ink, bg), 4.5) - 2e-2); }; } } -- cgit v1.3