How could I merge an image with a color, so that I can make frozen looking sprites while I don’t need another image?
Yup. You can do this one of two ways.
- Create a new BufferedImage as you load it which is icy (recommend).
- On-the-fly overlay some color on the top of your image.
To do 1, it’s something like this:
BufferedImage tintedImage = new BufferedImage(oldImage.getWidth(), oldImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int i = 0; i < tintedImage.getRaster().getWidth(); i++)
{
for (int j = 0; j < tintedImage.getRaster().getHeight(); j++)
{
int[] data = new int[4];
data = oldImage.getRaster().getPixel(i,j,data);
//Adds a blue tint, but make sure it doesn't go over 255.
data[2] = Math.min(255,data[2]+50);
tintedImage.getRaster().setPixel(i,j,data);
}
}
To do 2:
g.drawImage(oldImage,x,y,width,height,null);
g.setColor(new Color(0,0,50,100));
g.fillRect(x,y,width,height);
#2 will unfortunately leave a haze around the transparent area. You can also just do #1 every single time you draw, but that’s slow.
I’m not sure that’s a good way to tint something - firstly it’ll only work if you want to tint towards a primary colour and secondly because you’re adding to the blue colour channel the result is going to be brighter than the original, not just blue-er. A better way would be to do a weighted average between the base colour and the tint colour. Better still would be to convert to a different colour space which makes the operation trivial (like HSL) then convert back to RGB to update your image.
What about using RBGFilter?