authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-14 19:47:08 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-14 19:47:08 -07:00
log91c035aab08e631ab7592635ae945c4921cfc696
tree0914550612d962c87f25ee62df7318e5c4c686cb
parent1b72f3d7d8b744aa3fe9646698237f8e93f23dcc

add CMYK


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
3const std = @import("std");
4const Self = @This();
5
6c: f32, // cyan
7m: f32, // magenta
8y: f32, // yellow
9k: f32, // black
10a: f32, // alpha
11
12pub 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
27pub 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();
33
44pub const sRGB = @import("./sRGB.zig");
55pub const LinearRgb = @import("./LinearRgb.zig");
6pub const CMYK = @import("./CMYK.zig");
sRGB.zig+10
......@@ -97,3 +97,13 @@ pub fn to_float(x: Self) Float {
9797 .a = @as(f32, @floatFromInt(x.a)) / 255.0,
9898 };
9999}
100
101pub 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}