alphaBlendColors function

int alphaBlendColors(
  1. int dst,
  2. int src, [
  3. int fraction = 0xff
])

Returns a new color of src alpha-blended onto dst. The opacity of src is additionally scaled by fraction / 255.

Implementation

int alphaBlendColors(int dst, int src, [int fraction = 0xff]) {
  final srcAlpha = getAlpha(src);
  if (srcAlpha == 255 && fraction == 0xff) {
    // src is fully opaque, nothing to blend
    return src;
  }
  if (srcAlpha == 0 && fraction == 0xff) {
    // src is fully transparent, nothing to blend
    return dst;
  }
  var a = (srcAlpha / 255.0);
  if (fraction != 0xff) {
    a *= (fraction / 255.0);
  }

  final sr = (getRed(src) * a).round();
  final sg = (getGreen(src) * a).round();
  final sb = (getBlue(src) * a).round();
  final sa = (srcAlpha * a).round();

  final dr = (getRed(dst) * (1.0 - a)).round();
  final dg = (getGreen(dst) * (1.0 - a)).round();
  final db = (getBlue(dst) * (1.0 - a)).round();
  final da = (getAlpha(dst) * (1.0 - a)).round();

  return getColor(sr + dr, sg + dg, sb + db, sa + da);
}