Added generic constants to vec and fromHex, toHex for Color

This commit is contained in:
abux 2026-08-06 00:20:25 +01:00
parent cb7b54995b
commit f7e249d645
3 changed files with 66 additions and 8 deletions

View file

@ -36,15 +36,36 @@ pub const Color = extern struct {
/// ---------------------------------------------------- /// ----------------------------------------------------
/// ---------------------------------------------------- /// ----------------------------------------------------
pub inline fn hex(comptime value: u32) Self { pub inline fn fromHex(value: u32) Self {
const r = @as(f32, @as(u8, @truncate(value >> 16))) / 255.0; const r = @as(f32, @as(u8, @truncate(value >> 24))) / 255.0;
const g = @as(f32, @as(u8, @truncate(value >> 8))) / 255.0; const g = @as(f32, @as(u8, @truncate(value >> 16))) / 255.0;
const b = @as(f32, @as(u8, @truncate(value >> 0))) / 255.0; const b = @as(f32, @as(u8, @truncate(value >> 8))) / 255.0;
const a = if (value > 0xFFFFFF) const a = if (value > 0xFFFFFF)
@as(f32, @as(u8, @truncate(value >> 24))) / 255.0 @as(f32, @as(u8, @truncate(value))) / 255.0
else else
1.0; 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;
} }
/// ---------------------------------------------------- /// ----------------------------------------------------

View file

@ -149,8 +149,11 @@ test "color hex" {
std.debug.print("\x1b[35m--- color hex ---\n", .{}); std.debug.print("\x1b[35m--- color hex ---\n", .{});
defer std.debug.print("\x1b[0m\n", .{}); defer std.debug.print("\x1b[0m\n", .{});
const c: Color = .hex(0xFF8800); const a: Color = .fromHex(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 }); 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" { test "color blend" {

View file

@ -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 });
};
}; };
} }