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.
IndexDiscussionExhibition › LerpColor in a Loop Problem
Page Index Toggle Pages: 1
LerpColor in a Loop Problem (Read 1397 times)
LerpColor in a Loop Problem
Nov 2nd, 2009, 11:38am
 
I am trying to create a background that is a gradient scale from left to right(black to blue). I have set up the lerpColor in a loop and when i start the program, the whole screen stays black and I can not figure out why? can someone help me? here is my code

void setup () {
 size (1000,600);
}

float xBack = 1;

void draw () {
background (255);
 color from = color ( 0, 0, 0);
 color to = color ( 0, 0, 255);


 fill (from);
 rect ( 0, 0, 1, 600);


 for ( float xBack = 1; xBack < 999; xBack = xBack + 1) {
   color interA = lerpColor(from, to, .255);
   fill (interA);
   rect ( xBack, 0, 1, 600);
 }

 fill (to);
 rect (999, 0, 1, 600);

}
Re: LerpColor in a Loop Problem
Reply #1 - Nov 2nd, 2009, 12:03pm
 
one of the major problems is that you use lerpColor but doesnt change the amt parameter (lerpColor(c1, c2, amt)) so even if it would work, the color wouldnt change. But beside that another problem is that you have to call noStroke. cause right now, you are drawing a thin black line arrounc your rects so you dont see the color of the rect.

but a few more things.
Your sketch is much more flexibel if you use, width, and height. as variables. for example. XBack< width . so you can change the size without changing all the paramters. Another thing. You can easily write xBack++ instead of xBack = xBack + 1;
and there was no need to call the first and the last rect outside of the loop. Just start with 0 ...

and i would probably use line instead of rects...

void setup () {
size (1000,600);
}

color from = color ( 0, 0, 0);
color to = color ( 0, 0, 255);

void draw () {
 for ( int i = 0; i < width; i++) {
   color interA = lerpColor(from, to, (float)i/width);
   stroke(interA);
   line(i,0,i,height);
 }
}
Re: LerpColor in a Loop Problem
Reply #2 - Nov 2nd, 2009, 12:05pm
 
Ohhh. and please!... why is everybody posting his questions in the exhibition section today.
Re: LerpColor in a Loop Problem
Reply #3 - Nov 2nd, 2009, 12:18pm
 
Well it is at the top of the list of sections Roll Eyes
Re: LerpColor in a Loop Problem
Reply #4 - Nov 2nd, 2009, 12:23pm
 
hmm. im trying all of this. could you give me an example of the code that would need to go in, using lines or rectangles. im trying what you say and i am now getting a shade of blue, depending on the number i use for "amt" between 0.0 and 1.0.

i want the background to be gradient with black(0,0,0) on the left and blue(0,0,255) on the right and just filled in between.

i posted here because i saw someone else post a question here.

thanks!
Re: LerpColor in a Loop Problem
Reply #5 - Nov 2nd, 2009, 12:25pm
 
did you try the code i added to my post. it does exactly what you are asking for. doesnt it ?
Re: LerpColor in a Loop Problem
Reply #6 - Nov 2nd, 2009, 12:29pm
 
ah ok, i thought you were showing part of the code as example. i just had to change the size to make it bigger.

THANK YOU SO MUCH!
Re: LerpColor in a Loop Problem
Reply #7 - Nov 2nd, 2009, 12:33pm
 
one more thing.

how would i start the gradient later?
meaning, i want the first 100 pixels on the x coordinate to be black before the gradient starts?

thanks again!
Re: LerpColor in a Loop Problem
Reply #8 - Nov 2nd, 2009, 12:48pm
 
its a bit more complicated. but if i didnt miss anything basic here. this should work...

you can define start and end, thats the point where the gradient starts and ends. before and after that is the from and to color...

Code:
void setup () {
size (1000,600);
}

color from = color ( 0, 0, 0);
color to = color ( 0, 0, 255);

int start = 200;
int end = 500;
void draw () {
background(255);
for ( int i = 0; i < end-start; i++) {
color interA = lerpColor(from, to,(float)i/(end-start));
stroke(interA);
line(i+start,0,i+start,height);
}

//filling the gaps
noStroke();
fill(from);
rect(0,0,start,height);
fill(to);
rect(end,0,width,height);
}
Re: LerpColor in a Loop Problem
Reply #9 - Nov 4th, 2009, 2:59pm
 
2 additional alternatives for you:

If you're using P3D or OPENGL renderers you can also just draw a quad and set each vertex color accordingly, like:

Code:
size(400,400,P3D);
beginShape(QUAD);
fill(0);
vertex(0,0);
fill(0,0,255);
vertex(width,0);
vertex(width,height);
fill(0);
vertex(0,height);
endShape();


If you want to draw multicolor gradients, you could also give toxiclibs a go (for this next example to work you need to download toxiclibscore & colorutils, both from here).

Code:
import toxi.geom.*;
import toxi.color.*;

void setup() {
 size(1000,100);
}

void draw() {
 ColorGradient grad=new ColorGradient();
 grad.addColorAt(0,TColor.BLACK);
 grad.addColorAt(width,TColor.BLUE);
 grad.addColorAt(mouseX,TColor.RED);
 grad.addColorAt(width-mouseX,TColor.YELLOW);
 ColorList l=grad.calcGradient(0,width);
 int x=0;
 for(Iterator i=l.iterator(); i.hasNext();) {
   TColor c=(TColor)i.next();
   stroke(c.toARGB());
   line(x,0,x,height);
   x++;
 }
}
Page Index Toggle Pages: 1