IMG_0069

IMG_0072

I wrote an small Flash tool that might be of use to you. In flash I placed a MovieClip with an jpeg image on the stage and named it “image”. Beside that I placed an rectangle MovieClip named “color”. The ActionScript I wrote takes the average color from the image and colors the rectangle with that.

Color Average 1

import flash.display.BitmapData;

var image:MovieClip = this[“image”];
image._x = 100;
image._y = 100;
image._visible = true;

var bitmap:BitmapData = new BitmapData(image._width,image._height,false);
bitmap.draw(image);

var sumColorR:Number = 0;
var sumColorG:Number = 0;
var sumColorB:Number = 0;
var count:Number = 0;

for(var h:Number = 0; h < image._height; h++)
{
for(var w:Number = 0; w < image._width; w++)
{
var col:Number = bitmap.getPixel(w, h);
sumColorR += (col & 0xFF0000) >>> 16;
sumColorG += (col & 0x00FF00) >>> 8;
sumColorB += (col & 0x0000FF);
count++;
}
}

var backgroundcolor = new Color(“_root.color”);

var colorR:Number = Math.round(sumColorR / count) << 16;
var colorG:Number = Math.round(sumColorG / count) << 8;
var colorB:Number =Math.round(sumColorB / count);

var color:Number = colorR | colorG | colorB;

trace(color);

backgroundcolor.setRGB(color);

Color Average 2

I just remembered another frustrating thing in ActionScript 2.0

Dynamicly removing a MovieClip is not as easy as my_movieclip.removeMovieClip() or removeMovieClip(my_movieclip). These methods are used for removing MovieClips but sometimes they work, sometimes they don’t. It turns out that you can only remove MovieClips in a certain depth range. Very frustrating. Here is the code that I use to remove MovieClips.

function removeMC(mc:MovieClip):Void
{
//Flash only allows you to programmatically remove a movie clip instance if its depth is within a specific range.
//Any movie clip with a depth between 0 and 1048575 inclusive can be programmatically removed.
//Any other movie clip remains unaffected by removeMovieClip( ).

var mTemp:MovieClip = _root.getInstanceAtDepth(0);
mc.swapDepths(0);
mc.removeMovieClip();
if(mTemp != undefined) mTemp.swapDepths(0);
}

I hope this might help some people!