Skip to content

Commit

Permalink
jok.Color: rgba/hsla color conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Ji committed Dec 22, 2024
1 parent 45c3f1a commit 0f762fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = "jok",
.version = "0.28.0",
.version = "0.28.1",
.paths = .{
"README.md",
"build.zig",
Expand Down
23 changes: 23 additions & 0 deletions src/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,29 @@ pub const Color = extern struct {
return sdl.SDL_MapRGBA(@ptrCast(pixel_format), c.r, c.g, c.b, c.a);
}

pub inline fn fromHSL(hsl: [4]f32) Color {
const _rgba = zmath.hslToRgb(
zmath.loadArr4(hsl),
);
return .{
.r = @intFromFloat(_rgba[0] * 255),
.g = @intFromFloat(_rgba[1] * 255),
.b = @intFromFloat(_rgba[2] * 255),
.a = @intFromFloat(_rgba[3] * 255),
};
}
pub inline fn toHSL(c: Color) [4]f32 {
const hsl = zmath.rgbToHsl(
zmath.f32x4(
@as(f32, @floatFromInt(c.r)) / 255,
@as(f32, @floatFromInt(c.g)) / 255,
@as(f32, @floatFromInt(c.b)) / 255,
@as(f32, @floatFromInt(c.a)) / 255,
),
);
return zmath.vecToArr4(hsl);
}

pub inline fn mod(c0: Color, c1: Color) Color {
return .{
.r = @intFromFloat(@as(f32, @floatFromInt(c0.r)) * @as(f32, @floatFromInt(c1.r)) / 255.0),
Expand Down

0 comments on commit 0f762fe

Please sign in to comment.