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

**URL:** https://discourse.processing.org/t/posterize-question-what-does-this-statement-mean-set-the-pixel-at-the-current-index-to-r-g-b/24862
**Category:** Coding Questions
**Tags:** homework
**Created:** [October 23, 2020, 9:19pm UTC](https://discourse.processing.org/t/posterize-question-what-does-this-statement-mean-set-the-pixel-at-the-current-index-to-r-g-b/24862 "2020-10-23T21:19:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![thughes](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@thughes](https://discourse.processing.org/u/thughes)
#### Post date: [October 23, 2020, 9:19pm UTC](https://discourse.processing.org/t/posterize-question-what-does-this-statement-mean-set-the-pixel-at-the-current-index-to-r-g-b/24862/1 "2020-10-23T21:19:07Z")

</div>

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.**

```auto
PImage img;  

public void setup() {

  size(468, 311);

  img = loadImage("presents.jpg");

  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()

{
}

```

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [October 23, 2020, 9:30pm UTC](https://discourse.processing.org/t/posterize-question-what-does-this-statement-mean-set-the-pixel-at-the-current-index-to-r-g-b/24862/2 "2020-10-23T21:30:36Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)
#### Post date: [October 23, 2020, 11:58pm UTC](https://discourse.processing.org/t/posterize-question-what-does-this-statement-mean-set-the-pixel-at-the-current-index-to-r-g-b/24862/3 "2020-10-23T23:58:09Z")

</div>

I wasn’t 100% sure what “posterize” really means, so I [looked it up](https://en.wikipedia.org/wiki/Posterization).  
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).

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [October 24, 2020, 1:40am UTC](https://discourse.processing.org/t/posterize-question-what-does-this-statement-mean-set-the-pixel-at-the-current-index-to-r-g-b/24862/4 "2020-10-24T01:40:14Z")

</div>

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?

> [@thughes](#):
>
> `grayAmount = (int)Math.round((r * 0.299) + (g * 0.587) + (b * 0.114)) ;`

`:)`

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/340d5cf3b2e77744cc498faa0b640f7d23bfab93.jpeg)

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8e22461f3fc9842c93640a0a41f443e90de4c7d9.png)

References:  
_[https://processing.org/tutorials/pixels/](https://processing.org/tutorials/pixels/)_
