We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Color blending not blending properly
Page Index Toggle Pages: 1
Color blending not blending properly (Read 416 times)
Color blending not blending properly
Mar 31st, 2006, 3:57am
 
color c1 = color(0,0,127);
color c2 = color(0,0,127);
color c3 = blend(c1, c2, ADD);
color c4 = color(0,0,255);

print("c3="+c3+"\n");
print("c4="+c4+"\n");

These are the prints I get:

c3=-16776963
c4=-16776961

Why are these numbers not the same and why are they negative?
Re: Color blending not blending properly
Reply #1 - Mar 31st, 2006, 5:37am
 
Why dont you add the two values in c1 and c2 before putting them into c3?

I have no idea why c4 is like that, is there more code than this?
Re: Color blending not blending properly
Reply #2 - Mar 31st, 2006, 7:37am
 
The code I posted is just an example to show what problems I am having with color blend ADD method. If this problem could be explained I should be able to solve the problem I am having in my project.
Re: Color blending not blending properly
Reply #3 - Mar 31st, 2006, 11:11am
 
127+127 == 254 not 255...
However, the blend function seems to be making the blue component of c3 253 instead of 254.

The reason they're negative is that colour is just a single 32 bit int, with 8 bits each for alpha, red, green and blue, so printing it treats it as a single value, not 4 component values.

This function will print the values of a color nicely:
Quote:
void colorPrint(color c)
{
 int a=((c&0xFF000000)>>24)&0x000000FF;
 int r=(c&0x00FF0000)>>16;
 int g=(c&0x0000FF00)>>8;
 int b=(c&0x000000FF);
 println("Color("+r+","+g+","+b+","+a+")");
}
Re: Color blending not blending properly
Reply #4 - Mar 31st, 2006, 10:35pm
 
Thanks for the print function. It appears that when ever blending two colors that are odd the new color will be 1 less then it should be.

This problem enlarges when adding more colors together:

color c1 = color(0,0,5);
color c2 = color(0,0,5);
color c4 = color(0,0,5);
color c3 = blend(c1, c2, ADD);
color c5 = blend(c3, c4, ADD);

void setup() {
 colorPrint(c5);
}
void colorPrint(color c){
 int a=((c&0xFF000000)>>24)&0x000000FF;
 int r=(c&0x00FF0000)>>16;
 int g=(c&0x0000FF00)>>8;
 int b=(c&0x000000FF);
 println("Color("+r+","+g+","+b+","+a+")");
}

The print = Color(0,0,13,255)

This appears to be the problem I am having - when I add colors over and over again, in time, the new color goes to black.
Page Index Toggle Pages: 1