authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-14 20:23:22 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-14 20:23:22 -07:00
log2a7d141ddb46bb1ef5dd5d1bb9f82152807ad650
tree728efae9725c40250902a06891bd248bcb8e0915
parent80ac29e7cdcfdc1728c92047ea571cb01dc9f626

add HSV


3 files changed, 45 insertions(+), 0 deletions(-)

HSV.zig created+26
......@@ -0,0 +1,26 @@
1//! object representing a single color in the HSV colorspace with h channel value from 0-360 and s,v channel values from 0-1
2
3const std = @import("std");
4const Self = @This();
5
6h: f32, // hue
7s: f32, // saturation
8v: f32, // value
9a: f32, // alpha
10
11pub fn initHSVA(h: f32, s: f32, v: f32, a: f32) Self {
12 std.debug.assert(h >= 0.0 and h <= 1.0);
13 std.debug.assert(s >= 0.0 and s <= 1.0);
14 std.debug.assert(v >= 0.0 and v <= 1.0);
15 std.debug.assert(a >= 0.0 and a <= 1.0);
16 return Self{
17 .h = h,
18 .s = s,
19 .v = v,
20 .a = a,
21 };
22}
23
24pub fn eql(x: Self, y: Self) bool {
25 return x.h == y.h and x.s == y.s and x.v == y.v and x.a == y.a;
26}
mod.zig+1
......@@ -5,3 +5,4 @@ pub const sRGB = @import("./sRGB.zig");
55pub const LinearRgb = @import("./LinearRgb.zig");
66pub const CMYK = @import("./CMYK.zig");
77pub const HSL = @import("./HSL.zig");
8pub const HSV = @import("./HSV.zig");
sRGB.zig+18
......@@ -126,3 +126,21 @@ pub fn to_hsl(x: Self) color.HSL {
126126 const a = f.a;
127127 return color.HSL.initHSLA(h, s, l, a);
128128}
129
130pub fn to_hsv(x: Self) color.HSV {
131 const f = x.to_float();
132 const cmax = @max(f.r, f.g, f.b);
133 const cmin = @min(f.r, f.g, f.b);
134 const delta = cmax - cmin;
135 const h = blk: {
136 if (delta == 0) break :blk 0;
137 if (cmax == f.r) break :blk ((((f.g - f.b) / delta) % 6) * 60) % 360;
138 if (cmax == f.g) break :blk ((((f.b - f.r) / delta) + 2) * 60) % 360;
139 if (cmax == f.b) break :blk ((((f.r - f.g) / delta) + 4) * 60) % 360;
140 unreachable;
141 };
142 const s = if (cmax == 0) 0 else delta / cmax;
143 const v = cmax;
144 const a = f.a;
145 return color.HSV.initHSVA(h, s, v, a);
146}