I have two RGBA colors in linear [0..1] space, src
and dst
and I want to alpha blender src
over/under dst
n times. Of course, I could call the respective operation n times using the result from the previous one (which is what I am currently doing), but I want to optimize it into a single operation. I think just calculating the correct alpha on src
should be sufficient, but I may be wrong. Here's what I currently use:
public static final Color alphaOver(Color dst, Color src) {
float src1A = 1 - src.a;
float outA = src.a + dst.a * src1A;
if (outA == 0)
return Color.TRANSPARENT;
return new Color(
outA,
(src.r * src.a + dst.r * dst.a * src1A) / outA,
(src.g * src.a + dst.g * dst.a * src1A) / outA,
(src.b * src.a + dst.b * dst.a * src1A) / outA);
}
public static final Color alphaUnder(Color dst, Color src) {
return alphaOver(src, dst);
}
public static final Color alphaOver(Color dst, Color src, int times) {
Color ret = dst;
for (int i = 0; i < times; i++)
ret = alphaOver(ret, src);
return ret;
}
public static final Color alphaUnder(Color dst, Color src, int times) {
Color ret = dst;
for (int i = 0; i < times; i++)
ret = alphaUnder(ret, src);
return ret;
}
How can I optimize the last two functions?