diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/player.zig | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/src/player.zig b/src/player.zig index e69b3ce..74a1f10 100644 --- a/src/player.zig +++ b/src/player.zig @@ -42,26 +42,30 @@ fn fasterColourBlend(onto: Colour, from: Colour) Colour { const na: u16 = af + @divTrunc(of * (255 - af), 255); if (na == 0) return Colour.Black; - const rf: u16 = from.r; - const ro: u16 = onto.r; - const gf: u16 = from.g; - const go: u16 = onto.g; - const bf: u16 = from.b; - const bo: u16 = onto.b; + const rf: i32 = from.r; + const ro: i32 = onto.r; + const gf: i32 = from.g; + const go: i32 = onto.g; + const bf: i32 = from.b; + const bo: i32 = onto.b; - // These computations are expensive, but more accurate + // The most accurate i've found is + const nr = @divTrunc(ro * na + (rf - ro) * af, na); + const ng = @divTrunc(go * na + (gf - go) * af, na); + const nb = @divTrunc(bo * na + (bf - bo) * af, na); + + // Note: there are other versions which don't require using intermediate + // i32 division, and instead work on plain u16. + + // These computations are more expensive and less accurate // const nr = @divTrunc(af * rf, na) + ro - @divTrunc(af * ro, na); // const ng = @divTrunc(af * gf, na) + go - @divTrunc(af * go, na); // const nb = @divTrunc(af * bf, na) + bo - @divTrunc(af * bo, na); // These computations are incorrect, but fast - const nr = (af * rf + (255 - af) * ro) / 255; - const ng = (af * gf + (255 - af) * go) / 255; - const nb = (af * bf + (255 - af) * bo) / 255; - - // The most accurate i've found is @divTrunc(ro * na + (rf - ro) * af, na); - // but this requires using signed integers, for which presumably division is - // slower still + // const nr = (af * rf + (255 - af) * ro) / 255; + // const ng = (af * gf + (255 - af) * go) / 255; + // const nb = (af * bf + (255 - af) * bo) / 255; return Colour{ .a = @intCast(u8, na), |
