In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

subdividing boxes

Get Adobe Flash player
by blamfantastico 18 Mar 2011
    Embed
/**
 * Copyright blamfantastico ( http://wonderfl.net/user/blamfantastico )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9wAh
 */

package {
    import flash.display.Graphics;
    import flash.display.Sprite;
    import com.actionscriptbible.Example;
        
    [SWF(width = "465", height = "465", backgroundColor = "0x505050")]
    public class FlashTest extends Example {
        public function FlashTest() {
            // write as3 code here..
            
            divideBox(graphics, 2, 2, 460, 460, 300); 
            //divideBox(graphics, 100, 100, 200, 200, 300);
            trace(failsafe);
         }
        
         public function divideBox(g:Graphics, x:Number, y:Number, bw:Number, bh:Number, hue:int, itr:int = 0):void {             
            if (failsafe) {
                failsafe--; 
                
                var i:int; 
                var j:int; 
                
                var xcount:int = 2;
                var ycount:int = 2;
                
                var w:Number = bw / xcount;
                var h:Number = bh / ycount;
                
                for (i = 0; i < xcount; i++) {
                    for (j = 0; j < ycount; j++) {
                        hue += Math.random() + itr*2;
                        //g.beginFill(randomColor(hue, 20, 0.1 + 0.07*itr, 0, 0.3, 0.1)); 
                        g.beginFill(blendColors(0x393D44, 0x331824, itr/5)); 
                        g.drawRect(x + i*w, y + j*h, w, h);
                     
                        if (Math.random() > (itr/6) && w > smallest) divideBox(graphics, x + i*w, y + j*h, w, h, hue, itr+1);                   
                    }
                 }
            }
        }
        
        private var failsafe:int = 1000;
        private var smallest:int = 4; 
    }
}
































//////////////////////////////////////////////////////
      /**
      * Interpolate between two colors.
      * @param color1 Starting color.
      * @param color2 Ending color.
      * @param weight Value between 0 and 1.
      */
      function blendColors(color1:uint, color2:uint, w:Number):uint {
        var r:uint = color1 >> 16 & 0xff;
        var g:uint = color1 >> 8 & 0xff;
        var b:uint = color1 & 0xff;
        r = r + ((color2 >> 16 & 0xff) - r) * w;
        g = g + ((color2 >> 8 & 0xff) - g) * w;
        b = b + ((color2 & 0xff) - b) * w;
        return 0xff000000 | (r << 16) | (g << 8) | b;
      }


  /**
  * Get a random color.
  * @param col Base Color to randomly vary from.
  * @param hue.
  * @param saturation.
  * @param lightness.
  */
  function randomColor(baseH:int = 180, rangeH:Number = 360, baseS:Number = 1, rangeS:Number = 0, baseL:Number = 0.5, rangeL:Number = 0):uint {
     var hsl:Object = {hue: baseH, saturation: baseS, lightness: baseL}; 
     var hrand:int = (hsl.hue - rangeH/2 + Math.random()*rangeH)%360;
     var srand:Number = Math.max(0,Math.min(1,hsl.saturation - rangeS/2 + Math.random()*rangeS));
     var lrand:Number = Math.max(0,Math.min(1,hsl.lightness - rangeL/2 + Math.random()*rangeL));
     
     var col:uint = HSLtoRGB(1, hrand, srand, lrand);
     return col
  }
  
 /**
  * Convert HSL to RGB.
  * http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL
  * @param a Alpha.
  * @param h Hue
  * @param s Saturation
  * @param l Lightness
  */
  function HSLtoRGB(a:Number=1, h:Number=0, s:Number=0.5, l:Number=1):uint{
     h = h % 360;
     if (h < 0) h += 360;
     h /= 60;
     s = Math.min(1, Math.max(0, s));
     l = Math.min(1, Math.max(0, l));;
     a = Math.min(1, Math.max(0, a));;
     var c:Number = (1 - Math.abs((2 * l) - 1)) * s;
     var x:Number = c * (1 - Math.abs((h % 2) - 1));
     var m:Number = (l - (c / 2)) * 255;
     var cm:Number = (c * 255) + m;
     var xm:Number = (x * 255) + m;
     var rgb:uint = uint(a * 255) << 24;
     if (h < 1)      rgb |= (cm << 16) | (xm << 8) | m;
     else if (h < 2) rgb |= (xm << 16) | (cm << 8) | m;
     else if (h < 3) rgb |= (m  << 16) | (cm << 8) | xm;
     else if (h < 4) rgb |= (m  << 16) | (xm << 8) | cm;
     else if (h < 5) rgb |= (xm << 16) | (m  << 8) | cm;
     else if (h < 6) rgb |= (cm << 16) | (m  << 8) | xm;
     return rgb;
  }