vecmath file split mayhem
This commit is contained in:
95
packages/vecmath/src/colors/Color.zig
Normal file
95
packages/vecmath/src/colors/Color.zig
Normal file
@@ -0,0 +1,95 @@
|
||||
const std = @import("std");
|
||||
const vm = @import("root");
|
||||
|
||||
pub const Color = extern struct {
|
||||
r: u8,
|
||||
g: u8,
|
||||
b: u8,
|
||||
a: u8,
|
||||
|
||||
pub const Array = [4]u8;
|
||||
|
||||
pub const clear = init(0, 0, 0, 0);
|
||||
pub const black = initOpaque(0, 0, 0);
|
||||
pub const red = initOpaque(255, 0, 0);
|
||||
pub const green = initOpaque(0, 255, 0);
|
||||
pub const blue = initOpaque(0, 0, 255);
|
||||
pub const cyan = initOpaque(0, 255, 255);
|
||||
pub const magenta = initOpaque(255, 0, 255);
|
||||
pub const yellow = initOpaque(255, 255, 0);
|
||||
pub const white = initOpaque(255, 255, 255);
|
||||
|
||||
pub inline fn init(r: u8, g: u8, b: u8, a: u8) Color {
|
||||
return .{ .r = r, .g = g, .b = b, .a = a };
|
||||
}
|
||||
|
||||
pub inline fn initOpaque(r: u8, g: u8, b: u8) Color {
|
||||
return .{ .r = r, .g = g, .b = b, .a = 255 };
|
||||
}
|
||||
|
||||
pub inline fn initArray(array: Array) Color {
|
||||
return @bitCast(array);
|
||||
}
|
||||
|
||||
pub inline fn l(comptime literal: []const u8) Color {
|
||||
if (literal.len == 4 and literal[0] == '#') { // #RGB
|
||||
return .initOpaque(
|
||||
(std.fmt.parseUnsigned(u8, literal[1..2], 16) catch unreachable) * 0x11,
|
||||
(std.fmt.parseUnsigned(u8, literal[2..3], 16) catch unreachable) * 0x11,
|
||||
(std.fmt.parseUnsigned(u8, literal[3..4], 16) catch unreachable) * 0x11,
|
||||
);
|
||||
} else if (literal.len == 5 and literal[0] == '#') { // #RGBA
|
||||
return .init(
|
||||
(std.fmt.parseUnsigned(u8, literal[1..2], 16) catch unreachable) * 0x11,
|
||||
(std.fmt.parseUnsigned(u8, literal[2..3], 16) catch unreachable) * 0x11,
|
||||
(std.fmt.parseUnsigned(u8, literal[3..4], 16) catch unreachable) * 0x11,
|
||||
(std.fmt.parseUnsigned(u8, literal[4..5], 16) catch unreachable) * 0x11,
|
||||
);
|
||||
} else if (literal.len == 7 and literal[0] == '#') { // #RRGGBB
|
||||
return .initOpaque(
|
||||
(std.fmt.parseUnsigned(u8, literal[1..3], 16) catch unreachable),
|
||||
(std.fmt.parseUnsigned(u8, literal[3..5], 16) catch unreachable),
|
||||
(std.fmt.parseUnsigned(u8, literal[5..7], 16) catch unreachable),
|
||||
);
|
||||
} else if (literal.len == 9 and literal[0] == '#') { // #RRGGBBAA
|
||||
return .init(
|
||||
(std.fmt.parseUnsigned(u8, literal[1..3], 16) catch unreachable),
|
||||
(std.fmt.parseUnsigned(u8, literal[3..5], 16) catch unreachable),
|
||||
(std.fmt.parseUnsigned(u8, literal[5..7], 16) catch unreachable),
|
||||
(std.fmt.parseUnsigned(u8, literal[7..9], 16) catch unreachable),
|
||||
);
|
||||
}
|
||||
|
||||
@compileError("Invalid color literal: " ++ literal);
|
||||
}
|
||||
|
||||
pub inline fn asArray(self: Color) Array {
|
||||
return @bitCast(self);
|
||||
}
|
||||
};
|
||||
|
||||
test "l" {
|
||||
const i: Color = .l("#012");
|
||||
try std.testing.expectEqual(0x00, i.r);
|
||||
try std.testing.expectEqual(0x11, i.g);
|
||||
try std.testing.expectEqual(0x22, i.b);
|
||||
try std.testing.expectEqual(0xFF, i.a);
|
||||
|
||||
const j: Color = .l("#3456");
|
||||
try std.testing.expectEqual(0x33, j.r);
|
||||
try std.testing.expectEqual(0x44, j.g);
|
||||
try std.testing.expectEqual(0x55, j.b);
|
||||
try std.testing.expectEqual(0x66, j.a);
|
||||
|
||||
const k: Color = .l("#F08040");
|
||||
try std.testing.expectEqual(0xF0, k.r);
|
||||
try std.testing.expectEqual(0x80, k.g);
|
||||
try std.testing.expectEqual(0x40, k.b);
|
||||
try std.testing.expectEqual(0xFF, k.a);
|
||||
|
||||
const l: Color = .l("#20304050");
|
||||
try std.testing.expectEqual(0x20, l.r);
|
||||
try std.testing.expectEqual(0x30, l.g);
|
||||
try std.testing.expectEqual(0x40, l.b);
|
||||
try std.testing.expectEqual(0x50, l.a);
|
||||
}
|
||||
Reference in New Issue
Block a user