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 › how give random colors only at start of animation
Page Index Toggle Pages: 1
how give random colors only at start of animation (Read 2606 times)
how give random colors only at start of animation
Oct 5th, 2009, 1:31pm
 
i want to make a perlin animation, where each wave-line has a unique color and moves slowly upwards, only that the color should not change again on each new frame as it does here, but raise upwards with the animation...
Code:
import processing.video.*;
MovieMaker mm;
float e,x,t,d;
PImage a;
void setup(){
size(1280,720);
background(255);
smooth();
a = loadImage("15.jpg");
frameRate(25);
mm = new MovieMaker(this, width, height, "001.avi",25, MovieMaker.SORENSON, MovieMaker.BEST);
}
void draw(){
for(e=1;e<900;e/=.998){
int xx = int(random(a.width));
int yy = int(random(a.height));
color pix = a.get(xx, yy);
stroke(pix,e/4);
strokeWeight(0.01*e);
beginShape();
noFill();
for(;x<8;x+=.1,curveVertex(x*400,e+e*noise(x,t+e/60)/8));
endShape();
t+=x=1E-8;
}
mm.addFrame();
}
void keyPressed() {
if (key == ' ') {
mm.finish();
exit();
}
}


Re: how give random colors only at start of animation
Reply #1 - Oct 5th, 2009, 2:30pm
 
Then you'll need to define the colours somewhere other than draw: perhaps put them in an array?  The slight difficulty you'll have is since your loop uses e/=.998 at each step it's going to be a little bit more difficult to reference the index of the array.  Probably simplest to create a counter variable outside the loop and use that as an index:

Code:
void draw(){
int counter = 0;
for(e=1;e<900;e/=.998){
int xx = int(random(a.width));
int yy = int(random(a.height));
color pix = myColourArray(counter);
counter ++;
stroke(pix,e/4);
strokeWeight(0.01*e);
beginShape();
noFill();
for(;x<8;x+=.1,curveVertex(x*400,e+e*noise(x,t+e/60)/8));
endShape();
t+=x=1E-8;
}
mm.addFrame();

}


You'd obviously need to iterate through the loop once in setup to setup the array with the colour values; but obviously declare the array as global...
Re: how give random colors only at start of animation
Reply #2 - Oct 6th, 2009, 1:07am
 
thank you!Smiley ...more complicated than i thought. i have a vage idea of what you mean, but don't get it all together...
Re: how give random colors only at start of animation
Reply #3 - Oct 6th, 2009, 2:51am
 
Looking at it again the fact that you're not iterating using e++ does make things more tricky, but still manageable.  First declare a global (i.e. before setup) array of colours with length 0...

Then run your ' for(e=1;e<900;e/=.998)' loop in setup and append the generated colours to the array.

Then you can just access the colour from the array in draw() as I first suggested.  Also with this approach you don't need to declare xx or yy in the draw loop as you don't use them anywhere else there.

I can post code if you get stuck, but it will probably sink in better if you work on it yourself Wink
Re: how give random colors only at start of animation
Reply #4 - Oct 6th, 2009, 5:12am
 
thank you! ..ok, the pronciple;
- global color array
- in setup (because want it only once) generating the random colors from map and storing them into array
doing this by for structure to have each line with different color
- calling them up within the draw() function
(e++ does a good dense line structure too, even when its not that easy for fine tuning)

beginners code not working:

Code:
import processing.video.*;
MovieMaker mm;
float e,x,t,d;
PImage a;

// declare global cols array
int paint;
color[] cols = new color[paint];

void setup(){
size(1280,720);
background(255);
smooth();
a = loadImage("15.jpg");
frameRate(25);
mm = new MovieMaker(this, width, height, "001.avi",25, MovieMaker.SORENSON, MovieMaker.BEST);

//for(e=1;e<900;e/=.998){
for(e=1;e<900;e++){
int xx = int(random(a.width));
int yy = int(random(a.height));
// storing colors into array ????
color cols = a.get(xx, yy);

}

}

void draw(){
beginShape();
strokeWeight(0.01*e);
//getting colors out of array ???
stroke(paint,e/4);
for(;x<8;x+=.1,curveVertex(x*400,e+e*noise(x,t+e/60)/8));
endShape();
t+=x=1E-8;

mm.addFrame();
}

void keyPressed() {
if (key == ' ') {
mm.finish();
exit();
}
}
Re: how give random colors only at start of animation
Reply #5 - Oct 6th, 2009, 8:33am
 
Well - you had quite a few problems there (see comments).  I've got the code to a point where it executes; though I'm not convinced it's working quite the way you want:

Code:
//import processing.video.*;
//MovieMaker mm;
float e,x,t,d;
PImage a;

// declare global cols array
// If you're going to use this variable give it a more meaningful name and then refer to it as necessary in the code
int numCols = 900;
// If you're incrementing your loop by using i++ you know exactly how many colours you need so can declare that here:
color[] cols = new color[numCols];

void setup(){
 size(1280,720);
 background(255);
 smooth();
 a = loadImage("15.jpg");
 
//  mm = new MovieMaker(this, width, height, "001.avi",25, MovieMaker.SORENSON, MovieMaker.BEST);
 
// Note the use of numCols as the limit
// Also note that I'm not using your global variable e in the loop.  It's rather odd to use a global
       // variable in a loop the way you were, and things broke when the array reference used e, since
      // you reference an array with an int NOT a float...
for(int i=1 ; i<numCols; i++){
int xx = int(random(a.width));
int yy = int(random(a.height));
// storing colors into array ????
// First you've already declared the type used for cols so you don't need to
// declare it again, otherwise the interpreter will think you're
// declaring a new local variable
// Second you need to tell it where in the array you're storing the value.  In this case
// since you're now using e++ you can use e as the index in the array
               color newColor = a.get(xx, yy);
cols[i] = newColor;
// Note an advantage to using e++ here is that you avoid the use of the append method I suggested
}

frameRate(25);
   
}

