| ... | @@ -11,13 +11,6 @@ g: u8, // green value | ... | @@ -11,13 +11,6 @@ g: u8, // green value |
| 11 | b: u8, // blue value | 11 | b: u8, // blue value |
| 12 | a: u8, // alpha value | 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 | pub fn initRGBA(r: u8, g: u8, b: u8, a: u8) Self { | 14 | pub 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 | } |
| 98 | | 91 | |
| 99 | pub fn to_float(x: Self) Float { | 92 | pub 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 | } |
| 107 | | 95 | |
| 108 | pub fn from_float(y: vec4) Self { | 96 | pub 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 { |
| 117 | | 105 | |
| 118 | // https://www.rapidtables.com/convert/color/rgb-to-cmyk.html | 106 | // https://www.rapidtables.com/convert/color/rgb-to-cmyk.html |
| 119 | pub fn to_cmyk(x: Self) color.CMYK { | 107 | pub 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 | } |
| 128 | | 115 | |
| 129 | // https://www.rapidtables.com/convert/color/rgb-to-hsl.html | 116 | // https://www.rapidtables.com/convert/color/rgb-to-hsl.html |
| 130 | pub fn to_hsl(x: Self) color.HSL { | 117 | pub 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 | } |
| 147 | | 133 | |
| 148 | // https://www.rapidtables.com/convert/color/rgb-to-hsv.html | 134 | // https://www.rapidtables.com/convert/color/rgb-to-hsv.html |
| 149 | pub fn to_hsv(x: Self) color.HSV { | 135 | pub 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 | } |
| 166 | | 151 | |