Вопрос

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)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top