void draw(){
   //getting colors out of array ???
// Well - you'll need to iterate through the whole array, which means you need to repeat the loop
// used to populate it:
for(int i=1; i<numCols; i++){
               // If you're referencing e since it increments in each iteration it also needs to be in the loop.
               strokeWeight(0.01*e);
    // BeginShape needs to be in the loop since each iteration creates a new shape
beginShape();
// for clarity I'm going to refer to the array in a new variable
color colorFromArray = cols[i];
// (though in theory you could have referenced it directly in stroke)
stroke(colorFromArray,e/4);
// I find your original code rather ugly: quite difficult to read.  Something you borrowed from elsewhere perhaps?
// I'd recommend sticking to standard syntax until you're comfortable using these sorts of shortcut.
for(float x=0;x<8;x+=.1){
                   curveVertex(x*400,e+e*noise(x,t+e/60)/8);
               }
endShape();
t+=x=1E-8;
               // Since e is no longer the iterator in your for loop you need to iterate it too...
               e++;
}

// mm.addFrame();
}

void keyPressed() {
 if (key == ' ') {
//    mm.finish();
   exit();
 }
}
Re: how give random colors only at start of animation
Reply #6 - Oct 6th, 2009, 12:29pm
 
oh man, thank you so much! and for all that explanations.
the original code is from here:http://www.openprocessing.org/visuals/?visualID=4326 i just put the colors to it.
by seting the value t+=x=1E-5; to -8, i made it pretty slow, only good visible in 25 frame video...
now it looks like it doesn't move anymore... have to check it with more time and use your color function in a different project for better understanding.


Re: how give random colors only at start of animation
Reply #7 - Oct 6th, 2009, 3:12pm
 
I must admit I hadn't really looked at the algorithm doing the drawing properly:

Code:
for(x=0;x<8;x+=.1) {
   curveVertex(x*400,e+e*noise(x,t+e/60)/8);
}


A couple of points worth making:

1.  curveVertex takes two arguments: x and y.  There's no point drawing anything if the value of x (or y for that matter) results in something drawn far off screen.  Since in the containing for loop x increases until it's equal to 8 you're drawing vertices up till x = 8*400 = 3200.  That's 2.5 times larger than the width of the screen.  i.e. you're calculating a lot of vertices that never appear on screen... which is going to slow things down needlessly.

Assuming the goal of that was to reduce the height variation of the lines you should have focused your attention on the second variable: simply increasing the amount you divide by at the end is enough to reduce height variation.

There's also another issue with the y parameter: the for loop that generates the lines shouldn't go much higher than the height of the window, or again it's drawing lines that won't appear: since the window is only 720 pixels high it doesn't make sense to go up to 900...

TBH the original code you copied had a lot of 'magic numbers' in it - i.e. numbers that worked fine at the set dimensions but meant the code would need some tweaking if you changed other factors, such as the size...
Re: how give random colors only at start of animation
Reply #8 - Oct 6th, 2009, 11:34pm
 
thanks! ok, i noticed the size thing, ..did large print output versions before. anyway with the new string the motion doesn't appear...?
Re: how give random colors only at start of animation
Reply #9 - Oct 7th, 2009, 2:49pm
 
Ah - my bad: Because I'd taken e out of the for loop it wasn't getting reset to zero each time the draw loop ran.  Just add e=0; before the for loop in draw()...

It's certainly an interesting visual effect.
Re: how give random colors only at start of animation
Reply #10 - Oct 7th, 2009, 2:56pm
 
Actually having said that, if I leave t+=x=1E-8; after a few frames it stops changing.  Setting it to 1E-5 seems to result in regular changes though and also makes it run faster.  I must admit I know nothing about perlin noise and don't really understand how changing the coordinates changes the values returned...
Re: how give random colors only at start of animation
Reply #11 - Oct 7th, 2009, 3:24pm
 
Just for the hell of it here's a version I threw together based on the original source you linked.  Fewer magic numbers in there: you can change size() and it should only calculate enough to fill the screen; though I find you then need to tweak some of the other values to get a nice effect.  It works well when you feed it a source image, but also works pretty well with totally random colours...

Code:
float e,x,t;

//PImage colourSource;

float iterator = 0.992;
color[] colours = new color[0];


void setup(){
size(600,300);
background(0);
noFill();
//colourSource = loadImage("elephant.jpg");

for(e=1;e<height;e/=iterator){
int r = (int) random(0,255);
int g = (int) random(0,255);
int b = (int) random(0,255);
color newColour = color(r,g,b);
//int x = (int) random(colourSource.width);
//int y = (int) random(colourSource.height);
//color newColour = colourSource.get(x,y);
colours = append(colours, newColour);
}

}

void draw(){
background(0);
smooth();
int counter = 0;
int xLimit = 6;
float xMultiplier = width/xLimit;

for(e=1;e<height;e/=iterator){
int opacity = int(e/2);
stroke(colours[counter], opacity);
strokeWeight(2);
beginShape();
for(x=0;x<xLimit;x+=.1){
curveVertex(x*xMultiplier,e+e*noise(x,t+e/60)/2);
}
endShape();
t+=x=1E-4;
counter ++;
}
}
Re: how give random colors only at start of animation
Reply #12 - Oct 8th, 2009, 2:19am
 
Cheesy, you should post it at openprocessing!
Page Index Toggle Pages: 1