JAVA: Substitute one color with another using Colo

2019-07-25 04:41发布

问题:

I need to substitute one color with white on an image. I've read about ColorModel and RGBImageFilter.substituteColorModel, but I don't understand very well.

The color I want to substitute is: R: 113 G: 75 B: 96

And of course, white is 255,255,255.

If you can give a direct solution will be great, but if not, a bit of explanation on how to do it also will be great. I don't want just the solution, I want to understand the hows and whys.

Thank u so much.

回答1:

At my company we have to do this sort of switching frequently on embedded displays. We use indexed bitmaps to accomplish what you are talking about. The basic idea is to switch out the palette for a given index to get different looks and feels.

The idea with an indexed bitmap is that you have say 256 colors at your disposal in the pallete. You can assign any RGB values that you want to each slot in the palette. The image itself is just a list of indexes into the palette(single byte per pixel). This is really cool on CPU and storage constrained platform(where you can't deal with decompressing images and you can't spare the space for full color bitmaps). You can make alternate palettes (greens, yellows, reds, etc). You just switch out the palette and the graphics look completely different. We use this to make really fine gradients on widgets that can switch color without having to carry around each state of a button.

To solve your specific problem with indexed bitmaps, you would just switch palettes and make sure that in one palette the index was (113,75,96) and in the second palette that same index held (255,255,255).

IndexColorModel is a good place to start in AWT.

Good luck!