swaroop_ps
swaroop_ps

Reputation: 125

convert pixel values into RGB format

I a have the red, green and blue values of a pixel seperately. How to convert them into RBG format to create a new image? I basically need a reverse process for this:

int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;

Upvotes: 3

Views: 3632

Answers (2)

Alba Mendez
Alba Mendez

Reputation: 4645

It's a great practice to use the java.awt.Color class instead.
It will make the things simpler and easier. In your case it would be:

Color myColor = new Color(red, green, blue); //Construct the color
myColor.getRGB(); //Get the RGB of the constructed color

Or the inverse:

Color myColor = new Color(rgb); //Construct the color with the RGB value
myColor.getRed(); //Get the separate components of the constructed color
myColor.getGreen();
myColor.getBlue();

For more info, consult the Javadocs.

Upvotes: 0

James
James

Reputation: 9288

int rgb = ((r << 16) | ((g << 8) | b);

assuming it's RGB888. Make sure r,g and b are all in the 0-255 range

Upvotes: 5

Related Questions