How to convert r,g,b to RGB?
How can I convert a red/green/blue value (3 ints from 0-255) to a one int RGB value (used e.g. by BufferedIMage.setRGB())? I tried shifting, but my test returns another value than the Color.getRGB() call:
result =new Color(r, g, b).getRGB();//correct
int test = r << 16 | g << 8 | b;//different!
# 1
import java.awt.*;
public class TTTT {
public static void main(String[] args) {
int r = 15;
int g = 63;
int b = 127;
Color c = new Color(r, g, b);
int rgb = c.getRGB();
System.out.println(Integer.toBinaryString(rgb));
// as you can see, the first 8 bits are all 1
// this is the alpha component
int rgb2 = 255 << 24 | r << 16 | g << 8 | b;
System.out.println(rgb2 == rgb);
}
}