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 › Gradients as fills
Page Index Toggle Pages: 1
Gradients as fills? (Read 1508 times)
Gradients as fills?
Dec 22nd, 2006, 7:14pm
 
Very new to Processing, (and programming, actually!), and I was wondering if there was a way to use gradients as fills?  Or even if not that, just a general transition of color?  I am looking to make organic shapes using bezier curves, which I will then fill.  (Much like the color transitions in the curves of the Spanish movie trailer under "Exhibition.")

Thank you for helping a newbie!

~Terri
Re: Gradients as fills?
Reply #1 - Dec 22nd, 2006, 9:35pm
 
Hi, Terri: have you downloaded the Exibition source (motionLines_Class.pde) The gradient effect is done line by line. That's why I think about "fill pixel by pixel"... But don't pay me attention. Better take a glance to Search processing.org and Discourse(specifying "gradient").

One Saludo.
Nach.
Re: Gradients as fills?
Reply #2 - Dec 22nd, 2006, 9:56pm
 
you could try something like this: gradientFill.

F
Re: Gradients as fills?
Reply #3 - Dec 22nd, 2006, 10:14pm
 
nah, just copy the code over into a new sketch and press run. then alter as you like.

PImage (Processings builtin class for images) reference is here.

some other inspiration might be here:
http://builtwithprocessing.org/browse.php?s=gradient

F
Re: Gradients as fills?
Reply #4 - Dec 22nd, 2006, 10:58pm
 
Shocked Call me fool, fjen, but I thought that the builtwithprocessing's search only worked with domain names. Thanks for the engine!
Re: Gradients as fills?
Reply #5 - Dec 22nd, 2006, 11:41pm
 
actually it (currently) only works with sketch-names .. Smiley

F
Re: Gradients as fills?
Reply #6 - Dec 23rd, 2006, 1:23am
 
Here is the code that I have so far for my composition:

Code:
//Global Document Settings
colorMode (RGB, 255);
size (600, 400, P3D);
background(255);
gradient = new Gradient ( 100, 100, 00be12, 00870d );
gradient.horizontal();

beginShape();
texture(gradient);
line(600, 0, 0, 400);
line(575, 0, 0, 380);
endShape();


When I go to run it, it says "expecting RPAREN, found 'be12'"  I didn't follow the example to the que, so perhaps that is the problem!  I have been reading up on "void," but do not understand it, so was weary to use it.
Re: Gradients as fills?
Reply #7 - Dec 23rd, 2006, 6:01am
 
nah, you just got the colors wrong:

it's either:
gradient = new Gradient ( 100, 100, #00be12, #00870d );
or
gradient = new Gradient ( 100, 100, 0xFF00be12, 0xFF00870d );

F
Re: Gradients as fills?
Reply #8 - Dec 23rd, 2006, 6:19am
 
Ahhh!  Didn't know the # sign was important in programming, too!  (I am a web designer by trade.)

However, I still have not been able to test it, as now a new error is coming up: "No accessible field named 'gradient' was found in type 'Temporary_1146_7930'."

Thanks again for all of the help!

~Terri
Re: Gradients as fills?
Reply #9 - Dec 23rd, 2006, 4:25pm
 
ok. in simple mode (without setup(), draw()) you need to specify the class before you use it. so the class definition for Gradient goes before your code.

then you need to tell processing what kind of variable you are trying to initialize when you do:
gradient = new Gradient( ... )
it should be:
Gradient gradient = new Gradient( ... );
( note that Processing is case-sensitive, so the lowercase "gradient" is just a name. don't confuse with the class Gradient itself. )

another thing: texture only works with beginShape(), vertex() and
endShape(). line does not work in there.

here's the full working example:

Code:

class Gradient
extends PImage
{
int color1, color2;

Gradient ( int _w, int _h )
{
super(_w, _h);

color1 = 0xFF000000;
color2 = 0xFFFFFFFF;
}

Gradient ( int _w, int _h, int _c1, int _c2 )
{
super(_w, _h);

color1 = _c1;
color2 = _c2;
}

void vertical ()
{
float as = (((color2 >> 24) & 0xFF) - ((color1 >> 24) & 0xFF)) / this.height;
float rs = (((color2 >> 16) & 0xFF) - ((color1 >> 16) & 0xFF)) / this.height;
float gs = (((color2 >> 8) & 0xFF) - ((color1 >> 8) & 0xFF)) / this.height;
float bs = (((color2 >> 0) & 0xFF) - ((color1 >> 0) & 0xFF)) / this.height;

for ( int ih=0; ih < this.height; ih++ )
{
int c = color( ((color1 >> 16) & 0xFF) + round(rs * ih),
((color1 >> 8) & 0xFF) + round(gs * ih),
((color1 >> 0) & 0xFF) + round(bs * ih),
((color1 >> 24) & 0xFF) + round(as * ih)
);
for ( int iw=0; iw < this.width; iw++ )
{
this.pixels[iw+(ih*this.width)] = c;
}
}
}


void horizontal ()
{
float as = (((color2 >> 24) & 0xFF) - ((color1 >> 24) & 0xFF)) / this.width;
float rs = (((color2 >> 16) & 0xFF) - ((color1 >> 16) & 0xFF)) / this.width;
float gs = (((color2 >> 8) & 0xFF) - ((color1 >> 8) & 0xFF)) / this.width;
float bs = (((color2 >> 0) & 0xFF) - ((color1 >> 0) & 0xFF)) / this.width;

for ( int iw=0; iw < this.width; iw++ )
{
int c = color( ((color1 >> 16) & 0xFF) + round(rs * iw),
((color1 >> 8) & 0xFF) + round(gs * iw),
((color1 >> 0) & 0xFF) + round(bs * iw),
((color1 >> 24) & 0xFF) + round(as * iw)
);
for ( int ih=0; ih < this.height; ih++ )
{
this.pixels[iw+(ih*this.width)] = c;
}
}
}
}

size (600, 400, P3D);
background(255);
Gradient gradient = new Gradient ( 100, 100, #00be12, #00070d );
gradient.horizontal();

noStroke();
beginShape();
texture(gradient);
vertex(575, 0, 0, 0);
vertex(600, 0, 0, 100);
vertex(0, 400, 100, 0);
vertex(0, 380, 100, 100);
endShape();


best,
F
Re: Gradients as fills?
Reply #10 - Dec 23rd, 2006, 6:40pm
 
Thanks SO much!  As well, would I be able to look up some of the code for the class in the "Reference" section here...right now, it all looks like Greek!  (I am actually doing this for a university course, so if I use something, I need to be able to understand it, and would prefer to write it, myself.)
Re: Gradients as fills?
Reply #11 - Dec 23rd, 2006, 8:49pm
 
hi terri,

sure, i see. i don't think that it's that important for you as a beginner to fully understand how to write classes, rather how to use them. so i suggest you concentrate on the part below the class definition (from size(..) on). explanations of the syntax used there is here in the reference.

F
Re: Gradients as fills?
Reply #12 - Dec 23rd, 2006, 8:57pm
 
... but if you are super curious about the class thing, have a look here and there for an intro.

F
Page Index Toggle Pages: 1