flash on 2010-5-10
/**
* Copyright uwi ( http://wonderfl.net/user/uwi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cPyq
*/
package {
import flash.display.*;
import flash.text.TextField;
import flash.net.*;
import flash.events.*;
import flash.geom.*;
import flash.system.LoaderContext;
public class Test extends Sprite {
private var _tf : TextField;
private var _panel : Sprite;
private var _w : uint;
private var _h : uint;
public function Test() {
_tf = new TextField();
_tf.width = 465;
_tf.height = 465;
// addChild(_tf);
_panel = new Sprite();
_panel.x = stage.stageWidth / 2;
_panel.y = stage.stageHeight / 2;
addChild(_panel);
var loader : Loader = new Loader();
loader.load(new URLRequest("http://assets.wonderfl.net/images/related_images/f/fb/fbeb/fbeb9c4c068ff1c8c90eef23d777dce851a780ef"), new LoaderContext(true));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
}
private function onLoadComplete(e : Event) : void
{
var info : LoaderInfo = LoaderInfo(e.target);
info.removeEventListener(Event.COMPLETE, onLoadComplete);
_w = info.content.width;
_h = info.content.height;
_panel.x -= _w / 2;
_panel.y -= _h / 2;
_orig = new BitmapData(_w, _h, false, 0x000000);
_orig.draw(info.content);
_canvas = new BitmapData(_w, _h, false, 0x000000);
_panel.addChild(new Bitmap(_canvas));
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e : Event) : void
{
var x : Number = stage.mouseX - _panel.x;
var y : Number = stage.mouseY - _panel.y;
_canvas.lock();
recDraw(0, 0, _w, _h, x, y);
_canvas.unlock();
}
private function recDraw(xs : Number, ys : Number, xe : Number, ye : Number, xm : Number, ym : Number) : void
{
if( xe - xs >= 1 && ye - ys >= 1 &&
xs <= xm && xm < xe && ys <= ym && ym < ye
){
// 領域内にマウスポインタが含まれていればさらに分割
var xc : Number = (xs + xe) / 2;
var yc : Number = (ys + ye) / 2;
recDraw(xs, ys, xc, yc, xm, ym);
recDraw(xc, ys, xe, yc, xm, ym);
recDraw(xs, yc, xc, ye, xm, ym);
recDraw(xc, yc, xe, ye, xm, ym);
}else{
// 平均色を計算
var rect : Rectangle = new Rectangle(xs, ys, xe - xs, ye - ys)
var hist : Vector.<Vector.<Number>> = _orig.histogram(rect);
var ave : uint = 0;
for(var i : uint = 0;i < 3;i++){
var sum : Number = 0;
var ct : uint = 0;
for(var j : uint = 0;j < 256;j++){
sum += j * hist[i][j];
ct += hist[i][j];
}
ave = ave << 8 | uint(sum / ct);
}
_canvas.fillRect(rect, ave);
}
}
private var _canvas : BitmapData;
private var _orig : BitmapData;
private function tr(...o : Array) : void
{
_tf.appendText(o + "\n");
}
}
}