| ... | ... | @@ -11,13 +11,6 @@ g: u8, // green value |
| 11 | 11 | b: u8, // blue value |
| 12 | 12 | a: u8, // alpha value |
| 13 | 13 | |
| 14 | | pub const Float = struct { |
| 15 | | r: f32, |
| 16 | | g: f32, |
| 17 | | b: f32, |
| 18 | | a: f32, |
| 19 | | }; |
| 20 | | |
| 21 | 14 | pub fn initRGBA(r: u8, g: u8, b: u8, a: u8) Self { |
| 22 | 15 | return Self{ |
| 23 | 16 | .r = r, |
| ... | ... | @@ -96,13 +89,8 @@ pub fn to_linear_rgb(x: Self) color.LinearRgb { |
| 96 | 89 | ); |
| 97 | 90 | } |
| 98 | 91 | |
| 99 | | pub 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 | | }; |
| 92 | pub fn to_float(x: Self) vec4 { |
| 93 | return x.to_vec() / @as(vec4, @splat(255.0)); |
| 106 | 94 | } |
| 107 | 95 | |
| 108 | 96 | pub fn from_float(y: vec4) Self { |
| ... | ... | @@ -117,50 +105,47 @@ pub fn from_float(y: vec4) Self { |
| 117 | 105 | |
| 118 | 106 | // https://www.rapidtables.com/convert/color/rgb-to-cmyk.html |
| 119 | 107 | pub 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); |
| 126 | 113 | return color.CMYK.initCMYKA(c, m, y, k, a); |
| 127 | 114 | } |
| 128 | 115 | |
| 129 | 116 | // https://www.rapidtables.com/convert/color/rgb-to-hsl.html |
| 130 | 117 | pub 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); |
| 134 | 121 | const delta = cmax - cmin; |
| 135 | 122 | const h = blk: { |
| 136 | 123 | 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; |
| 140 | 127 | unreachable; |
| 141 | 128 | }; |
| 142 | 129 | const l = (cmax + cmin) / 2; |
| 143 | 130 | const s = if (delta == 0) 0 else delta / (1 - @abs(2 * l - 1)); |
| 144 | | const a = f.a; |
| 145 | 131 | return color.HSL.initHSLA(h, s, l, a); |
| 146 | 132 | } |
| 147 | 133 | |
| 148 | 134 | // https://www.rapidtables.com/convert/color/rgb-to-hsv.html |
| 149 | 135 | pub 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); |
| 153 | 139 | const delta = cmax - cmin; |
| 154 | 140 | const h = blk: { |
| 155 | 141 | 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); |
| 159 | 145 | unreachable; |
| 160 | 146 | }; |
| 161 | 147 | const s = if (cmax == 0) 0 else delta / cmax; |
| 162 | 148 | const v = cmax; |
| 163 | | const a = f.a; |
| 164 | 149 | return color.HSV.initHSVA(h, s, v, a); |
| 165 | 150 | } |
| 166 | 151 | |