From 0f762fe6a3db8aa01e07a239c9b3f64f6f60980e Mon Sep 17 00:00:00 2001 From: jack Date: Sun, 22 Dec 2024 09:22:13 +0800 Subject: [PATCH] jok.Color: rgba/hsla color conversion --- build.zig.zon | 2 +- src/basic.zig | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/build.zig.zon b/build.zig.zon index 9f977bff..1c03f37d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,6 @@ .{ .name = "jok", - .version = "0.28.0", + .version = "0.28.1", .paths = .{ "README.md", "build.zig", diff --git a/src/basic.zig b/src/basic.zig index 2ca6ce78..f63bbc2f 100644 --- a/src/basic.zig +++ b/src/basic.zig @@ -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),