By calling getRGB(int x, int y)
with a BufferedImage
object, one gets a single, negative number.
How can I convert three different values (a red, a green, and a blue) into this single, negative number?
By calling getRGB(int x, int y)
with a BufferedImage
object, one gets a single, negative number.
How can I convert three different values (a red, a green, and a blue) into this single, negative number?
Using the Color class:
new Color(r, g, b).getRGB()
BufferedImage ends up delegating to java.awt.image.ColorModel
which uses the following code:
public int getRGB(Object inData) {
return (getAlpha(inData) << 24)
| (getRed(inData) << 16)
| (getGreen(inData) << 8)
| (getBlue(inData) << 0);
}
Modifying this to suit your needs is a trivial exercise.
JB Nizet's answer is great, but it can be really slow when creating new objects of type 'Color' thousands of times. The simplest way would be:
int rgb = (red << 16 | green << 8 | blue)
As answered by ByteBit