55

So in a BufferedImage, you receive a single integer that has the RGB values represented in it. So far I use the following to get the RGB values from it:

// rgbs is an array of integers, every single integer represents the
// RGB values combined in some way
int r = (int) ((Math.pow(256,3) + rgbs[k]) / 65536);
int g = (int) (((Math.pow(256,3) + rgbs[k]) / 256 ) % 256 );
int b = (int) ((Math.pow(256,3) + rgbs[k]) % 256);

And so far, it works.

What I need to do is figure out how to get an integer so I can use BufferedImage.setRGB(), because that takes the same type of data it gave me.

Marcus Mangelsdorf
  • 2,852
  • 1
  • 30
  • 40
Rahat Ahmed
  • 2,191
  • 2
  • 30
  • 40

5 Answers5

111

I think the code is something like:

int rgb = red;
rgb = (rgb << 8) + green;
rgb = (rgb << 8) + blue;

Also, I believe you can get the individual values using:

int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I don't think you need an int on the second two lines. And the third line is supposed to be "rgb = ...", right? – Rahat Ahmed Jan 26 '11 at 04:31
  • It is a single integer. An integer contains 32 bits. The first 8 bits are for the alpha value (which is zero). The next 8 for the red, then next 8 for the green and the next 8 for the blue. Try it. If the final output equals you initial input then you know the conversion was done correctly. – camickr Jan 26 '11 at 04:32
  • Erm, I think it would depend on the color depth. Your example looks like 24-bit color to me (8 bits per channel) - is that what `BufferedImage` expects? – Matt Ball Jan 26 '11 at 04:33
  • Yes, I made the assumption the buffered image is of type BufferedImage.TYPE_INT_RGB. Maybe it isn't which is why the other formula is being used. In any case the concept is that (as far as i know) it is easier to shift bits to get the individual values instead of using the code posted. – camickr Jan 26 '11 at 04:37
  • what are the values for red, green and blue in your first code sample – Golden Lion Jan 18 '22 at 15:39
  • @GoldenLion, they are the individual values between 0-255 for each of your red/green/blue values. In the question r/g/b is used as the variable names. – camickr Jan 18 '22 at 19:28
  • def colorValue(value): retVal=((value[0]&0x0ff)<<16)|((value[1]&0x0ff)<<8)|(value[2]&0x0ff) return retVal If I pass in (255,120,111) will I get the correct value? – Golden Lion Jan 18 '22 at 19:48
36
int rgb = ((r&0x0ff)<<16)|((g&0x0ff)<<8)|(b&0x0ff);

If you know that your r, g, and b values are never > 255 or < 0 you don't need the &0x0ff

Additionaly

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

No need for multipling.

KitsuneYMG
  • 12,753
  • 4
  • 37
  • 58
29

if r, g, b = 3 integer values from 0 to 255 for each color

then

rgb = 65536 * r + 256 * g + b;

the single rgb value is the composite value of r,g,b combined for a total of 16777216 possible shades.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
Jay
  • 299
  • 2
  • 4
27
int rgb = new Color(r, g, b).getRGB();
taskinoor
  • 45,586
  • 12
  • 116
  • 142
Nicholas
  • 295
  • 3
  • 2
6

To get individual colour values you can use Color like following for pixel(x,y).

import java.awt.Color;
import java.awt.image.BufferedImage;

Color c = new Color(buffOriginalImage.getRGB(x,y));
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();

The above will give you the integer values of Red, Green and Blue in range of 0 to 255.

To set the values from RGB you can do so by:

Color myColour = new Color(red, green, blue);
int rgb = myColour.getRGB();

//Change the pixel at (x,y) ti rgb value
image.setRGB(x, y, rgb);

Please be advised that the above changes the value of a single pixel. So if you need to change the value entire image you may need to iterate over the image using two for loops.

Ajay Sant
  • 665
  • 1
  • 11
  • 21