Consider the following code:
for(int y = 0; y < ht; y++) {
int yp = ypos + y;
if(yp >= pri.getHeight() || yp < 0)
continue;
for(int x = 0; x < wt; x++) {
int xp = xpos + x;
if(xp >= pri.getWidth() || xp < 0)
continue;
int pos = yp * pri.getWidth() + xp;
if(pos < 0 || pos >= pixels.length)
continue;
int cv = pixels[pos];
int nv = colors[y*wt + x];
double alpha = nv >> 24 & 0xFF;
if(alpha < 255) {
nv = ColorUtils.packInt(255, cv >> 16 & 0xFF, cv >> 8 & 0xFF, cv & 0xFF);
nv = (int) Math.round(cv * (alpha / 255) + nv * (1 - alpha / 255));
}
pixels[pos] = nv;
}
}
As you may be able to guess, I’m attempting to blend pixels that are translucent. This method doesn’t appear to be working very well, however. Given pixel value ‘a’ (current set value in image) and new translucent value ‘b’ (let’s assume alpha of 200 or so), how do you blend the two pixels so that ‘a’ still appears through ‘b’ but with modified coloring (i.e red + blue = purplish)?