Skip to content

Commit

Permalink
colorspace: fix toHex methods
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Jan 9, 2025
1 parent 07a5a78 commit 90ed65e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/colorspace.zig
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,12 @@ pub const Rgb = struct {

/// Converts the RGB color into a hex value.
pub fn toHex(self: Rgb) u24 {
return self.r << 16 + self.g << 8 + self.g;
return
// zig fmt: off
(@as(u24, @intCast(self.r)) << @as(u5, @truncate(8 * 2))) +
(@as(u24, @intCast(self.g)) << @as(u5, @truncate(8 * 1))) +
(@as(u24, @intCast(self.g)) << @as(u5, @truncate(8 * 0)));
// zig fmt: on
}

/// Converts the RGB color into a RGBA color with the specified alpha.
Expand Down Expand Up @@ -395,7 +400,13 @@ pub const Rgba = packed struct {

/// Converts the RGBA color into a hex value.
pub fn toHex(self: Rgba) u32 {
return self.r << (8 * 3) + self.g << (8 * 2) + self.g << (8 * 1) + self.a << (8 * 0);
return
// zig fmt: off
(@as(u32, @intCast(self.r)) << @as(u5, @truncate(8 * 3))) +
(@as(u32, @intCast(self.g)) << @as(u5, @truncate(8 * 2))) +
(@as(u32, @intCast(self.g)) << @as(u5, @truncate(8 * 1))) +
(@as(u32, @intCast(self.a)) << @as(u5, @truncate(8 * 0)));
// zig fmt: on
}

/// Converts the RGBA color into a RGB color by removing the alpha channel.
Expand Down

0 comments on commit 90ed65e

Please sign in to comment.