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_
   Topics & Contributions
   Tools
(Moderator: REAS)
   Blending colors
« Previous topic | No topic »

Pages: 1 
   Author  Topic: Blending colors  (Read 2698 times)
Glen Murphy

WWW Email
Blending colors
« on: Oct 30th, 2002, 10:06am »

The 'blend' function takes two colours as input, and blends them according to the alpha value you supply.  
 
 
int blend(int org, int col, int alpha) {
  // org is original colour
  // col is colour to add
  // alpha is a value between 0 and 255,  
  // where 0 makes 'col' completely opaque
 
  int r1=(org&0x0000ff);
  int g1=(org&0x00ff00);
  int b1=(org&0xff0000);
  int r2=(col&0x0000ff);
  int g2=(col&0x00ff00);
  int b2=(col&0xff0000);
  
  int r3=(((alpha*(r1-r2)) >>8 )+r2)&0x000000ff;
  int g3=(((alpha*(g1-g2)) >>8 )+g2)&0x0000ff00;
  int b3=(((alpha*(b1-b2)) >>8 )+b2)&0x00ff0000;
 
  return (r3)|(g3)|(b3);
  }
 
 
 
// The following code shows an example of this
// in action. It uses a pixel buffer because  
// background() can't seem to take an int value
// eg 0x00ff0000
 
int pix[] = new int[320*320];
 
void setup() {
  size(320, 320);
  for(int i=0; i<320*320; i++) {
    pix[i] = 0xff0000;
    }
  }
 
void loop() {
  for(int i=0; i<320*320; i++) {
    pix[i] = blend(pix[i], 0x0000ff, 254);
    setPixel(i%320, (int)(i/320), pix[i]);
    }
  }
« Last Edit: Oct 31st, 2002, 12:42am by Processing »  
Pages: 1 

« Previous topic | No topic »