JAVA: Substitute one color with another using ColorModel

℡╲_俬逩灬. 提交于 2019-12-11 04:34:17

问题


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!



来源:https://stackoverflow.com/questions/9882174/java-substitute-one-color-with-another-using-colormodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!