Skip to content

Commit

Permalink
colorspace: replace if statements with if expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Oct 10, 2024
1 parent d73be3f commit e55426c
Showing 1 changed file with 6 additions and 34 deletions.
40 changes: 6 additions & 34 deletions src/colorspace.zig
Original file line number Diff line number Diff line change
Expand Up @@ -298,23 +298,9 @@ pub const Rgb = struct {
var g: f64 = @as(f64, @floatFromInt(self.g)) / 255;
var b: f64 = @as(f64, @floatFromInt(self.b)) / 255;

if (r > 0.04045) {
r = pow(f64, (r + 0.055) / 1.055, 2.4);
} else {
r /= 12.92;
}

if (g > 0.04045) {
g = pow(f64, (g + 0.055) / 1.055, 2.4);
} else {
g /= 12.92;
}

if (b > 0.04045) {
b = pow(f64, (b + 0.055) / 1.055, 2.4);
} else {
b /= 12.92;
}
r = if (r > 0.04045) pow(f64, (r + 0.055) / 1.055, 2.4) else r / 12.92;
g = if (g > 0.04045) pow(f64, (g + 0.055) / 1.055, 2.4) else g / 12.92;
b = if (b > 0.04045) pow(f64, (b + 0.055) / 1.055, 2.4) else b / 12.92;

return .{
.x = (r * 0.4124 + g * 0.3576 + b * 0.1805) * 100,
Expand Down Expand Up @@ -539,23 +525,9 @@ pub const Xyz = struct {
var g = (self.x * -0.9689 + self.y * 1.8758 + self.z * 0.0415) / 100;
var b = (self.x * 0.0557 + self.y * -0.2040 + self.z * 1.0570) / 100;

if (r > 0.0031308) {
r = 1.055 * pow(f64, r, (1.0 / 2.4)) - 0.055;
} else {
r *= 12.92;
}

if (g > 0.0031308) {
g = 1.055 * pow(f64, g, (1.0 / 2.4)) - 0.055;
} else {
g *= 12.92;
}

if (b > 0.0031308) {
b = 1.055 * pow(f64, b, (1.0 / 2.4)) - 0.055;
} else {
b *= 12.92;
}
r = if (r > 0.0031308) 1.055 * pow(f64, r, (1.0 / 2.4)) - 0.055 else r * 12.92;
g = if (g > 0.0031308) 1.055 * pow(f64, g, (1.0 / 2.4)) - 0.055 else g * 12.92;
b = if (b > 0.0031308) 1.055 * pow(f64, b, (1.0 / 2.4)) - 0.055 else b * 12.92;

return .{
.r = @intFromFloat(@round(255 * @max(0, @min(1, r)))),
Expand Down

0 comments on commit e55426c

Please sign in to comment.