Homepage

Premultiplied Alpha

A small but essential piece of information to know when writing shaders in Indigo, is that Indigo uses premultiplied alpha almost everywhere, with the notable exception of Shapes.

It is beyond the scope of these docs to explain straight vs premultiplied alpha, and it's very easy to search for. However, we can take a quick look at a practical example to try and give you some intuition.

The quesiton is: What color does this represent?

vec4 color = vec4(1.0, 0.0, 0.0, 0.5);

The expected visible color here is of course full red that is 50% transparent, and under "straight" alpha, that's exactly what you'd get. In Indigo, the result is actually just 100% red. What is going on?

Well here, the alpha has not yet been multiplied across the color values. To get the desired output we need to do the following:

vec4 color = vec4(0.5, 0.0, 0.0, 0.5);

...because the amount of red you see in a half transparent red is half the amount of red. Or quite often you see this sort of thing:

vec4 color = vec4(1.0, 0.0, 0.0, 0.5);
color = vec4(color.rgb * color.a, color.a);

People find it easier to think in straight alpha, but in reality, the images and textures we load are all premultiplied. When you look at a semi-transparent image in an image editor, you're looking at the premultiplied version.

Once you get used to the idea, it makes programming shaders easier.