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

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

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

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