compositeOver function

int compositeOver(
  1. int fg,
  2. int bg
)

Source-over composite of fg (any alpha) on opaque bg, per channel in encoded sRGB (what Flutter's Color.alphaBlend does). Returns opaque.

Implementation

int compositeOver(int fg, int bg) {
  final a = (fg >>> 24) & 0xFF;
  if (a == 255) return fg | 0xFF000000;
  if (a == 0) return bg | 0xFF000000;
  int ch(int shift) {
    final f = (fg >> shift) & 0xFF;
    final b = (bg >> shift) & 0xFF;
    return ((f * a + b * (255 - a)) / 255).round();
  }

  return 0xFF000000 | (ch(16) << 16) | (ch(8) << 8) | ch(0);
}