1const std = @import("std");
2const Self = @This();
3const color = @import("./mod.zig");
4const _x = @import("./_x.zig");
5const vec4 = @Vector(4, f32);
6
7y: f32, // [ 0 .. 1 ]
8u: f32, // [ -0.436 .. +0.436 ]
9v: f32, // [ -0.615 .. +0.615 ]
10a: f32, // [ 0 .. 1 ]
11
12pub 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
21pub fn initYUV(y: f32, u: f32, v: f32) Self {
22 return initYUVA(y, u, v, 255);
23}
24
25pub 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
29pub const M = _x.mixin(@This(), f32, .y, .u, .v);
30pub const to_vec = M.to_vec;
31pub const from_vec = M.from_vec;
32pub const to_array = M.to_array;
33
34// https://web.archive.org/web/20180423091842/http://www.equasys.de/colorconversion.html
35pub 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
45pub fn normal(x: Self) vec4 {
46 return x.to_vec() * @as(vec4, .{ 255, 255, 255, 1 });
47}