authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-14 18:29:10 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-09-14 18:32:26 -07:00
logc498be6597f316c93ed6b9f0c13838802fb23123
tree49eddd58740194eccca5a7cdff09370abf2c1fc3
parent70b5daf6a02ba292381a4b5a557d5cdf70a3c731

add LinearRgb


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
3const std = @import("std");
4const Self = @This();
5
6r: f32,
7g: f32,
8b: f32,
9a: f32,
10
11pub 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
24pub 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,3 +2,4 @@ const std = @import("std");
2const color = @This();2const color = @This();
33
4pub const sRGB = @import("./sRGB.zig");4pub const sRGB = @import("./sRGB.zig");
5pub const LinearRgb = @import("./LinearRgb.zig");
sRGB.zig+18
...@@ -2,6 +2,7 @@...@@ -2,6 +2,7 @@
22
3const std = @import("std");3const std = @import("std");
4const Self = @This();4const Self = @This();
5const color = @import("./mod.zig");
56
6r: u8, // red value7r: u8, // red value
7g: u8, // green value8g: u8, // green value
...@@ -63,3 +64,20 @@ pub fn format(x: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions,...@@ -63,3 +64,20 @@ pub fn format(x: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions,
63 std.fmt.fmtSliceHexLower(&.{x.b}),64 std.fmt.fmtSliceHexLower(&.{x.b}),
64 });65 });
65}66}
67
68pub 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}