| author | |
| committer | |
| log | c498be6597f316c93ed6b9f0c13838802fb23123 |
| tree | 49eddd58740194eccca5a7cdff09370abf2c1fc3 |
| parent | 70b5daf6a02ba292381a4b5a557d5cdf70a3c731 |
3 files changed, 45 insertions(+), 0 deletions(-)
LinearRgb.zig created+26| ... | ... | @@ -0,0 +1,26 @@ |
| 1 | //! object representing a single color in the Linear RGB colorspace with channel values from 0-1 | |
| 2 | ||
| 3 | const std = @import("std"); | |
| 4 | const Self = @This(); | |
| 5 | ||
| 6 | r: f32, | |
| 7 | g: f32, | |
| 8 | b: f32, | |
| 9 | a: f32, | |
| 10 | ||
| 11 | pub fn initRGBA(r: u8, g: u8, b: u8, a: u8) Self { | |
| 12 | std.debug.assert(r >= 0.0 and r <= 1.0); | |
| 13 | std.debug.assert(g >= 0.0 and g <= 1.0); | |
| 14 | std.debug.assert(b >= 0.0 and b <= 1.0); | |
| 15 | std.debug.assert(a >= 0.0 and a <= 1.0); | |
| 16 | return Self{ | |
| 17 | .r = r, | |
| 18 | .g = g, | |
| 19 | .b = b, | |
| 20 | .a = a, | |
| 21 | }; | |
| 22 | } | |
| 23 | ||
| 24 | pub fn eql(x: Self, y: Self) bool { | |
| 25 | return x.r == y.r and x.g == y.g and x.b == y.b; | |
| 26 | } |
mod.zig+1| ... | ... | @@ -2,3 +2,4 @@ const std = @import("std"); |
| 2 | 2 | const color = @This(); |
| 3 | 3 | |
| 4 | 4 | pub const sRGB = @import("./sRGB.zig"); |
| 5 | pub const LinearRgb = @import("./LinearRgb.zig"); |
sRGB.zig+18| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | const std = @import("std"); |
| 4 | 4 | const Self = @This(); |
| 5 | const color = @import("./mod.zig"); | |
| 5 | 6 | |
| 6 | 7 | r: u8, // red value |
| 7 | 8 | g: u8, // green value |
| ... | ... | @@ -63,3 +64,20 @@ pub fn format(x: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, |
| 63 | 64 | std.fmt.fmtSliceHexLower(&.{x.b}), |
| 64 | 65 | }); |
| 65 | 66 | } |
| 67 | ||
| 68 | pub fn to_linear_rgb(x: Self) color.LinearRgb { | |
| 69 | const lut = comptime blk: { | |
| 70 | var res: [256]f32 = undefined; | |
| 71 | for (0..256) |i| { | |
| 72 | const c = @as(f32, @floatFromInt(i)) / 255.0; | |
| 73 | res[i] = if (c <= 0.04045) c / 12.92 else std.math.pow(f32, (c + 0.055) / 1.055, 2.4); | |
| 74 | } | |
| 75 | break :blk res; | |
| 76 | }; | |
| 77 | return color.LinearRgb.initRGBA( | |
| 78 | lut[x.r], | |
| 79 | lut[x.g], | |
| 80 | lut[x.b], | |
| 81 | lut[x.a], | |
| 82 | ); | |
| 83 | } |