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)
   applying alpha to color vars
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: applying alpha to color vars  (Read 400 times)
sspboyd


applying alpha to color vars
« on: Apr 20th, 2004, 7:24pm »

Is it possible to give a color variable an alpha value after it's been declared? Eg. fill(colorVar, alpha);
 
I want to use a seven colour palate with random alpha values. I created a color array and defined my colours without alpha values. I have attempted to use the colours with an added the alpha in the fill() statement. What I get is a gray colour with the alpah value. Is there another way of doing this?
 
The following example shows what I am getting:
Code:

color[] carray = new color[7]; // create color array
 
carray[0]=color(204,204,204); // add colours w/o alpha
carray[1]=color(255,255,0);
carray[2]=color(204,255,255);
carray[3]=color(0,255,0);
carray[4]=color(255,0,255);
carray[5]=color(255,93,15);
carray[6]=color(0,33,255);
 
fill(carray[1]); // yellow fill
rect(10,10,40,40); // yellow rect
fill(carray[1], 126); // yellow fill, 50% alpha?
rect(30,30,40,40); // not yellow rect

 
steve
 

gmail.com w/ sspboyd username
sspboyd


Re: applying alpha to color vars
« Reply #1 on: Apr 20th, 2004, 9:02pm »

this is compromise i've come up with. It works but it isn't very elegant.
 
Code:

int[][] carray = new int[2][3]; // store RGB colour vals
int rc; // to store the colour selection
 
carray[0][0]=204;
carray[0][1]=20;
carray[0][2]=47;
 
carray[1][0]=255;
carray[1][1]=255;
carray[1][2]=0;
 
rc=0;
fill(carray[rc][0],carray[rc][1],carray[rc][2],random(55,155)); // R,G,B,A
rect(10,10,30,30);
 
rc=1;
fill(carray[rc][0],carray[rc][1],carray[rc][2],random(25,55)); // R,G,B,A
rect(30,30,30,30);
 

gmail.com w/ sspboyd username
TomC

WWW
Re: applying alpha to color vars
« Reply #2 on: Apr 20th, 2004, 10:39pm »

You could define a function which takes a color and an alpha value and returns a new color.
 
I'm fairly confident this should work:
 
Code:

color colorWithAlpha(color col, int alpha) {
  return color(red(col),green(col),blue(col),alpha);
}

 
So now you can say:
 
Code:

fill(colorWithAlpha(carray[1], 127)); // yellow fill, 50% alpha
rect(30,30,40,40); // 50% yellow rect  

 
An alternative way to do the same thing a bit faster (probably) using bit shifts, would be...
 
Code:

color colorWithAlpha(color col, int alpha) {
  return (alpha << 24) | ((col << 8) >>> 8);
}

 
update  I've just noticed that fill(color, alpha) is in the reference section, so it should work.  I'll wait a while and see if fry picks it up as a bug, otherwise I'll repost on the bug forum.
 
I guess it's a problem with the fact that colors are represented internally as ints, so there's no real difference between fill(int grey, int alpha) and fill(color col, int alpha).  Here's a fix for fill, so that your original code would work...
 
Code:

void fill(int col, int alpha) {
  if ((col & 0xff000000) != 0) {
    // if col has an alpha component (could just check for col > 255?)
    super.fill((alpha << 24) | ((col << 8) >>> 8));
  }
  else {
    // otherwise you must have meant grey
    super.fill(col,alpha);
  }
}
« Last Edit: Apr 20th, 2004, 10:50pm by TomC »  
fry


WWW
Re: applying alpha to color vars
« Reply #3 on: Apr 21st, 2004, 12:13am »

right, the distinction is that it's fill(gray, alpha) to go with fill(gray).
 
however, since we cheat and allow fill(color c), i suppose we could use the same logic to figure out whether it's fill(color c, float alpha) or fill(float gray, float alpha).. though i wonder if we aren't already setting ourselves up for trouble with that..
 
for the technically minded.. since a 'color' is just an int, internally the fill and stroke functions check to see if higher bits of the value are set, and if so, determines whether you meant:
fill(int gray) or fill(int some_color)  
[ the latter is the same as fill(color some_color) ]  
this mostly works, but there's a problem where if a value is zero, that comes out as simply opaque black (no higher bits set), instead of, say alpha 0 on a black color. the latter you'd run into a problem with if you tried to, say, fade a black rectangle by setting its alpha.. if that makes sense.
 
Pages: 1 

« Previous topic | Next topic »