| author | |
| committer | |
| log | 51da26f311b7e60ae65117414f02841c83dd69f5 |
| tree | 8cbb12da2335da01f720e67e997b44851ba722d2 |
| parent | 64cfdaabc303a26095c5b5f9146f87ba6997a031 |
3 files changed, 15 insertions(+), 4 deletions(-)
LinearRgb.zig+3-2| ... | ... | @@ -30,14 +30,15 @@ pub fn eql(x: Self, y: Self) bool { |
| 30 | 30 | |
| 31 | 31 | // https://www.w3.org/TR/WCAG/#dfn-relative-luminance |
| 32 | 32 | pub 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 | } |
| 35 | 36 | |
| 36 | 37 | pub usingnamespace _x.mixin(@This(), f32, .r, .g, .b); |
| 37 | 38 | |
| 38 | 39 | // https://gamedev.stackexchange.com/a/194038 |
| 39 | 40 | pub fn to_srgb(x: Self) color.sRGB { |
| 40 | const v: vec4 = x.to_array(); | |
| 41 | const v = x.to_vec(); | |
| 41 | 42 | const cutoff = v < @as(vec4, @splat(0.0031308)); |
| 42 | 43 | const higher = @as(vec4, @splat(1.055)) * std.math.pow(vec4, v, @splat(1.0 / 2.4)) - @as(vec4, @splat(0.055)); |
| 43 | 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 | 40 | } |
| 41 | 41 | |
| 42 | 42 | pub fn normal(x: Self) vec4 { |
| 43 | const v: vec4 = x.to_array(); | |
| 44 | return v * @as(vec4, .{ 255, 255, 255, 1 }); | |
| 43 | return x.to_vec() * @as(vec4, .{ 255, 255, 255, 1 }); | |
| 45 | 44 | } |
_x.zig+11| ... | ... | @@ -9,6 +9,9 @@ pub fn mixin( |
| 9 | 9 | ) type { |
| 10 | 10 | return struct { |
| 11 | 11 | pub fn to_vec(x: T) @Vector(4, f32) { |
| 12 | if (I == f32) { | |
| 13 | return x.to_array(); | |
| 14 | } | |
| 12 | 15 | return .{ |
| 13 | 16 | @floatFromInt(@field(x, @tagName(f1))), |
| 14 | 17 | @floatFromInt(@field(x, @tagName(f2))), |
| ... | ... | @@ -18,6 +21,14 @@ pub fn mixin( |
| 18 | 21 | } |
| 19 | 22 | |
| 20 | 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 | 32 | var x: T = undefined; |
| 22 | 33 | @field(x, @tagName(f1)) = @intFromFloat(in[0]); |
| 23 | 34 | @field(x, @tagName(f2)) = @intFromFloat(in[1]); |