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...@@ -11,13 +11,6 @@ g: u8, // green value
11b: u8, // blue value11b: u8, // blue value
12a: u8, // alpha value12a: u8, // alpha value
1313
14pub const Float = struct {
15 r: f32,
16 g: f32,
17 b: f32,
18 a: f32,
19};
20
21pub fn initRGBA(r: u8, g: u8, b: u8, a: u8) Self {14pub fn initRGBA(r: u8, g: u8, b: u8, a: u8) Self {
22 return Self{15 return Self{
23 .r = r,16 .r = r,
...@@ -96,13 +89,8 @@ pub fn to_linear_rgb(x: Self) color.LinearRgb {...@@ -96,13 +89,8 @@ pub fn to_linear_rgb(x: Self) color.LinearRgb {
96 );89 );
97}90}
9891
99pub fn to_float(x: Self) Float {92pub fn to_float(x: Self) vec4 {
100 return .{93 return x.to_vec() / @as(vec4, @splat(255.0));
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 };
106}94}
10795
108pub fn from_float(y: vec4) Self {96pub fn from_float(y: vec4) Self {
...@@ -117,50 +105,47 @@ pub fn from_float(y: vec4) Self {...@@ -117,50 +105,47 @@ pub fn from_float(y: vec4) Self {
117105
118// https://www.rapidtables.com/convert/color/rgb-to-cmyk.html106// https://www.rapidtables.com/convert/color/rgb-to-cmyk.html
119pub fn to_cmyk(x: Self) color.CMYK {107pub fn to_cmyk(x: Self) color.CMYK {
120 const f = x.to_float();108 const r, const g, const b, const a = x.to_float();
121 const k = 1 - @max(f.r, f.g, f.b);109 const k = 1 - @max(r, g, b);
122 const c = (1 - f.r - k) / (1 - k);110 const c = (1 - r - k) / (1 - k);
123 const m = (1 - f.g - k) / (1 - k);111 const m = (1 - g - k) / (1 - k);
124 const y = (1 - f.b - k) / (1 - k);112 const y = (1 - b - k) / (1 - k);
125 const a = f.a;
126 return color.CMYK.initCMYKA(c, m, y, k, a);113 return color.CMYK.initCMYKA(c, m, y, k, a);
127}114}
128115
129// https://www.rapidtables.com/convert/color/rgb-to-hsl.html116// https://www.rapidtables.com/convert/color/rgb-to-hsl.html
130pub fn to_hsl(x: Self) color.HSL {117pub fn to_hsl(x: Self) color.HSL {
131 const f = x.to_float();118 const r, const g, const b, const a = x.to_float();
132 const cmax = @max(f.r, f.g, f.b);119 const cmax = @max(r, g, b);
133 const cmin = @min(f.r, f.g, f.b);120 const cmin = @min(r, g, b);
134 const delta = cmax - cmin;121 const delta = cmax - cmin;
135 const h = blk: {122 const h = blk: {
136 if (delta == 0) break :blk 0;123 if (delta == 0) break :blk 0;
137 if (cmax == f.r) break :blk @rem(((f.g - f.b) / delta), 6.0) / 6.0;124 if (cmax == r) break :blk @rem(((g - b) / delta), 6.0) / 6.0;
138 if (cmax == f.g) break :blk (((f.b - f.r) / delta) + 2) / 6.0;125 if (cmax == g) break :blk (((b - r) / delta) + 2) / 6.0;
139 if (cmax == f.b) break :blk (((f.r - f.g) / delta) + 4) / 6.0;126 if (cmax == b) break :blk (((r - g) / delta) + 4) / 6.0;
140 unreachable;127 unreachable;
141 };128 };
142 const l = (cmax + cmin) / 2;129 const l = (cmax + cmin) / 2;
143 const s = if (delta == 0) 0 else delta / (1 - @abs(2 * l - 1));130 const s = if (delta == 0) 0 else delta / (1 - @abs(2 * l - 1));
144 const a = f.a;
145 return color.HSL.initHSLA(h, s, l, a);131 return color.HSL.initHSLA(h, s, l, a);
146}132}
147133
148// https://www.rapidtables.com/convert/color/rgb-to-hsv.html134// https://www.rapidtables.com/convert/color/rgb-to-hsv.html
149pub fn to_hsv(x: Self) color.HSV {135pub fn to_hsv(x: Self) color.HSV {
150 const f = x.to_float();136 const r, const g, const b, const a = x.to_float();
151 const cmax = @max(f.r, f.g, f.b);137 const cmax = @max(r, g, b);
152 const cmin = @min(f.r, f.g, f.b);138 const cmin = @min(r, g, b);
153 const delta = cmax - cmin;139 const delta = cmax - cmin;
154 const h = blk: {140 const h = blk: {
155 if (delta == 0) break :blk 0;141 if (delta == 0) break :blk 0;
156 if (cmax == f.r) break :blk (@rem(((f.g - f.b) / delta), 6.0) / 6.0);142 if (cmax == r) break :blk (@rem(((g - b) / delta), 6.0) / 6.0);
157 if (cmax == f.g) break :blk ((((f.b - f.r) / delta) + 2) / 6.0);143 if (cmax == g) break :blk ((((b - r) / delta) + 2) / 6.0);
158 if (cmax == f.b) break :blk ((((f.r - f.g) / delta) + 4) / 6.0);144 if (cmax == b) break :blk ((((r - g) / delta) + 4) / 6.0);
159 unreachable;145 unreachable;
160 };146 };
161 const s = if (cmax == 0) 0 else delta / cmax;147 const s = if (cmax == 0) 0 else delta / cmax;
162 const v = cmax;148 const v = cmax;
163 const a = f.a;
164 return color.HSV.initHSVA(h, s, v, a);149 return color.HSV.initHSVA(h, s, v, a);
165}150}
166151