1//! object representing a single color in the YCbCr colorspace with channel values from 0-255
2
3const std = @import("std");
4const Self = @This();
5const color = @import("./mod.zig");
6const _x = @import("./_x.zig");
7
8y: u8,
9cb: u8,
10cr: u8,
11a: u8,
12
13pub fn initYCbCrA(y: u8, cb: u8, cr: u8, a: u8) Self {
14 return Self{
15 .y = y,
16 .cb = cb,
17 .cr = cr,
18 .a = a,
19 };
20}
21
22pub fn initYCbCr(y: u8, cb: u8, cr: u8) Self {
23 return initYCbCrA(y, cb, cr, 255);
24}
25
26pub fn eql(x: Self, y: Self) bool {
27 return x.y == y.y and x.cb == y.cb and x.cr == y.cr and x.a == y.a;
28}
29
30const M = _x.mixin(@This(), u8, .y, .cb, .cr);
31pub const to_vec = M.to_vec;
32pub const from_vec = M.from_vec;
33pub const to_array = M.to_array;
34
35pub fn to_srgb(x: Self) color.sRGB {
36 // zig fmt: off
37 const r, const g, const b, const a = x.to_vec();
38 return color.sRGB.from_vec(.{
39 @max(0, @min(255, r + 1.402 * (b-128))),
40 @max(0, @min(255, r - 0.34414 * (g-128) - 0.71414 * (b-128))),
41 @max(0, @min(255, r + 1.772 * (g-128))),
42 @max(0, @min(255, a)),
43 });
44 // zig fmt: on
45}