authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-12-30 02:05:10 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-12-30 02:05:10 -08:00
log51da26f311b7e60ae65117414f02841c83dd69f5
tree8cbb12da2335da01f720e67e997b44851ba722d2
parent64cfdaabc303a26095c5b5f9146f87ba6997a031

tidy


3 files changed, 15 insertions(+), 4 deletions(-)

LinearRgb.zig+3-2
...@@ -30,14 +30,15 @@ pub fn eql(x: Self, y: Self) bool {...@@ -30,14 +30,15 @@ pub fn eql(x: Self, y: Self) bool {
3030
31// https://www.w3.org/TR/WCAG/#dfn-relative-luminance31// https://www.w3.org/TR/WCAG/#dfn-relative-luminance
32pub fn relative_luminance(x: Self) f32 {32pub fn relative_luminance(x: Self) f32 {
33 return (0.2126 * x.r) + (0.7152 * x.g) + (0.0722 * x.b);33 const r, const g, const b, _ = x.to_array();
34 return (0.2126 * r) + (0.7152 * g) + (0.0722 * b);
34}35}
3536
36pub usingnamespace _x.mixin(@This(), f32, .r, .g, .b);37pub usingnamespace _x.mixin(@This(), f32, .r, .g, .b);
3738
38// https://gamedev.stackexchange.com/a/19403839// https://gamedev.stackexchange.com/a/194038
39pub fn to_srgb(x: Self) color.sRGB {40pub fn to_srgb(x: Self) color.sRGB {
40 const v: vec4 = x.to_array();41 const v = x.to_vec();
41 const cutoff = v < @as(vec4, @splat(0.0031308));42 const cutoff = v < @as(vec4, @splat(0.0031308));
42 const higher = @as(vec4, @splat(1.055)) * std.math.pow(vec4, v, @splat(1.0 / 2.4)) - @as(vec4, @splat(0.055));43 const higher = @as(vec4, @splat(1.055)) * std.math.pow(vec4, v, @splat(1.0 / 2.4)) - @as(vec4, @splat(0.055));
43 const lower = v * @as(vec4, @splat(12.92));44 const lower = v * @as(vec4, @splat(12.92));
YUV.zig+1-2
...@@ -40,6 +40,5 @@ pub fn to_srgb(x: Self) color.sRGB {...@@ -40,6 +40,5 @@ pub fn to_srgb(x: Self) color.sRGB {
40}40}
4141
42pub fn normal(x: Self) vec4 {42pub fn normal(x: Self) vec4 {
43 const v: vec4 = x.to_array();43 return x.to_vec() * @as(vec4, .{ 255, 255, 255, 1 });
44 return v * @as(vec4, .{ 255, 255, 255, 1 });
45}44}
_x.zig+11
...@@ -9,6 +9,9 @@ pub fn mixin(...@@ -9,6 +9,9 @@ pub fn mixin(
9) type {9) type {
10 return struct {10 return struct {
11 pub fn to_vec(x: T) @Vector(4, f32) {11 pub fn to_vec(x: T) @Vector(4, f32) {
12 if (I == f32) {
13 return x.to_array();
14 }
12 return .{15 return .{
13 @floatFromInt(@field(x, @tagName(f1))),16 @floatFromInt(@field(x, @tagName(f1))),
14 @floatFromInt(@field(x, @tagName(f2))),17 @floatFromInt(@field(x, @tagName(f2))),
...@@ -18,6 +21,14 @@ pub fn mixin(...@@ -18,6 +21,14 @@ pub fn mixin(
18 }21 }
1922
20 pub fn from_vec(in: @Vector(4, f32)) T {23 pub fn from_vec(in: @Vector(4, f32)) T {
24 if (I == f32) {
25 var x: T = undefined;
26 @field(x, @tagName(f1)) = in[0];
27 @field(x, @tagName(f2)) = in[1];
28 @field(x, @tagName(f3)) = in[2];
29 x.a = in[3];
30 return x;
31 }
21 var x: T = undefined;32 var x: T = undefined;
22 @field(x, @tagName(f1)) = @intFromFloat(in[0]);33 @field(x, @tagName(f1)) = @intFromFloat(in[0]);
23 @field(x, @tagName(f2)) = @intFromFloat(in[1]);34 @field(x, @tagName(f2)) = @intFromFloat(in[1]);