| 1 | const std = @import("std"); |
| 2 | const Self = @This(); |
| 3 | const color = @import("./mod.zig"); |
| 4 | const _x = @import("./_x.zig"); |
| 5 | const vec4 = @Vector(4, f32); |
| 6 | |
| 7 | y: f32, // [ 0 .. 1 ] |
| 8 | u: f32, // [ -0.436 .. +0.436 ] |
| 9 | v: f32, // [ -0.615 .. +0.615 ] |
| 10 | a: f32, // [ 0 .. 1 ] |
| 11 | |
| 12 | pub fn initYUVA(y: f32, u: f32, v: f32, a: f32) Self { |
| 13 | return Self{ |
| 14 | .y = y, |
| 15 | .u = u, |
| 16 | .v = v, |
| 17 | .a = a, |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | pub fn initYUV(y: f32, u: f32, v: f32) Self { |
| 22 | return initYUVA(y, u, v, 255); |
| 23 | } |
| 24 | |
| 25 | pub fn eql(x: Self, y: Self) bool { |
| 26 | return x.y == y.y and x.u == y.u and x.v == y.v and x.a == y.a; |
| 27 | } |
| 28 | |
| 29 | pub const M = _x.mixin(@This(), f32, .y, .u, .v); |
| 30 | pub const to_vec = M.to_vec; |
| 31 | pub const from_vec = M.from_vec; |
| 32 | pub const to_array = M.to_array; |
| 33 | |
| 34 | // https://web.archive.org/web/20180423091842/http://www.equasys.de/colorconversion.html |
| 35 | pub fn to_srgb(x: Self) color.sRGB { |
| 36 | // zig fmt: off |
| 37 | const y, const u, const v, const a = x.to_array(); |
| 38 | const r = y * 1.000 + u * 0.000 + v * 1.140; |
| 39 | const g = y * 1.000 + u * -0.395 + v * -0.581; |
| 40 | const b = y * 1.000 + u * 2.032 + v * 0.000; |
| 41 | return color.sRGB.from_float(.{ r, g, b, a }); |
| 42 | // zig fmt: on |
| 43 | } |
| 44 | |
| 45 | pub fn normal(x: Self) vec4 { |
| 46 | return x.to_vec() * @as(vec4, .{ 255, 255, 255, 1 }); |
| 47 | } |