Rewrites color swapping algorithm, so that it supports partially transparent pixels.
@@ -171,16 +171,26 @@ public final class ImageCache {
final int backgroundRGB = character.getBackgroundColor().getRGB();
+ final int backgroundA = (backgroundRGB >> 24) & 0xFF;
+ final int backgroundR = (backgroundRGB >> 16) & 0xFF;
+ final int backgroundG = (backgroundRGB >> 8) & 0xFF;
+ final int backgroundB = backgroundRGB & 0xFF;
+
final int foregroundRGB = character.getForegroundColor().getRGB();
+ final int foregroundA = (foregroundRGB >> 24) & 0xFF;
+ final int foregroundR = (foregroundRGB >> 16) & 0xFF;
+ final int foregroundG = (foregroundRGB >> 8) & 0xFF;
+ final int foregroundB = foregroundRGB & 0xFF;
+
final boolean isTile = character instanceof AsciiTile;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int pixel = image.getRGB(x, y);
- int alpha = (pixel >> 24) & 0xff;
- int red = (pixel >> 16) & 0xff;
- int green = (pixel >> 8) & 0xff;
- int blue = (pixel) & 0xff;
+ int alpha = (pixel >> 24) & 0xFF;
+ int red = (pixel >> 16) & 0xFF;
+ int green = (pixel >> 8) & 0xFF;
+ int blue = (pixel) & 0xFF;
boolean isTransparent = alpha != 255;
isTransparent &= red == 0;
@@ -190,7 +200,23 @@ public final class ImageCache {
if (isTransparent) {
image.setRGB(x, y, backgroundRGB);
} else if (isTile == false) {
+ if (alpha == 0) {
image.setRGB(x, y, foregroundRGB);
+ } else {
+ // Blend the fore/background colors
+ int alphaBlend = backgroundA * (255 - foregroundA) + foregroundA;
+ int redBlend = backgroundR * (255 - foregroundA) + (foregroundR * foregroundA);
+ int greenBlend = backgroundG * (255 - foregroundA) + (foregroundG * foregroundA);
+ int blueBlend = backgroundB * (255 - foregroundA) + (foregroundB * foregroundA);
+
+ alphaBlend = alphaBlend & 0xFF;
+ redBlend = redBlend & 0xFF;
+ greenBlend = greenBlend & 0xFF;
+ blueBlend = blueBlend & 0xFF;
+
+ final int blendedRGBA = (alphaBlend << 24) + (redBlend << 16) + (greenBlend << 8) + blueBlend;
+ image.setRGB(x, y, blendedRGBA);
+ }
}
}
}
