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 & HelpPrograms › Using colors in bezier curves (or lines)
Page Index Toggle Pages: 1
Using colors in bezier curves (or lines) (Read 2200 times)
Using colors in bezier curves (or lines)
Nov 9th, 2009, 3:49am
 
Hi!

I'm very new to processing, so bear with me.
I'm thinking about creating a visualization where I draw a path. This I could do with lines and bezier curves using some nice thickness. However, I'd like to use different colors at different points of the path to show some value associated with that point (or time actually). I can do this by splitting the line into different sections and have them in different colors. However, that seems clumsy and that way I would not get nice gradients between different colors. Any ideas on how to create such effect? (I hope I did make myself clear...)
Re: Using colors in bezier curves (or lines)
Reply #1 - Nov 9th, 2009, 4:33am
 
There is no other way then splitting it in different lines, thats pretty easy when you just have a straight line. If you have some more complex bezier curve i believe it is not that easy anymore.
If this is not an interactive tool but a print application. Maybe just create a single Beziercurve and color them later in Illustrator after exporting the graph as PDF
Re: Using colors in bezier curves (or lines)
Reply #2 - Nov 9th, 2009, 5:07am
 
Hi!

Quick sketch of what I'm trying:
Code:

void setup() {
 size(300, 300);
 stroke(255);
 background(255, 255, 255);
}

void draw() {
 noFill();
 stroke(0, 0, 0);
 strokeWeight(10);
 line(55, 80, 115, 80);
 stroke(255,0,0);
 bezier(115,80, 190, 80, 110, 20 ,190,20);
 line(190, 20, 220, 20);
 stroke(0, 0, 0);
 line(220, 20, 280, 20);
 noLoop();
}


But obviously nice gradient from black to red and back. How is this done: benfry.com/revisionist/detail.jpg (sorry, I con't post direct links yet...)? And could that technique be used here also?

Thanks.

Re: Using colors in bezier curves (or lines)
Reply #3 - Nov 9th, 2009, 5:29am
 
Oh, BTW, it's not that I need *one line* that could be drawn in some Illustrator or so. Quite a few lines based on data... Wink
Re: Using colors in bezier curves (or lines)
Reply #4 - Nov 9th, 2009, 6:06am
 
thats easy as the gradient is only on the straight end lines...

Code:
void setup() {
size(300, 300);
stroke(255);
background(0);
}

void draw() {
noFill();
stroke(0, 0, 0);

strokeWeight(10);
int steps = 100;

for(int i = 0; i < steps; i++){
stroke(lerpColor(#ff0000,#000000,(float)i/steps));
point(115-i,80);
}

stroke(255,0,0);
bezier(115,80, 190, 80, 110, 20 ,190,20);

for(int i = 0; i < steps; i++){
stroke(lerpColor(#ff0000,#000000,(float)i/steps));
point(190+i,20);
}

noLoop();
}
Re: Using colors in bezier curves (or lines)
Reply #5 - Nov 9th, 2009, 6:24am
 
I haven't read any code and am thinking aloud here:  Could the bezier curve somehow be stored in a PImage and that used as a mask over a gradient?
Re: Using colors in bezier curves (or lines)
Reply #6 - Nov 9th, 2009, 6:33am
 
probably but, the gradient wouldnt be aligned along the curve. so at the point where the curve doesnt change on the x axis, their would be no color change.
Re: Using colors in bezier curves (or lines)
Reply #7 - Nov 9th, 2009, 6:38am
 
PhiLho posted a good example of how to fill shapes and text with a texture, this could be also applied to any other drawing and texture as a gradient for example. but like i said, Even if the shape changes its orientation, the texture doesnt

http://processing.org/discourse/yabb2/?num=1229687224
Re: Using colors in bezier curves (or lines)
Reply #8 - Nov 9th, 2009, 1:00pm
 
Hi!

Thanks for the tips! I tried to recreate the gradients from Ben Fry's Revisionist, like this:
Code:

void setup() {
 size(600, 600);
 stroke(255);
 background(255, 255, 255);
}

void draw() {
 int x1 = 10;
 int x2 = width - x1;
 int y1 = height - x1 - 20;
 int y2 = x1;
 
// first one without gradient
 stroke(0, 128, 0);
 fill(0, 128, 0, 128);
 beginShape();
 vertex(x1, y1);
 bezierVertex(x1+250, y1, x2-450, y2, x2, y2);
 vertex(x2, y2+100);
 bezierVertex(x2-450, y2+100, x1+250, y1+5, x1, y1+5);
 vertex(x1, y1);
 endShape();
 
// Second one with gradient
 y1 = y1 + 10;
 y2 = y2 + 120;
 
 PGraphics pgGradient = createGraphics(width, height, P2D);
 PGraphics pgMask = createGraphics(width, height, P2D);
 pgGradient.beginDraw();
 pgGradient.strokeWeight(1);
 color cFrom = color(70, 150, 76);
 color cTo = color(255, 255, 255);
 for (int i = 0; i < pgGradient.width; i++){
   pgGradient.stroke(lerpColor(cFrom, cTo, (float)i/pgGradient.width));
   pgGradient.line(i, 0, i, pgGradient.height);
 }
 pgGradient.endDraw();
 
 pgMask.background(0);
 pgMask.fill(255);
 pgMask.stroke(255);
 //pgMask.noStroke();
 pgMask.beginDraw();
 pgMask.beginShape();
 pgMask.vertex(x1, y1);
 pgMask.bezierVertex(x1+250, y1, x2-450, y2, x2, y2);
 pgMask.vertex(x2, y2+100);
 pgMask.bezierVertex(x2-450, y2+100, x1+250, y1+5, x1, y1+5);
 pgMask.vertex(x1, y1);
 pgMask.endShape();
 pgMask.endDraw();
 
 pgGradient.mask(pgMask.get(0,0,pgMask.width, pgMask.height));
 image(pgGradient, 0, 0);
 
 noLoop();
}


Is this how you think he did it?

Thanks!
Re: Using colors in bezier curves (or lines)
Reply #9 - Nov 9th, 2009, 1:16pm
 
Did you see my previous posting with the gradient code?
i believe thats how he has done it. but what you came up with looks really code. that was the other idea Smiley
Re: Using colors in bezier curves (or lines)
Reply #10 - Nov 9th, 2009, 10:18pm
 
Cedric wrote on Nov 9th, 2009, 1:16pm:
Did you see my previous posting with the gradient code
i believe thats how he has done it. but what you came up with looks really code. that was the other idea Smiley


I did, thanks! - otherwise I'd probably never have found about the function called lerpColor Smiley

I noticed some problems with my code above. The borders didn't look that nice, so closer look revealed some problems. The first shape has stroke and fill - but the fill goes sometimes outside the stroke. I'd say a small bug. Also, the second shape with the gradient, the large vertical edge on right is not straight if the color is darker. Not sure why.

However, larger problem is that I tried to export it to PDF by simply importing pdf and setting size(600, 600, PDF, "BezierTest1.pdf"); However, I don't get the same picture. I only get the gradient filling the whole screen (i.e. pgGradient without the mask). What's wrong How can I do this same with PDF

Thanks!
Re: Using colors in bezier curves (or lines)
Reply #11 - Nov 10th, 2009, 2:43am
 
Hmm good question. I dont know why, but i could bet that PDF export doesnt support masking.
Page Index Toggle Pages: 1