| 1 | //! object representing a single color in the CMYK colorspace with channel values from 0-1 |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const Self = @This(); |
| 5 | const color = @import("./mod.zig"); |
| 6 | |
| 7 | c: f32, // cyan [ 0 .. 1 ] |
| 8 | m: f32, // magenta [ 0 .. 1 ] |
| 9 | y: f32, // yellow [ 0 .. 1 ] |
| 10 | k: f32, // black [ 0 .. 1 ] |
| 11 | a: f32, // alpha [ 0 .. 1 ] |
| 12 | |
| 13 | pub fn initCMYKA(c: f32, m: f32, y: f32, k: f32, a: f32) Self { |
| 14 | std.debug.assert(c >= 0.0 and c <= 1.0); |
| 15 | std.debug.assert(m >= 0.0 and m <= 1.0); |
| 16 | std.debug.assert(y >= 0.0 and y <= 1.0); |
| 17 | std.debug.assert(k >= 0.0 and k <= 1.0); |
| 18 | std.debug.assert(a >= 0.0 and a <= 1.0); |
| 19 | return Self{ |
| 20 | .c = c, |
| 21 | .m = m, |
| 22 | .y = y, |
| 23 | .k = k, |
| 24 | .a = a, |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | pub fn eql(x: Self, y: Self) bool { |
| 29 | return x.c == y.c and x.m == y.m and x.y == y.y and x.k == y.k and x.a == y.a; |
| 30 | } |
| 31 | |
| 32 | pub fn to_array(x: Self) [5]f32 { |
| 33 | return .{ |
| 34 | x.c, |
| 35 | x.m, |
| 36 | x.y, |
| 37 | x.k, |
| 38 | x.a, |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | // https://www.rapidtables.com/convert/color/cmyk-to-rgb.html |
| 43 | pub fn to_srgb(x: Self) color.sRGB { |
| 44 | const c, const m, const y, const k, const a = x.to_array(); |
| 45 | const r = (1 - c) * (1 - k); |
| 46 | const g = (1 - m) * (1 - k); |
| 47 | const b = (1 - y) * (1 - k); |
| 48 | return color.sRGB.from_float(.{ r, g, b, a }); |
| 49 | } |