diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-05 00:28:27 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:21:29 -0300 |
| commit | 0f3803afd6596391e4c105a45274c04024d9e4c5 (patch) | |
| tree | b32e336c0578b0ee600ea2b085a54b78d5b00a01 /src/colors.zig | |
| parent | 6f8960651ffac4b9858da2af09b16792923f0f4c (diff) | |
| download | pardes-0f3803afd6596391e4c105a45274c04024d9e4c5.tar.gz pardes-0f3803afd6596391e4c105a45274c04024d9e4c5.zip | |
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/colors.zig')
| -rw-r--r-- | src/colors.zig | 18 |
1 files changed, 12 insertions, 6 deletions
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 { |
