summaryrefslogtreecommitdiff
path: root/src/colors.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/colors.zig')
-rw-r--r--src/colors.zig18
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 {