alphaBlend static method

void alphaBlend(
  1. Vector4 foreground,
  2. Vector4 background,
  3. Vector4 result
)

Blend the foreground color over background color and store the color in result.

Implementation

static void alphaBlend(
    Vector4 foreground, Vector4 background, Vector4 result) {
  final a = foreground.a + (1.0 - foreground.a) * background.a;
  final factor = 1.0 / a;

  final r = factor *
      (foreground.a * foreground.r +
          (1.0 - foreground.a) * background.a * background.r);
  final g = factor *
      (foreground.a * foreground.g +
          (1.0 - foreground.a) * background.a * background.g);
  final b = factor *
      (foreground.a * foreground.b +
          (1.0 - foreground.a) * background.a * background.b);

  result.setValues(r, g, b, a);
}