# Posterize question-What does this statement mean(set the pixel at the current index to r,g,b.)?

I am supposed to posterize an image with purple and green. This is the code I am working with. I am trying to set the pixel at the current index to r,g,b.

What exactly does it mean to::: set the pixel at the current index to r,g,b.

``````PImage img;

public void setup() {

size(468, 311);

for (int i = 0; i < img.pixels.length; i++)

{

color c = img.pixels[i];

float r = red(c);

float g = green(c);

float b = blue(c);

img.pixels[i] = color(r, g, b);

int grayAmount;

grayAmount = (int)Math.round((r * 0.299) + (g * 0.587) + (b * 0.114)) ;

if ( grayAmount < 64 )
{
r = 0;
g = 0;
b = 255;
} else if ( grayAmount < 128 )
{
r = 255;
g = 0;
b = 255;
} else if ( grayAmount < 192 )
{
r = 0;
g= 255;
b = 0;
} else
{
r = 255;
g = 255;
b = 0;
}
}

img.updatePixels();

image(img, 0, 0, width, height);
}

public void draw()

{
}
``````

well you are already accessing and setting the pixel at index i in this section of the code.

``````color c = img.pixels[i];

float r = red(c);

float g = green(c);

float b = blue(c);

img.pixels[i] = color(r, g, b);
``````

At this point you’ve just grabbed the individual rgb values of the pixels and you’ve put them straight back in to the pixel without any changes. If you want to make further changes to your pixel you’ll have to call `img.pixels[i] = color(r, g, b);` but only after the r or g or b values have changed.

1 Like

I wasn’t 100% sure what “posterize” really means, so I looked it up.
So basically it’s reducing the bit depth in some way.

One way is to have the “continuous” (integer) range 0 - 255 be not so continuous, but go in some larger steps like every 16th step for example (collect all 0-15 as just 0, 16-31 as 16, 32-47 as 32 etc. Basically integer division by some number (discarding decimals), then multiply result by same number).

c = (c/16) * 16

Or some other method.

Maybe the task you have is to produce two versions, one posterizing the purple and one the green colors?

Btw, you may or may not have known what “posterize” is, but it’s unclear from your post.

Also, the title is useless and says nothing about what you want to know, unless one opens and read the post (I’d suggest editing it, you may get more responses that way too).

Hello,

From your previous homework examples you are expected to correct errors in the code.

@paulgoux pointed out one error.

Once you correct that you will see that it is posterizing to the 4 colors in your code based on the grayAmount.

This is already working code and simple enough to adapt to posterize to purple and green.

@thughes,
Can you explain what this is doing?

`:)`  References:
https://processing.org/tutorials/pixels/