Color Average
どこぞかのサイト様を参考(というか丸写し)させていただききました。
/**
* Copyright deform ( http://wonderfl.net/user/deform )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ip6h
*/
// どこぞかのサイト様を参考(というか丸写し)させていただききました。
package {
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.text.TextField;
import flash.system.LoaderContext;
public class FlashTest extends Sprite {
private var loader:Loader;
private var threshold:uint;
public function FlashTest() {
// write as3 code here..
loader = new Loader();
var context:LoaderContext = new LoaderContext(true);
loader.load(new URLRequest("http://lh4.ggpht.com/_eRXo-YR2fNw/S3a1YUcgkkI/AAAAAAAAAI0/uMIwMeyd-L8/s640/002.JPG"), context);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onLoaded);
}
private function _onLoaded(e:Event):void
{
addChild(loader);
e.target.removeEventListener(Event.COMPLETE, _onLoaded);
colorAverage(loader);
var tf:TextField = new TextField();
var average:Number = colorAverage(loader);
var averageHex:uint = Number("0x"+average.toString(16));
if(averageHex < 0x888888) tf.text = "DARKEN : ";
else tf.text = "LIGHTEN : ";
var spr:Sprite = new Sprite();
spr.graphics.beginFill(averageHex);
spr.graphics.drawRect(0,30,stage.stageWidth,30);
spr.graphics.endFill();
tf.appendText(averageHex.toString());
addChild(tf);
}
public function colorAverage(target:DisplayObject):uint {
var bmd:BitmapData = new BitmapData(1, 1, false);
var mat:Matrix = new Matrix();
mat.scale(1/(target.width/target.scaleX), 1/(target.height/target.scaleY));
bmd.draw(target, mat);
var aveColor:uint = bmd.getPixel(0,0);
var spr:Sprite = new Sprite();
spr.graphics.beginFill(aveColor);
spr.graphics.drawRect(0,0,stage.stageWidth,30);
spr.graphics.endFill();
addChild(spr);
return aveColor;
}
}
}