| author | |
| committer | |
| log | 2a7d141ddb46bb1ef5dd5d1bb9f82152807ad650 |
| tree | 728efae9725c40250902a06891bd248bcb8e0915 |
| parent | 80ac29e7cdcfdc1728c92047ea571cb01dc9f626 |
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 | ||
| 3 | const std = @import("std"); | |
| 4 | const Self = @This(); | |
| 5 | ||
| 6 | h: f32, // hue | |
| 7 | s: f32, // saturation | |
| 8 | v: f32, // value | |
| 9 | a: f32, // alpha | |
| 10 | ||
| 11 | pub 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 | ||
| 24 | pub 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"); |
| 5 | 5 | pub const LinearRgb = @import("./LinearRgb.zig"); |
| 6 | 6 | pub const CMYK = @import("./CMYK.zig"); |
| 7 | 7 | pub const HSL = @import("./HSL.zig"); |
| 8 | pub const HSV = @import("./HSV.zig"); |
sRGB.zig+18| ... | ... | @@ -126,3 +126,21 @@ pub fn to_hsl(x: Self) color.HSL { |
| 126 | 126 | const a = f.a; |
| 127 | 127 | return color.HSL.initHSLA(h, s, l, a); |
| 128 | 128 | } |
| 129 | ||
| 130 | pub 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 | } |