const std = @import("std"); pub fn labF(t_: f64) f64 { const d = 6.0 / 29.0; return if (t_ > d * d * d) std.math.pow(f64, t_, 1.0 / 3.0) else t_ / (3.0 * d * d) + 4.0 / 29.0; } pub fn labFInv(t_: f64) f64 { const d = 6.0 / 29.0; return if (t_ > d) t_ * t_ * t_ else 3.0 * d * d * (t_ - 4.0 / 29.0); } pub fn linearToByte(v: f64) u8 { const c = std.math.clamp(v, 0.0, 1.0); const s = if (c <= 0.0031308) c * 12.92 else 1.055 * std.math.pow(f64, c, 1.0 / 2.4) - 0.055; return byteRound(std.math.clamp(s, 0.0, 1.0) * 255.0); } // D65 reference white. pub const xn = 0.95047; pub const yn = 1.0; pub const zn = 1.08883; pub fn byteRound(f: f64) u8 { return @intFromFloat(@round(std.math.clamp(f, 0.0, 255.0))); } pub fn byteTrunc(f: f64) u8 { return @intFromFloat(std.math.clamp(f, 0.0, 255.0)); } pub fn srgbToLinear(c: u8) f64 { const v = @as(f64, @floatFromInt(c)) / 255.0; return if (v <= 0.04045) v / 12.92 else std.math.pow(f64, (v + 0.055) / 1.055, 2.4); }