((r1*a1 + r2*(255-a1)*(a2/(double)255))/a0 + .5)
((r1*a1/(double)255/a0 + r2*(255-a1)*(a2/(double)255))/a0 + .5)
Hope that makes sense, you need to scale back r1, g1, b1 with respect to their overall alpha (a0), or you're multiplying by 11 twice.
At least that's how I understand the formula to work. I might be off by a factor of 255 because I'm not exactly sure how that works.
Anyway, conceptually if a1 + a2 ~= a0, then you have a1/a0 + a2/a0 ~= 1 -> multiply 1 by original colours, weighted by their relative alpha.
((r1*a1/(double)255/a0 + r2*(255-a1)*(a2/(double)255))/a0 + .5)
Hope that makes sense, you need to scale back r1, g1, b1 with respect to their overall alpha (a0), or you're multiplying by 11 twice.
At least that's how I understand the formula to work. I might be off by a factor of 255 because I'm not exactly sure how that works.
Anyway, conceptually if a1 + a2 ~= a0, then you have a1/a0 + a2/a0 ~= 1 -> multiply 1 by original colours, weighted by their relative alpha.

