| 1 | //! object representing a single color in the YCbCr colorspace with channel values from 0-255 |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const Self = @This(); |
| 5 | const color = @import("./mod.zig"); |
| 6 | const _x = @import("./_x.zig"); |
| 7 | |
| 8 | y: u8, |
| 9 | cb: u8, |
| 10 | cr: u8, |
| 11 | a: u8, |
| 12 | |
| 13 | pub 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 | |
| 22 | pub fn initYCbCr(y: u8, cb: u8, cr: u8) Self { |
| 23 | return initYCbCrA(y, cb, cr, 255); |
| 24 | } |
| 25 | |
| 26 | pub 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 | |
| 30 | const M = _x.mixin(@This(), u8, .y, .cb, .cr); |
| 31 | pub const to_vec = M.to_vec; |
| 32 | pub const from_vec = M.from_vec; |
| 33 | pub const to_array = M.to_array; |
| 34 | |
| 35 | pub 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 | } |