render: premultiply alpha for user-provided colors

The wlroots rendering API expects colors to be provided with
premultipled alpha but we currently do not parse them as such. This
causes blending with e.g. a transparent border color to be very broken.
This commit is contained in:
MaxVerevkin
2023-01-06 10:37:21 +02:00
committed by Isaac Freund
parent 701d16c2ea
commit 030f7efd4f
2 changed files with 11 additions and 9 deletions

View File

@ -104,7 +104,7 @@ pub fn setCursorWarp(
return Error.UnknownOption;
}
/// Parse a color in the format 0xRRGGBB or 0xRRGGBBAA
/// Parse a color in the format 0xRRGGBB or 0xRRGGBBAA. Returned color has premultiplied alpha.
fn parseRgba(string: []const u8) ![4]f32 {
if (string.len != 8 and string.len != 10) return error.InvalidRgba;
if (string[0] != '0' or string[1] != 'x') return error.InvalidRgba;
@ -114,10 +114,12 @@ fn parseRgba(string: []const u8) ![4]f32 {
const b = try fmt.parseInt(u8, string[6..8], 16);
const a = if (string.len == 10) try fmt.parseInt(u8, string[8..10], 16) else 255;
const alpha = @intToFloat(f32, a) / 255.0;
return [4]f32{
@intToFloat(f32, r) / 255.0,
@intToFloat(f32, g) / 255.0,
@intToFloat(f32, b) / 255.0,
@intToFloat(f32, a) / 255.0,
@intToFloat(f32, r) / 255.0 * alpha,
@intToFloat(f32, g) / 255.0 * alpha,
@intToFloat(f32, b) / 255.0 * alpha,
alpha,
};
}