blending

Provides a set of functions to blend together an RGB and an RGBA, or two RGBA color values, using custom or predefined blending functions.

Custom blending function

The custom blending function realizes the combination of the two colors assuming both are at 100% alpha and must have the following interface:

vec3 BLEND_FN(vec3 dst, vec3 src);

Compute the RGB composite of a solid background color dst and a foreground color with alpha src using a custom blending function BLEND_FN

template <BLEND_FN>
vec3 blendSimple(vec3 dst, vec4 src);

Compute the RGBA composite of a background color with alpha dst and a foreground color with alpha src using a custom blending function BLEND_FN

template <BLEND_FN>
vec4 blend(vec4 dst, vec4 src);

Predefined blending modes

The available blending modes and their respective blending functions are:

  • transparency – the classical transparency blending (src)
  • additive – dst + src
  • subtractive – 1 - (1-dst) - (1-src)
  • multiplicative – dst * src
  • inverse multiplicative – 1 - (1-dst) * (1-src)

All of these have the associative property, and all apart from the first one are also symmetrical.

Compute the RGB composite of a solid background color dst and a foreground color with alpha src using a predefined blending mode

vec3 blendTransparency(vec3 dst, vec4 src);
vec3 blendAdditive(vec3 dst, vec4 src);
vec3 blendSubtractive(vec3 dst, vec4 src);
vec3 blendMultiplicative(vec3 dst, vec4 src);
vec3 blendInverseMultiplicative(vec3 dst, vec4 src);

Compute the RGBA composite of a background color with alpha dst and a foreground color with alpha src using a predefined blending mode

vec4 blendTransparency(vec4 dst, vec4 src);
vec4 blendAdditive(vec4 dst, vec4 src);
vec4 blendSubtractive(vec4 dst, vec4 src);
vec4 blendMultiplicative(vec4 dst, vec4 src);
vec4 blendInverseMultiplicative(vec4 dst, vec4 src);

For each of these, there is also an equivalent function that operates on color values with premultiplied alpha. These are generally simpler and more efficient.

vec3 blendTransparencyPremultiplied(vec3 dst, vec4 src);
vec3 blendAdditivePremultiplied(vec3 dst, vec4 src);
vec3 blendSubtractivePremultiplied(vec3 dst, vec4 src);
vec3 blendMultiplicativePremultiplied(vec3 dst, vec4 src);
vec3 blendInverseMultiplicativePremultiplied(vec3 dst, vec4 src);
vec4 blendTransparencyPremultiplied(vec4 dst, vec4 src);
vec4 blendAdditivePremultiplied(vec4 dst, vec4 src);
vec4 blendSubtractivePremultiplied(vec4 dst, vec4 src);
vec4 blendMultiplicativePremultiplied(vec4 dst, vec4 src);
vec4 blendInverseMultiplicativePremultiplied(vec4 dst, vec4 src);