Question

I got the red and blue values of a pixel from get.RGB and using bitwise operators:

            red = (rgb[i][j] >> 8) & 0xFF;
            blue = rgb[i][j] & 0xFF;

But how do I recolour the pixel in question with the "average of red and blue values"?

This means I have to exclude the green.

The only required import is BufferedImage.

Was it helpful?

Solution

You question is really unclear. Ask your teacher what it is you are trying to achieve and then maybe we can help doing so. Here are some elements that can help you.

Get red, green and blue for an RGB number

You got the first part almost right. Remember the colors returned by getRGB() are on 8 bits each, so here is how to get them:

int red = (rgb[i][j] >> 16) & 0xFF; 
int green = (rgb[i][j] >> 8) & 0xFF;
int blue = rgb[i][j] & 0xFF;

Build an RGB number from red, green and blue values

If your problem is how to recombine rgb colors into one number, here is how to do that:

pixelColor = (red << 16) + (green << 8) + blue

Doing what you want with it

The average of red and blue is really obvious, this is the formula you pointed out:

int average = (red + blue) / 2;

If what you want is a purple pixel using the average of red and green, then you just have to set red and blue to the average value, and green to 0:

int pixelColor = (average << 16) + average // using average for red and blue, and 0 for green

EDIT: Seems that you don't want to take out the green after all.

int pixelColor = (average << 16) + (green << 8) + average // using average for red and blue, and keep the green unchanged

EDIT: Or maybe you want the green to take the average of red and blue?

int pixelColor = (red << 16) + (average << 8) + blue; // keep red and bllue, and put their average as the green color
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top