From f7e249d645ca36286d52eb2f0846a41738f8ed1c Mon Sep 17 00:00:00 2001 From: abux Date: Thu, 6 Aug 2026 00:20:25 +0100 Subject: [PATCH] Added generic constants to vec and fromHex, toHex for Color --- src/color.zig | 33 +++++++++++++++++++++++++++------ src/root.zig | 7 +++++-- src/vec.zig | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/color.zig b/src/color.zig index c13de48..6813dd9 100644 --- a/src/color.zig +++ b/src/color.zig @@ -36,15 +36,36 @@ pub const Color = extern struct { /// ---------------------------------------------------- /// ---------------------------------------------------- - pub inline fn hex(comptime value: u32) Self { - const r = @as(f32, @as(u8, @truncate(value >> 16))) / 255.0; - const g = @as(f32, @as(u8, @truncate(value >> 8))) / 255.0; - const b = @as(f32, @as(u8, @truncate(value >> 0))) / 255.0; + pub inline fn fromHex(value: u32) Self { + const r = @as(f32, @as(u8, @truncate(value >> 24))) / 255.0; + const g = @as(f32, @as(u8, @truncate(value >> 16))) / 255.0; + const b = @as(f32, @as(u8, @truncate(value >> 8))) / 255.0; + const a = if (value > 0xFFFFFF) - @as(f32, @as(u8, @truncate(value >> 24))) / 255.0 + @as(f32, @as(u8, @truncate(value))) / 255.0 else 1.0; - return .{ .r = r, .g = g, .b = b, .a = a }; + + return .{ + .r = r, + .g = g, + .b = b, + .a = a, + }; + } + + /// ---------------------------------------------------- + /// ---------------------------------------------------- + pub inline fn toHex(self: Self) u32 { + const r: u32 = @intFromFloat(std.math.clamp(self.r, 0.0, 1.0) * 255.0); + const g: u32 = @intFromFloat(std.math.clamp(self.g, 0.0, 1.0) * 255.0); + const b: u32 = @intFromFloat(std.math.clamp(self.b, 0.0, 1.0) * 255.0); + const a: u32 = @intFromFloat(std.math.clamp(self.a, 0.0, 1.0) * 255.0); + + return (r << 24) | + (g << 16) | + (b << 8) | + a; } /// ---------------------------------------------------- diff --git a/src/root.zig b/src/root.zig index f426368..6917582 100644 --- a/src/root.zig +++ b/src/root.zig @@ -149,8 +149,11 @@ test "color hex" { std.debug.print("\x1b[35m--- color hex ---\n", .{}); defer std.debug.print("\x1b[0m\n", .{}); - const c: Color = .hex(0xFF8800); - std.debug.print("0xFF8800: r={d:.4} g={d:.4} b={d:.4} a={d:.1}\n", .{ c.r, c.g, c.b, c.a }); + const a: Color = .fromHex(0xFF8800); + std.debug.print("0xFF8800: r={d:.4} g={d:.4} b={d:.4} a={d:.1}\n", .{ a.r, a.g, a.b, a.a }); + + const b = Color.blue.toHex(); + std.debug.print("blue hex: {}\n", .{b}); } test "color blend" { diff --git a/src/vec.zig b/src/vec.zig index edbcd55..b5ec601 100644 --- a/src/vec.zig +++ b/src/vec.zig @@ -133,5 +133,39 @@ pub fn Vec( }, }; } + + // + // CONSTANTS + // + + pub const up: Self = blk: { + if (Len == 2) break :blk .new(.{ 0, -1 }); + if (Len == 3) break :blk .new(.{ 0, -1, 0 }); + }; + + pub const down: Self = blk: { + if (Len == 2) break :blk .new(.{ 0, 1 }); + if (Len == 3) break :blk .new(.{ 0, 1, 0 }); + }; + + pub const left: Self = blk: { + if (Len == 2) break :blk .new(.{ 1, 0 }); + if (Len == 3) break :blk .new(.{ 1, 0, 0 }); + }; + + pub const right: Self = blk: { + if (Len == 2) break :blk .new(.{ -1, 0 }); + if (Len == 3) break :blk .new(.{ -1, 0, 0 }); + }; + + pub const forward: Self = blk: { + if (Len != 3) @compileError("forward only defined for Vec3"); + break :blk .new(.{ 0, 0, 1 }); + }; + + pub const backward: Self = blk: { + if (Len != 3) @compileError("backward only defined for Vec3"); + break :blk .new(.{ 0, 0, -1 }); + }; }; }