otisonoza
otisonoza

Reputation: 1344

Construct a pixel from RGB values

I have RGB values using this:

int pixel = image[x][y];

red = ((pixel & 0xff0000) >> 16);
green = ((pixel & 0x00ff00) >> 8);
blue = (pixel & 0x0000ff);

Then I modify red, green and blue.

How do I construct a new pixel with the new values?

Upvotes: 1

Views: 252

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

You want to make sure that red, green, blue are all within 0..255 range. Then you get them together:

int newPixel = 
  ((int) max(0, min(red, 0xFF)) << 16) |
  ((int) max(0, min(green, 0xFF)) << 8) |
  max(0, min(blue, 0xFF));

Upvotes: 3

Related Questions