Super simple code. What am I missing?

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???


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);
}

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:

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:

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:

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);
}
2 Likes

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!!

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! :slight_smile:

Hello,

It is not a bug.

The color primitive is a 32-bit int :

Processing has a colorMode() 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:

Additional references:
https://processing.org/reference/color_datatype.html
https://processing.org/tutorials/color/
https://processing.org/reference/red_.html