authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-12-30 02:05:41 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-12-30 02:05:41 -08:00
logf62caddf9f04c739245423ea068f40ce335514e9
treee0f94f2096ed22df99e868c24291b0aa66aa6b05
parent51da26f311b7e60ae65117414f02841c83dd69f5

use destructure more in srgb


1 files changed, 19 insertions(+), 34 deletions(-)

sRGB.zig+19-34
......@@ -11,13 +11,6 @@ g: u8, // green value
1111b: u8, // blue value
1212a: u8, // alpha value
1313
14pub const Float = struct {
15 r: f32,
16 g: f32,
17 b: f32,
18 a: f32,
19};
20
2114pub fn initRGBA(r: u8, g: u8, b: u8, a: u8) Self {
2215 return Self{
2316 .r = r,
......@@ -96,13 +89,8 @@ pub fn to_linear_rgb(x: Self) color.LinearRgb {
9689 );
9790}
9891
99pub fn to_float(x: Self) Float {
100 return .{
101 .r = @as(f32, @floatFromInt(x.r)) / 255.0,
102 .g = @as(f32, @floatFromInt(x.g)) / 255.0,
103 .b = @as(f32, @floatFromInt(x.b)) / 255.0,
104 .a = @as(f32, @floatFromInt(x.a)) / 255.0,
105 };
92pub fn to_float(x: Self) vec4 {
93 return x.to_vec() / @as(vec4, @splat(255.0));
10694}
10795
10896pub fn from_float(y: vec4) Self {
......@@ -117,50 +105,47 @@ pub fn from_float(y: vec4) Self {
117105
118106// https://www.rapidtables.com/convert/color/rgb-to-cmyk.html
119107pub fn to_cmyk(x: Self) color.CMYK {
120 const f = x.to_float();
121 const k = 1 - @max(f.r, f.g, f.b);
122 const c = (1 - f.r - k) / (1 - k);
123 const m = (1 - f.g - k) / (1 - k);
124 const y = (1 - f.b - k) / (1 - k);
125 const a = f.a;
108 const r, const g, const b, const a = x.to_float();
109 const k = 1 - @max(r, g, b);
110 const c = (1 - r - k) / (1 - k);
111 const m = (1 - g - k) / (1 - k);
112 const y = (1 - b - k) / (1 - k);
126113 return color.CMYK.initCMYKA(c, m, y, k, a);
127114}
128115
129116// https://www.rapidtables.com/convert/color/rgb-to-hsl.html
130117pub fn to_hsl(x: Self) color.HSL {
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);
118 const r, const g, const b, const a = x.to_float();
119 const cmax = @max(r, g, b);
120 const cmin = @min(r, g, b);
134121 const delta = cmax - cmin;
135122 const h = blk: {
136123 if (delta == 0) break :blk 0;
137 if (cmax == f.r) break :blk @rem(((f.g - f.b) / delta), 6.0) / 6.0;
138 if (cmax == f.g) break :blk (((f.b - f.r) / delta) + 2) / 6.0;
139 if (cmax == f.b) break :blk (((f.r - f.g) / delta) + 4) / 6.0;
124 if (cmax == r) break :blk @rem(((g - b) / delta), 6.0) / 6.0;
125 if (cmax == g) break :blk (((b - r) / delta) + 2) / 6.0;
126 if (cmax == b) break :blk (((r - g) / delta) + 4) / 6.0;
140127 unreachable;
141128 };
142129 const l = (cmax + cmin) / 2;
143130 const s = if (delta == 0) 0 else delta / (1 - @abs(2 * l - 1));
144 const a = f.a;
145131 return color.HSL.initHSLA(h, s, l, a);
146132}
147133
148134// https://www.rapidtables.com/convert/color/rgb-to-hsv.html
149135pub fn to_hsv(x: Self) color.HSV {
150 const f = x.to_float();
151 const cmax = @max(f.r, f.g, f.b);
152 const cmin = @min(f.r, f.g, f.b);
136 const r, const g, const b, const a = x.to_float();
137 const cmax = @max(r, g, b);
138 const cmin = @min(r, g, b);
153139 const delta = cmax - cmin;
154140 const h = blk: {
155141 if (delta == 0) break :blk 0;
156 if (cmax == f.r) break :blk (@rem(((f.g - f.b) / delta), 6.0) / 6.0);
157 if (cmax == f.g) break :blk ((((f.b - f.r) / delta) + 2) / 6.0);
158 if (cmax == f.b) break :blk ((((f.r - f.g) / delta) + 4) / 6.0);
142 if (cmax == r) break :blk (@rem(((g - b) / delta), 6.0) / 6.0);
143 if (cmax == g) break :blk ((((b - r) / delta) + 2) / 6.0);
144 if (cmax == b) break :blk ((((r - g) / delta) + 4) / 6.0);
159145 unreachable;
160146 };
161147 const s = if (cmax == 0) 0 else delta / cmax;
162148 const v = cmax;
163 const a = f.a;
164149 return color.HSV.initHSVA(h, s, v, a);
165150}
166151