tsoding / olive.c

Simple 2D Graphics Library for C
https://tsoding.github.io/olive.c/
MIT License
1.86k stars 105 forks source link

Alpha blending bug #13

Open JasonMDavey opened 1 year ago

JasonMDavey commented 1 year ago

It appears the bug from "Programming in C in 2022" is still present.

This is because when blending colours you use the alpha value of the "incoming" colour to calculate the new blended RGB colour components for the pixel, but the alpha component of the pixel is never updated.

So if the canvas initially starts filled with zeroes, then each pixel will always have alpha 0. And blended colour writes will never modify this. So the final exported image will have the correct R/G/B values, but always have alpha 0 in every pixel.

You probably want to "accumulate" the alpha from both the current colour on the canvas, and the "incoming" colour. For example, if the canvas contains a pixel with 25% alpha, and you draw a pixel with 50% alpha on top, the resulting alpha should be greater than 25%. But the current code will never modify it.