Use packed struct

This commit is contained in:
Matt Robenolt
2023-12-12 17:20:42 -08:00
parent 91937c4ada
commit 3866e09210
2 changed files with 2 additions and 17 deletions

View File

@ -1982,7 +1982,7 @@ pub const OptionAsAlt = enum {
}; };
/// Color represents a color using RGB. /// Color represents a color using RGB.
pub const Color = struct { pub const Color = packed struct(u24) {
r: u8, r: u8,
g: u8, g: u8,
b: u8, b: u8,
@ -1992,21 +1992,6 @@ pub const Color = struct {
return .{ .r = self.r, .g = self.g, .b = self.b }; return .{ .r = self.r, .g = self.g, .b = self.b };
} }
// Pack into an integer
pub fn toInt(self: Color) u24 {
// u24 covers RGB, typically, an alpha would pack to a full u32
return (@as(u24, self.r) << 16) + (@as(u24, self.g) << 8) + self.b;
}
test "toInt" {
const testing = std.testing;
try testing.exectEqual((Color{ .r = 0, .g = 0, .b = 0 }).toInt(), 0);
try testing.exectEqual((Color{ .r = 255, .g = 255, .b = 255 }).toInt(), 16777215);
try testing.exectEqual((Color{ .r = 100, .g = 20, .b = 12 }).toInt(), 6558732);
try testing.exectEqual((Color{ .r = 55, .g = 63, .b = 202 }).toInt(), 3620810);
}
pub fn parseCLI(input: ?[]const u8) !Color { pub fn parseCLI(input: ?[]const u8) !Color {
return fromHex(input orelse return error.ValueRequired); return fromHex(input orelse return error.ValueRequired);
} }

View File

@ -41,7 +41,7 @@ pub fn get(config: *const Config, k: Key, ptr_raw: *anyopaque) bool {
Color => { Color => {
const ptr: *c_uint = @ptrCast(@alignCast(ptr_raw)); const ptr: *c_uint = @ptrCast(@alignCast(ptr_raw));
ptr.* = value.toInt(); ptr.* = @as(c_uint, @as(u24, @bitCast(value)));
}, },
else => |T| switch (@typeInfo(T)) { else => |T| switch (@typeInfo(T)) {