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
(Moderators: fry, REAS)
   making graphics fade out
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: making graphics fade out  (Read 731 times)

WWW Email
making graphics fade out
« on: Nov 30th, 2002, 3:40pm »

One feature that I miss in P5 is the possibility to create colors with a fade - like in this Java-example:
memDraw.setColor(new Color(0,0,0,5));
so, I guess the work-around (for now?? will transparent colors be supported in future releases?) is to manipulate the pixels[] array...
But, I need help on this one! As far as I have understood, pixels[] contains a list of all the single pixels colors - but: not as RGB values, but a single integer... if it was RGB values, I would presume I could just take each value and divide or subtract it with a number, which would result in the single R, G and B values to become for example black (0) - and visually you would see graphics fade into a black background.
How to I do this with a single integer? I have tried this basic example:
// Basic Fading Effect v. 0.1
// Mikkel Crone Köser, 2002
// www.beyondthree.com
int tempColor;
void setup(){
  fill(255, 0, 0);
void loop(){
  for(int x=0; x < width; x++){
    for(int y=0; y < height; y++){
 tempColor = getPixel(x, y); // Returns an integer
 setPixel(x, y, int(tempColor/0.50));
  ellipse(mouseX, mouseY, 35, 35);
...the sad thing about this coding is, that doing the rather heavy loop slows down performance AND, I cannot set the transparency level - anything else than dividing with 0.50 (in the setPixel line) result in a rather strange colorTransformation.
I have done a small experiment in standard Java 1.4.1, which does the fade I am hoping to work out in P5:
- mikkel

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com

Re: making graphics fade out
« Reply #1 on: Nov 30th, 2002, 11:37pm »

do you mean alpha that will be supported in the future (it's slated for at least by beta), along with antialiasing.
the colors in the pixels[] array are in fact rgb, but they're packed into a single int. more discussion on that here:
so for the time being, alpha, as you found, can be hacked. the 'blend' function below does the alpha compositing of two pixel colors, so p1 would be one color (as an int), p2 would be an elements from pixels[], and a2 is the alpha value (0..255).  
 static public final int blend(int p1, int p2, int a2) {
    // scale alpha by alpha of incoming pixel
    a2 = (a2 * ((p2 >> 24) & 0xff)) >> 8;
    int a1 = 255 - a2;
    int r = (a1*((p1 >> 16) & 0xff) + a2*((p2 >> 16) & 0xff)) >> 8;
    int g = (a1*((p1 >>  8) & 0xff) + a2*((p2 >>  8) & 0xff)) >> 8;
    int b = (a1*((p1 ) & 0xff) + a2*((p2 ) & 0xff)) >> 8;
    return 0xff000000 | (r << 16) | (g << 8) | b;

not terribly fast, but this code can be simplified in cases where you have a less general need (ie, if the shape is a rect, do this computation only once).
this doesn't get you all the way there, but a little more hacking should do it.
Pages: 1 

« Previous topic | Next topic »