0

I'v been making a game in java from scratch and have recently come to an issue when trying to color black and white BuffereImages. I want to keep the darkness of the pixels (if that makes any sense). For example, the grass in Minecraft is a gray image, but when seen in game it comes in different shades of green, but keeps its value. I don't have any code to show because I don't know where to start. All I have is a couple BufferedImages that I want to color. Thanks.

8bitslime
  • 164
  • 1
  • 13
  • 1
    You can use the black and white as an alpha mask (whiter means ligher green, black means darker green or vice-versa). See http://stackoverflow.com/questions/221830/set-bufferedimage-alpha-mask-in-java for one approach – lreeder Dec 26 '13 at 18:57
  • so use a solid color and display a half transparent version of the image on top of it? – 8bitslime Dec 26 '13 at 19:02
  • Yes, that's it in a nutshell. – lreeder Dec 26 '13 at 19:05
  • That worked great! but I used my graphics object to display a green sheet over it, this may seem stupid, but how would I set 1 tile to the color and save the image? – 8bitslime Dec 26 '13 at 19:14
  • Update your BufferedImage using the mask, then use ImageIO to write it to disk. See http://docs.oracle.com/javase/tutorial/2d/images/saveimage.html for ways to write a BufferedImage to specific file formas. – lreeder Dec 26 '13 at 19:45
  • I'm not interested in saving it, I want code to set it to a color without having to render it. – 8bitslime Dec 26 '13 at 20:31
  • The code in the first link I posted updates pixels in a BufferedImage without rendering it. – lreeder Dec 27 '13 at 14:13
  • I already set up a tile object (I'm working on tile-map rendering atm) with its image and a mask. when it renders the images it will lay a mask on top. Works great and using the animation capabilities I added to the tile object, I was able to get grass to flash different colors :P – 8bitslime Dec 28 '13 at 04:06

1 Answers1

1

Use Color.getHSBColor() to find a gamut of saturations or values for a given hue, as shown here for saturation. Use a suitable LookupOp to update the the image, as shown in the articles cited here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045