Average color
[ 2009-03-21 14:58:45 | Author: liuhuan ]
A way to caculate average color for a image. I wasted a lot of time trying to open local image file into flash. This new function is only supported by flash player 10, using Bytearray which is convinient and powerful.
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import fl.controls.Button;
var square:Sprite = new Sprite();
square.graphics.beginFill(0x000000);
square.graphics.drawRect(0,0,100,100);
square.graphics.endFill();
square.x=390;
square.y=70;
square.width=100;
square.height=100;
addChild(square);
var fileReference:FileReference = new FileReference();
var fileFilter:FileFilter=new FileFilter("Images","*.jpg");
fileReference.addEventListener(Event.SELECT, onSelectFile);
fileReference.addEventListener(Event.COMPLETE,onFileComplete);
var photoHolder:Sprite = new Sprite();
addChild(photoHolder);
photoHolder.x=10;
photoHolder.y=10;
bBrowse.addEventListener(MouseEvent.MOUSE_DOWN, browseFile);
function onFileComplete(event:Event):void {
var bytes:ByteArray=fileReference.data;
var loader:Loader = new Loader();
loader.loadBytes(bytes);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
photoHolder.addChild(loader);
}
function browseFile(e:MouseEvent) {
fileReference.browse([fileFilter]);
}
function onSelectFile(event:Event):void {
for (var i:Number=0; i< photoHolder.numChildren; i++) {
photoHolder.removeChildAt(0);
}
photoHolder.scaleX=1;
photoHolder.scaleY=1;
fileReference.load();
}
function completeHandler(e:Event) {
if (photoHolder.width>360) {
photoHolder.height=360*photoHolder.height/photoHolder.width;
photoHolder.width=360;
}
var bitmap:BitmapData=new BitmapData(photoHolder.width,photoHolder.height,false);
bitmap.draw(photoHolder);
setColor(averageColor(bitmap));
}
function averageColor(bitmap:BitmapData):Number {
var sumColorR:Number=0;
var sumColorG:Number=0;
var sumColorB:Number=0;
var count:Number=0;
for (var h:Number = 0; h < bitmap.height; h++) {
for (var w:Number = 0; w < bitmap.width; w++) {
var col:Number=bitmap.getPixel(w,h);
sumColorR+=col>>16&0xFF;
sumColorG+=col>>8&0xFF;
sumColorB+=col&0xFF;
count++;
}
}
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;
return color;
}
function setColor(color:Number) {
var newColorTransform:ColorTransform = new ColorTransform();
newColorTransform.color=color;
square.transform.colorTransform=newColorTransform;
}
import flash.display.BitmapData;
import flash.geom.Rectangle;
import fl.controls.Button;
var square:Sprite = new Sprite();
square.graphics.beginFill(0x000000);
square.graphics.drawRect(0,0,100,100);
square.graphics.endFill();
square.x=390;
square.y=70;
square.width=100;
square.height=100;
addChild(square);
var fileReference:FileReference = new FileReference();
var fileFilter:FileFilter=new FileFilter("Images","*.jpg");
fileReference.addEventListener(Event.SELECT, onSelectFile);
fileReference.addEventListener(Event.COMPLETE,onFileComplete);
var photoHolder:Sprite = new Sprite();
addChild(photoHolder);
photoHolder.x=10;
photoHolder.y=10;
bBrowse.addEventListener(MouseEvent.MOUSE_DOWN, browseFile);
function onFileComplete(event:Event):void {
var bytes:ByteArray=fileReference.data;
var loader:Loader = new Loader();
loader.loadBytes(bytes);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
photoHolder.addChild(loader);
}
function browseFile(e:MouseEvent) {
fileReference.browse([fileFilter]);
}
function onSelectFile(event:Event):void {
for (var i:Number=0; i< photoHolder.numChildren; i++) {
photoHolder.removeChildAt(0);
}
photoHolder.scaleX=1;
photoHolder.scaleY=1;
fileReference.load();
}
function completeHandler(e:Event) {
if (photoHolder.width>360) {
photoHolder.height=360*photoHolder.height/photoHolder.width;
photoHolder.width=360;
}
var bitmap:BitmapData=new BitmapData(photoHolder.width,photoHolder.height,false);
bitmap.draw(photoHolder);
setColor(averageColor(bitmap));
}
function averageColor(bitmap:BitmapData):Number {
var sumColorR:Number=0;
var sumColorG:Number=0;
var sumColorB:Number=0;
var count:Number=0;
for (var h:Number = 0; h < bitmap.height; h++) {
for (var w:Number = 0; w < bitmap.width; w++) {
var col:Number=bitmap.getPixel(w,h);
sumColorR+=col>>16&0xFF;
sumColorG+=col>>8&0xFF;
sumColorB+=col&0xFF;
count++;
}
}
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;
return color;
}
function setColor(color:Number) {
var newColorTransform:ColorTransform = new ColorTransform();
newColorTransform.color=color;
square.transform.colorTransform=newColorTransform;
}
Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=964
There is no comment on this article.
You can't post comment on this article.