FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   brightness fade
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: brightness fade  (Read 1232 times)
LigaMainz

WWW
brightness fade
« on: Sep 13th, 2003, 2:11am »

hi. i am just checking bit-wise image calculating.
i made a brightness/darkness-fade and now i am wondering if there is any better/faster way to do this.
Especially for the lines of code where i check if the single colors are larger than 255 or smaller than 0 there should be a more elegant way.
 
Code:

int darkenPixel(int org, int step){
  int r1=(org & 0xff0000) - (step << 16);
  int g1=(org & 0x00ff00) - (step << 8) ;
  int b1=(org & 0x0000ff) - (step) ;
 
  if(r1 < 0x000000) r1 = 0x000000;
  if(g1 < 0x000000) g1 = 0x000000;
  if(b1 < 0x000000) b1 = 0x000000;
 
  return (( r1 ) | ( g1 ) | b1 ) | 0xff000000;
}
 
int brightenPixel(int org, int step){
  int r1=(org & 0xff0000)+ (step << 16);
  int g1=(org & 0x00ff00)+ (step << 8) ;
  int b1=(org & 0x0000ff)+ (step) ;
 
  if(r1 > 0xff0000) r1 = 0xff0000;
  if(g1 > 0x00ff00) g1 = 0x00ff00;
  if(b1 > 0x0000ff) b1 = 0x0000ff;
 
  return (( r1 ) | ( g1 ) | b1 ) | 0xff000000;
}
 

 
And here the link:
http://www.say-nono.com/main/sketchbook/index.php?file=brightness
 
 
Vince
 
LigaMainz

WWW
Re: brightness fade
« Reply #1 on: Sep 13th, 2003, 3:45am »

ok.
i think i found the best and most flexible way.
It isn't my code but i think it may be useful for other beginners like me to check bitwise image-calculating.
 
Code:

int colorFade(int org, int mix, float percent){
   
  // seperate the single color values of the original color by bitshifting
  int r1=(org & 0xff0000)>>16;
  int g1=(org & 0x00ff00)>>8;
  int b1=(org & 0x0000ff);
 
  // seperate the single color values of the mixcolor by bitshifting
  int r2=(mix & 0xff0000)>>16;
  int g2=(mix & 0x00ff00)>>8;
  int b2=(mix & 0x0000ff);
 
  // calculate the difference between the orignial color and the mixingcolor
  // multiplicate this value with the fadevalue and add the result to the original color
  int r3=(int)((r2-r1)*percent) + r1;
  int g3=(int)((g2-g1)*percent) + g1;
  int b3=(int)((b2-b1)*percent) + b1;
 
  // compose the three colors again by bitshifting
  return (( r3<<16 ) | ( g3<<8 ) | b3 ) | 0xff000000;
}

 
I think i saw a similar piece of code in this very beautiful example by zevan:
http://www.shapevent.com/proc/diag/
He refers to http://www.bodytag.org.
 
this is my example:
http://www.say-nono.com/main/sketchbook/index.php?file=colorfade
 
(Is there a better way than this?)
 
Vince
 
Pages: 1 

« Previous topic | Next topic »