# Super simple code. What am I missing?

**URL:** https://discourse.processing.org/t/super-simple-code-what-am-i-missing/17342
**Category:** Coding Questions
**Created:** [January 26, 2020, 6:51am UTC](https://discourse.processing.org/t/super-simple-code-what-am-i-missing/17342 "2020-01-26T06:51:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Tomfoolery](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@Tomfoolery](https://discourse.processing.org/u/Tomfoolery)
#### Post date: [January 26, 2020, 6:51am UTC](https://discourse.processing.org/t/super-simple-code-what-am-i-missing/17342/1 "2020-01-26T06:51:38Z")

</div>

I’ll probably kick myself when somebody answers this but I just can’t figure out what’s wrong with my code. All I want is to increase the red value by .01 per frame. When I run it, the value doesn’t change at all. If I replace the += with -=, it subtracts just fine. If i replace the .01 with anything over 1, it adds just fine. But for some reason r += .01 refuses to do anything! Please help! What’s going on???

```auto

color c;

void setup()
{
  size(600, 600);
  c = color(100, 0, 0);
}

void draw()
{
  background(c);
  float r = red(c);
  r += .01;         
  c = color(r, 0, 0);
  println(r);
}

```

---

<div class="post-metadata">

### Author: ![J0R1AN](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/j0r1an/32/7498_2.png) [@J0R1AN](https://discourse.processing.org/u/J0R1AN)
#### Post date: [January 26, 2020, 7:28am UTC](https://discourse.processing.org/t/super-simple-code-what-am-i-missing/17342/2 "2020-01-26T07:28:57Z")

</div>

I don’t really see why your code isn’t working. My only guess is that the `red()` function just takes the integer value of that color. It gets increased by .01 each time and that is not enough to increase the integer value. I’ll explain what happens:

**First frame:**

```auto
void draw()
{
  background(c); // color is used
  float r = red(c); // r is set to 100 in first frame
  r += .01; // r becommes 100.01
  c = color(r, 0, 0); // c gets updates to the right value
  println(r); // r gets printed correctly as 100.01
}

```

**Second frame:**

```auto
void draw()
{
  background(c); // color is used (100.01, 0, 0)
  float r = red(c); // r is set to integer red value of 100
  r += .01; // r becommes 100.01
  c = color(r, 0, 0); // c gets updates to the right value
  println(r); // r gets printed correctly as 100.01
}

```

What I should do to fix this is not set r to red( c). If you just declare the variable at the top of the program and then not set it again processing will remember that value.  
**Here are the changes:**

```auto
color c;
float r; // Declare red here already

void setup()
{
  size(600, 600);
  c = color(100, 0, 0);
  r = red(c); // Set the initial value of 100
}

void draw()
{
  background(c);
  r += .01; // Only increment it, not set it to red(c)
  c = color(r, 0, 0);
  println(r);
}

```

---

<div class="post-metadata">

### Author: ![Tomfoolery](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@Tomfoolery](https://discourse.processing.org/u/Tomfoolery)
#### Post date: [January 26, 2020, 7:45am UTC](https://discourse.processing.org/t/super-simple-code-what-am-i-missing/17342/3 "2020-01-26T07:45:56Z")

</div>

Thanks a lot for the reply. Your solution works, but I’m still baffled by the problem. The processing reference says that red() always returns a float, and aside from that, the program runs without issue if I decrement the value instead of incrementing it…

Oh wait!! It’s not that decrementing works, it’s that the decimal keeps getting chopped off reducing the value by a whole number. That also explains why the color change was so much faster than I was expecting.

It still doesn’t make sense to me that I’m getting integer problems when everything’s running as a float (maybe color stores its values as int?), but I’ll take your solution and run with it.  
Thanks!!

---

<div class="post-metadata">

### Author: ![J0R1AN](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/j0r1an/32/7498_2.png) [@J0R1AN](https://discourse.processing.org/u/J0R1AN)
#### Post date: [January 27, 2020, 7:31pm UTC](https://discourse.processing.org/t/super-simple-code-what-am-i-missing/17342/4 "2020-01-27T19:31:39Z")

</div>

About the color values, I think you’re right. A variable of type `color` is stored as an `int`, you can see this when you try to print a color. This probably means that the red, green and blue values also get stored as in int.  
Interesting bug! 🙂

---

<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: [February 1, 2020, 10:13pm UTC](https://discourse.processing.org/t/super-simple-code-what-am-i-missing/17342/5 "2020-02-01T22:13:59Z")

</div>

Hello,

It is not a bug.

The color primitive is a 32-bit _int_ :

> <https://github.com/processing/processing/blob/master/core/src/processing/core/PGraphics.java#L7787-L7793>

Processing has a [colorMode()](https://processing.org/reference/colorMode_.html) which “Changes the way Processing interprets color data” and you see a lot of functions that allow use of floats.

An exploration into the source helps to understand what is going on under the hood.

This is the red() function:

> <https://github.com/processing/processing/blob/master/core/src/processing/core/PGraphics.java#L7934-L7937>

Additional references:  
[https://processing.org/reference/color\_datatype.html](https://processing.org/reference/color_datatype.html)  
[https://processing.org/tutorials/color/](https://processing.org/tutorials/color/)  
[https://processing.org/reference/red\_.html](https://processing.org/reference/red_.html)
