简体   繁体   中英

Using setRGB to set 0-255 values instead of hex code values

I am currently using the .setRGB() method. It appears that the input for the int[] rgbArray is a hex-code value, which are very large integer values. I currently have a set of integers that range from 0 - 255, so whenever I input them using .setRGB , the image is just pure black, which makes sense given the range of hex values versus RGB values.

I was wondering is there a way for me to use non-hex values?

It's not Hex; it's bit-fiddled RGB. I would use java.awt.Color and then call getRGB() . So,

int rgb = new Color(red, green, blue).getRGB();

As mentioned in the other answer, the "large values" that you are talking about are the combined R,G,B components of the color, and possibly an "Alpha" component.

Setting a value of 255 with the setRGB method should thus cause the pixel to become perfeclty blue - unless it is an image that has an alpha channel. In this case it would become a perfectly transparent blue (aka "black").

However, when you have a value between 0 and 255, you can convert it into an RGB value (with 0=black and 255=white) as follows:

int value = ...
int rgb = (255 << 24) | (value << 16) | (value << 8) | value;

(The alpha component here is set to 255. For images without alpha channel, this will not change anything. But for images that have an alpha channel, this is crucial to make sure that the color is actually visible )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM