| author | |
| committer | |
| log | 91c035aab08e631ab7592635ae945c4921cfc696 |
| tree | 0914550612d962c87f25ee62df7318e5c4c686cb |
| parent | 1b72f3d7d8b744aa3fe9646698237f8e93f23dcc |
3 files changed, 40 insertions(+), 0 deletions(-)
CMYK.zig created+29| ... | @@ -0,0 +1,29 @@ | ||
| 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 | |||
| 6 | c: f32, // cyan | ||
| 7 | m: f32, // magenta | ||
| 8 | y: f32, // yellow | ||
| 9 | k: f32, // black | ||
| 10 | a: f32, // alpha | ||
| 11 | |||
| 12 | pub fn initCMYKA(c: f32, m: f32, y: f32, k: f32, a: f32) Self { | ||
| 13 | std.debug.assert(c >= 0.0 and c <= 1.0); | ||
| 14 | std.debug.assert(m >= 0.0 and m <= 1.0); | ||
| 15 | std.debug.assert(y >= 0.0 and y <= 1.0); | ||
| 16 | std.debug.assert(k >= 0.0 and k <= 1.0); | ||
| 17 | std.debug.assert(a >= 0.0 and a <= 1.0); | ||
| 18 | return Self{ | ||
| 19 | .c = c, | ||
| 20 | .m = m, | ||
| 21 | .y = y, | ||
| 22 | .k = k, | ||
| 23 | .a = a, | ||
| 24 | }; | ||
| 25 | } | ||
| 26 | |||
| 27 | pub fn eql(x: Self, y: Self) bool { | ||
| 28 | 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; | ||
| 29 | } | ||
mod.zig+1| ... | @@ -3,3 +3,4 @@ const color = @This(); | ... | @@ -3,3 +3,4 @@ const color = @This(); |
| 3 | 3 | ||
| 4 | pub const sRGB = @import("./sRGB.zig"); | 4 | pub const sRGB = @import("./sRGB.zig"); |
| 5 | pub const LinearRgb = @import("./LinearRgb.zig"); | 5 | pub const LinearRgb = @import("./LinearRgb.zig"); |
| 6 | pub const CMYK = @import("./CMYK.zig"); |
sRGB.zig+10| ... | @@ -97,3 +97,13 @@ pub fn to_float(x: Self) Float { | ... | @@ -97,3 +97,13 @@ pub fn to_float(x: Self) Float { |
| 97 | .a = @as(f32, @floatFromInt(x.a)) / 255.0, | 97 | .a = @as(f32, @floatFromInt(x.a)) / 255.0, |
| 98 | }; | 98 | }; |
| 99 | } | 99 | } |
| 100 | |||
| 101 | pub fn to_cmyk(x: Self) color.CMYK { | ||
| 102 | const f = x.to_float(); | ||
| 103 | const k = 1 - @max(f.r, f.g, f.b); | ||
| 104 | const c = (1 - f.r - k) / (1 - k); | ||
| 105 | const m = (1 - f.g - k) / (1 - k); | ||
| 106 | const y = (1 - f.b - k) / (1 - k); | ||
| 107 | const a = f.a; | ||
| 108 | return color.CMYK.initCMYKA(c, m, y, k, a); | ||
| 109 | } |