forked from: Worm Study04
Study for color generation.
// forked from fladdict's Worm Study04
// forked from fladdict's Worm Study03
// forked from fladdict's Worm Study02
// forked from fladdict's Worm Study01
// forked from fladdict's code on 2008-12-17
// write as3 code here..
// Study for color generation.
package{
import flash.display.*
import flash.events.*
import flash.geom.*
public class Sketch02 extends Sprite
{
public var bm:Bitmap;
public var bmd:BitmapData;
public var particles:Array;
public var numParticles:int = 50;
public function Sketch02():void
{
bmd = new BitmapData(stage.stageWidth,stage.stageHeight, false, 0x000000);
bm = new Bitmap(bmd);
addChild(bm);
particles = [];
for(var i:int=0; i<numParticles; i++)
{
var prt:Object = createParticle();
particles.push(prt);
}
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function enterFrameHandler(e:Event):void
{
var n:int = particles.length;
for(var i:int=0; i<n; i++)
{
var prt:Object = particles[i];
prt.dv += Math.random()*0.03-0.015;
prt.direction += prt.dv;
prt.x += Math.cos(prt.direction) * prt.v;
prt.y += Math.sin(prt.direction) * prt.v;
if(prt.x < 0 ){
prt.x = stage.stageWidth;
}else if(prt.x > stage.stageWidth){
prt.x = 0;
}
if(prt.y < 0 ){
prt.y = stage.stageHeight;
}else if(prt.y > stage.stageHeight){
prt.y = 0;
}
//
//Add randomness to color
//
prt.rad += prt.radV;
var r:Number = prt.red * ((1+Math.cos(prt.rad))*2);
var g:Number = prt.green * ((1+Math.cos(prt.rad))*2);
var b:Number = prt.blue * ((1+Math.cos(prt.rad))*2);
r = Math.round(Math.min(255, Math.max(0,r)));
g = Math.round(Math.min(255, Math.max(0,g)));
b = Math.round(Math.min(255, Math.max(0,b)));
var col:Number = r<<16 | g<<8 | b;
bmd.setPixel(prt.x, prt.y, col);
}
}
protected function createParticle():Object
{
var obj:Object = {};
//moving setting
obj.x = stage.stageWidth * 0.5;
obj.y = stage.stageHeight * 0.5;
obj.v = 0.4 + Math.random()*0.5;
obj.direction = Math.random()*Math.PI*2;
//color setting
obj.red = Math.random()*255;
obj.green = Math.random()*obj.red;
obj.blue = Math.random()*obj.red;
//oscilator setting
obj.rad = Math.random()*Math.PI*5;
obj.radV = 1;
obj.color = getColor();
obj.dv = (Math.random()-0.5) * 0.09;
return obj;
}
protected function getColor():int
{
var r:int = Math.random()*255;
var g:int = Math.random()*r*0.5 + r*0.5;
var b:int = Math.random()*g*0.5 + r*0.5;
return r<<8 | g<<8 | b;
}
protected function normRand():Number
{
return (Math.random()+Math.random()+Math.random()+Math.random()+Math.random())/5 - 0.5;
}
}
}