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]) {
  var srcAlpha = getAlpha(src);
  if (srcAlpha == 255 && fraction == 0xff) {
    // src is fully opaque, nothing to blend
    return src;
  }
  var a = (srcAlpha / 255.0);
  if (fraction != 0xff) {
    a *= (fraction / 255.0);
  }

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

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

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