Hi all,
I posted a topic similar to this some months ago but it was never resolved so I thought I might try again.
I have a series of monochrome images that I have to dynamically recolor in my application. There are about
20 images, and a total of 8 possible color schemes for all of them- meaning either I recolor in Java or create
160 images in total, which I'd rather not do if I need to change the colors for whatever reason.
The following code shows the method I'm using, with a printout showing where things go wrong:
//Our image has been loaded from file and displays fine as is, and the color is an sRGB value extracted from a Color object. It's //values are tested and work correctly. protected final void colorizeImage(BufferedImage image, int color){ for(int i = 0; i < image.getWidth(); i++){ for(int j = 0; j < image.getHeight(); j++){ if(image.getRGB(i,j) != 0){ //0 represents a transparent pixel, and should not be painted image.setRGB(i, j, color); if(image.getRGB(i, j) != color) System.out.println("Incorrect- Expected " + (new Color(color)) + ", but found " + (new Color(image.getRGB(i, j)))); } } } }
I find when running this code at present the call will happen quite frequently, usually uniformly for one image indicating
one color is incorrect. The color for the whole image is the same, but not the color I specified. I cannot seem to determine
any consistency from the errors occurring either; some colors will work fine in certain circumstances and others will be
completely incorrect.
I expect the problem has to do either with the way I'm using/working with buffered images, but I really can't tell.
If anyone reading this post has a bit of experience working with dynamically recoloring images, please let me know.
I'm more than happy to provide the images or additional source code if it would help the problem.
Thanks for reading,
Nitrogen Fingers