Advertisement

Strange deviations while implementing alpha compositing

Started by April 21, 2020 08:54 AM
1 comment, last by hs12503 4 years, 9 months ago

I've been trying to create my own 2d renderer (for learning purposes) and I'm trying to implement alpha compositing. The formula I'm using is very trivial, which is `color = alpha * src + (1 - alpha) * dest`.

Everything seemed to be going and looking well until I started comparing it to existing 2d canvas implementations. I've tested my canvas against OpenGL, the JS canvas, and a BufferedImage in Java, and they have all been getting slightly different results to the formula I use.

For example, let's say I have a background with a red value of 1, and a shape with a red value of 0.49 and an alpha of 0.026. From plugging in the values to the formula, I got a red value of 251.6187, which can be rounded to 252. However, every canvas implementation I've used returns a red value of 251.

I initially thought I needed to apply round, floor, or ceil function to my result, but while that helped this example, it caused problems for others.

Is this not the alpha compositing formula that most 2D canvas implementations use, or am I doing something wrong? Any advice is appreciated. Thanks!

Here is a program to illustrate the issue: https://editor.p5js.org/SarahJ/sketches/VTzi0r5c7

This topic is closed to new replies.

Advertisement