Controlling speed of pixel fading away

Hi, i usually use this trick if i want to fade away my pixels:

  
for( int i=0; i < lenght; i++ ) if (filmPixels[i] > 0) filmPixels[i] = filmPixels[i] - 1;  

But now I need to control the speed of the fading away, so instead of 1 im using a variable "value " controlled by a slider.
If my variable “value” is 0.1 it fade away very slow , so i need to increment that value in order to make it faster. I discovered that if my “value” is more than 2 then i get a wierd behaivor: when the image dissapear it appears again, but that behaivor is just when i go more than 2.

I would like to know why is this? and how can i make if i want to make it very fast (more than 2 without that wierd behavior) ?

thanks

  
  
  
for( int i=0; i < lenght; i++ ) if (filmPixels[i] > 0) filmPixels[i] = filmPixels[i] - value;  
  

hi adam,

not sure why the image would disappear and then reappear, but you may want to clamp your value to 0 if you are subtracting more than one each frame

  
  
    for( int i=0; i < lenght; i++ ) if (filmPixels[i] > 0) filmPixels[i] = MAX(filmPixels[i] - value, 0);    
  

Also I really like the effect that this produces, as it puts a natural ramp on the fade

  
  
   //value ranges from 0 (instantly black) to 1 (no change). generally numbers around .995 create nice slow fades  
    for( int i=0; i < lenght; i++ ) filmPixels[i] *= value;  
  

if this is for drawing an image there’s no need to loop through the pixels, you can just do:

  
  
.h  
ofColor color;  
int value;  
  
.cpp  
void setup(){  
color.set(255,255,255);  
value = .995;  
}  
  
void draw(){  
color *= value;  
ofSetColor(color);  
img.draw();  
}  
  

Hi , and which approach is faster? looping through pixels? or not?

Arturo’s method of setting the tint color is faster since the computation happens on the GPU rather than having to change the pixel values.

Good trick Arturo!