colors with transparency (to fix later)
This commit is contained in:
parent
6e3d7a74be
commit
6065695af9
2 changed files with 98 additions and 64 deletions
|
@ -18,16 +18,45 @@ pub const RGB = struct {
|
|||
}
|
||||
};
|
||||
|
||||
// pub const Default256 = [_]RGB{
|
||||
// RGB{ .r = 0, .g = 0, .b = 0 },
|
||||
// RGB{ .r = 204, .g = 4, .b = 3 },
|
||||
// RGB{ .r = 25, .g = 203, .b = 0 },
|
||||
// RGB{ .r = 206, .g = 203, .b = 0 },
|
||||
// RGB{ .r = 13, .g = 115, .b = 204 },
|
||||
// RGB{ .r = 203, .g = 30, .b = 209 },
|
||||
// RGB{ .r = 13, .g = 205, .b = 205 },
|
||||
// RGB{ .r = 221, .g = 221, .b = 221 },
|
||||
// };
|
||||
pub const RGBA = struct {
|
||||
r: f32,
|
||||
g: f32,
|
||||
b: f32,
|
||||
/// Alpha channel between 0.0 - 1.0
|
||||
a: f32 = 1.0,
|
||||
|
||||
/// Create from string at comptime
|
||||
pub fn init(comptime string: []const u8) !RGBA {
|
||||
std.debug.assert(string.len == 8 or string.len == 9);
|
||||
comptime var index: usize = 0;
|
||||
if (string[0] == '#') index += 1;
|
||||
const r = try fmt.parseInt(u8, string[index .. index + 2], 16);
|
||||
const g = try fmt.parseInt(u8, string[index + 2 .. index + 4], 16);
|
||||
const b = try fmt.parseInt(u8, string[index + 4 .. index + 6], 16);
|
||||
const a = try fmt.parseInt(u8, string[index + 6 .. index + 8], 16);
|
||||
return .{
|
||||
.r = @as(f32, @floatFromInt(r)) / 0xff,
|
||||
.g = @as(f32, @floatFromInt(g)) / 0xff,
|
||||
.b = @as(f32, @floatFromInt(b)) / 0xff,
|
||||
.a = @as(f32, @floatFromInt(a)) / 0xff,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn blend(base: *const RGBA, add: *const RGBA) RGBA {
|
||||
var r: RGBA = undefined;
|
||||
r.a = 1 - (1 - add.a) * (1 - base.a);
|
||||
if (r.a < std.math.floatEps(f32)) {
|
||||
r.r = 0;
|
||||
r.g = 0;
|
||||
r.b = 0;
|
||||
return r;
|
||||
}
|
||||
r.r = (add.r * add.a / r.a + base.r * base.a * (1 - add.a)) / r.a;
|
||||
r.g = (add.g * add.a / r.a + base.g * base.a * (1 - add.a)) / r.a;
|
||||
r.b = (add.b * add.a / r.a + base.b * base.a * (1 - add.a)) / r.a;
|
||||
return r;
|
||||
}
|
||||
};
|
||||
|
||||
test "RGB init from string" {
|
||||
const red = try RGB.init("#ff0000");
|
||||
|
@ -47,3 +76,18 @@ test "RGB init from string" {
|
|||
|
||||
try std.testing.expectError(fmt.ParseIntError.InvalidCharacter, RGB.init("xyzxyz"));
|
||||
}
|
||||
|
||||
test "RGBA blend" {
|
||||
const red = RGBA{ .r = 1.0, .g = 0, .b = 0, .a = 0.0 };
|
||||
const blue = RGBA{ .r = 0, .g = 0, .b = 1.0, .a = 0.0 };
|
||||
const result = red.blend(&blue);
|
||||
std.debug.print("\n{any}\n", .{result});
|
||||
const reeb = RGBA{ .r = 1.0, .g = 0, .b = 0, .a = 1.0 };
|
||||
const blueb = RGBA{ .r = 0, .g = 0, .b = 1.0, .a = 0.0 };
|
||||
const resultt = reeb.blend(&blueb);
|
||||
std.debug.print("{any}\n", .{resultt});
|
||||
const roob = RGBA{ .r = 1.0, .g = 0, .b = 0, .a = 0.5 };
|
||||
const bloob = RGBA{ .r = 0, .g = 0, .b = 1.0, .a = 0.5 };
|
||||
const resulttt = roob.blend(&bloob);
|
||||
std.debug.print("{any}\n", .{resulttt});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue