Found it. Behold, the texturemixer.
package conversion;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
public abstract class TextureMixer {
private static short baseR, baseG, baseB;
private static PixelGrabber grabber;
private static Image texture;
private static int[] pixelList;
private static int[][] pixelArrayR, pixelArrayG, pixelArrayB, pixelArrayA;
private static int width, height;
public static Image mixTexture(String filename, int lightR, int lightG, int lightB, int darkR, int darkG, int darkB){
// Testcode
// long m1 = System.currentTimeMillis();
width = 16;
height = 16;
texture = Toolkit.getDefaultToolkit().getImage("src/textures/" + filename + ".png");;
pixelList = new int[width * height];
pixelArrayA = new int[width][height];
pixelArrayR = new int[width][height];
pixelArrayG = new int[width][height];
pixelArrayB = new int[width][height];
// Magenta = 255,0,255.
baseR = 255;
baseG = 0;
baseB = 255;
grabber = new PixelGrabber(texture, 0, 0, width, height, pixelList, 0, width);
grabber.startGrabbing();
// Puts all pixels from the texture in a list
try {
grabber.startGrabbing();
while(!grabber.grabPixels()){
}
} catch (InterruptedException e) {
System.out.println("src/worldModel/TerrainType/convertImage: " + e);
}
// Converts the hex list to 4 'readable' lists (RGB and alpha)
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
int pixel = pixelList[i + (j*width)];
pixelArrayA[i][j] = (pixel >> 24) & 0xFF;
pixelArrayR[i][j] = (pixel >> 16) & 0xFF;
pixelArrayG[i][j] = (pixel >> 8) & 0xFF;
pixelArrayB[i][j] = (pixel) & 0xFF;
// Converts every pixel with the correct RGB (magenta) to the 'light color'
if(pixelArrayR[i][j] == baseR && pixelArrayG[i][j] == baseG && pixelArrayB[i][j] == baseB){
pixelArrayR[i][j] = lightR;
pixelArrayG[i][j] = lightG;
pixelArrayB[i][j] = lightB;
/*
* Converts every shade of gray to the 'dark color' multiplied by the intensitie of the grayness.
* In other words, black stays black, white becomes the dark color, and everything inbetween
* becomes a darker shade of the 'dark' color.
*/
} else if(pixelArrayR[i][j] == pixelArrayG[i][j] && pixelArrayR[i][j] == pixelArrayB[i][j]) {
pixelArrayR[i][j] = Math.round(darkR * pixelArrayR[i][j]/255);
pixelArrayG[i][j] = Math.round(darkG * pixelArrayR[i][j]/255);
pixelArrayB[i][j] = Math.round(darkB * pixelArrayR[i][j]/255);
}
}
}
// Converts the 4 human-readable lists (notice the alpha hasn't changed) back to the hex list
int[] pixelList = new int[width * height];
for(int i = 0;i < width; i++){
for(int j = 0;j < height; j++){
pixelList[i + (j*width)] = ((pixelArrayA[i][j] << 24)& 0xFF000000)
| ((pixelArrayR[i][j] << 16) & 0x00FF0000)
| ((pixelArrayG[i][j] << 8) & 0x0000FF00)
| ((pixelArrayB[i][j]) & 0x000000FF);
}
}
// Testcode
// long m2 = System.currentTimeMillis();
// System.out.println("Duration: " + (m2 - m1));
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, pixelList, 0, width));
}
}