Monday, March 19, 2012

Flex SDK (AS3) -- Gradient to a noise

to create a perlin noise,
http://noypi-linux.blogspot.com/2012/03/flex-sdk-perlin-noise-as3.html

from that BitmapData use the code below:
(the code below is a port from http://devmag.org.za/2009/04/25/perlin-noise/)

----------- begin code -------------

public function MapGradient(gradientStart:uint, gradientEnd:uint, perlinNoise:BitmapData) : BitmapData
{
   var width:int = perlinNoise.width;
   var height:int = perlinNoise.height;
 
   var image:BitmapData = new BitmapData(width, height);
 
   for (var i:int = 0; i < width; ++i)
   {
   for (var j:int = 0; j < height; ++j)
   {
    var c:uint = GetColor(gradientStart, gradientEnd, perlinNoise.getPixel32(i, j));
    image.setPixel(i, j, c);
   }
   }
 
   return image;
}

public function GetColor(gradientStart:uint, gradientEnd:uint, t:Number) : uint
{
 var u:Number = 1 - t;
 
 var alpha:uint = 255;
 var red:uint = (((gradientStart & 0x00ff0000)>>>16) * u) 
       + (((gradientEnd & 0x00ff0000)>>>16) * t);
 var green:uint = (((gradientStart & 0x0000ff00)>>>8) * u) 
      + (((gradientEnd & 0x0000ff00)>>>8) * t);
 var blue:uint = ((gradientStart & 0x000000ff) * u) 
     + ((gradientEnd & 0x000000ff) * t);
       
 var color:uint = (alpha << 24) | 
      (red << 16) | 
      (green << 8) |
      (blue);
    
 return color;
}