简体   繁体   English

使用setRGB设置0-255值而不是十六进制代码值

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

I am currently using the .setRGB() method. 我目前正在使用.setRGB()方法。 It appears that the input for the int[] rgbArray is a hex-code value, which are very large integer values. 看来int[] rgbArray的输入是十六进制代码值,这是非常大的整数值。 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. 我目前有一组范围在0到255之间的整数,因此,每当我使用.setRGB输入它们时,图像都是纯黑色的,考虑到十六进制值与RGB值的范围,这是有意义的。

I was wondering is there a way for me to use non-hex values? 我想知道是否可以使用非十六进制值?

It's not Hex; 不是十六进制; it's bit-fiddled RGB. 它是位经过处理的RGB。 I would use java.awt.Color and then call getRGB() . 我将使用java.awt.Color ,然后调用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. 正如在另一个答案中提到的那样,您所谈论的“大值”是颜色的R,G,B组合分量,可能还有“ Alpha”分量。

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. 因此,使用setRGB方法将值设置为255应该会导致该像素变成蓝色, 除非它是具有alpha通道的图像。 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: 但是,当您拥有介于0到255之间的value时,可以将其转换为RGB值(0 =黑色且255 =白色),如下所示:

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 ) (此处的Alpha分量设置为255。对于没有Alpha通道的图像,这不会做任何更改。但是对于具有Alpha通道的图像,这对于确保颜色实际可见至关重要)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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