summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/colors.zig18
-rw-r--r--src/draw.zig39
2 files changed, 21 insertions, 36 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 {
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);
};
}
}