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/colors.zig | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src/colors.zig') diff --git a/src/colors.zig b/src/colors.zig index 6077afce..fc288194 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -16,14 +16,20 @@ const MAX_PANES = pardes.MAX_PANES; const BOX_H = pardes.BOX_H; const Pardes = pardes.Pardes; +/// sRGB channel to linear light. +pub const to_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; +}; + /// WCAG relative luminance, and the contrast of two colours. pub fn relativeLuminance(rgb: [3]u8) f32 { - var l: [3]f32 = undefined; - for (&l, rgb) |*out, v| { - const x = @as(f32, @floatFromInt(v)) / 255; - out.* = if (x <= 0.04045) x / 12.92 else std.math.pow(f32, (x + 0.055) / 1.055, 2.4); - } - return 0.2126 * l[0] + 0.7152 * l[1] + 0.0722 * l[2]; + return 0.2126 * to_linear[rgb[0]] + 0.7152 * to_linear[rgb[1]] + 0.0722 * to_linear[rgb[2]]; } pub fn contrast(a: [3]u8, b: [3]u8) f32 { -- cgit v1.3