Color Range Picker.
Take a 50x50px area and calculates the range of these pixels.
/**
* Copyright jeffibacache ( http://wonderfl.net/user/jeffibacache )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/vqdd
*/
package
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.PixelSnapping;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.system.LoaderContext;
[SWF(width="465", height="465", frameRate="60")]
public class Main extends Sprite
{
private var _loader:Loader;
private var _bitmap:Bitmap;
private var _sprite:Sprite;
private var _cursor:Sprite;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, setupUI);
}
private function setupUI(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, setupUI);
setup();
}
private function setup():void
{
dataSetup();
viewSetup();
}
private function dataSetup():void
{
_bitmap = new Bitmap(null, PixelSnapping.ALWAYS, true);
_sprite = new Sprite();
_cursor = new Sprite();
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
loadImage();
}
private function loadImage():void
{
_loader.load(new URLRequest("http://jeffibacache.com/super_mario_bros_3.cover.front.jpg"), new LoaderContext(true));
}
private function loaderCompleteHandler(e:Event):void
{
_bitmap.bitmapData = Bitmap(_loader.content).bitmapData;
_cursor.startDrag(true, new Rectangle(0, 0, _bitmap.width - 50, _bitmap.height - 50));
_sprite.x = _bitmap.width;
resolveImage();
}
private function resolveImage():void
{
changeColors();
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
changeColors();
}
private function changeColors():void
{
var vector:Vector.<uint> = _bitmap.bitmapData.getVector(new Rectangle(_cursor.x, _cursor.y, _cursor.width, _cursor.height));
var i:int = 0;
var length:int = vector.length;
var red:uint;
var green:uint;
var blue:uint;
for(i; i < length; ++i)
{
red += vector[i] >> 16 & 0xFF;
green += vector[i] >> 8 & 0xFF;
blue += vector[i] & 0xFF;
}
_sprite.graphics.clear();
_sprite.graphics.beginFill(uint("0x" + resolveHex((red / vector.length).toString(16)) + resolveHex((green / vector.length).toString(16)) + resolveHex((blue / vector.length).toString(16))));
_sprite.graphics.drawRect(0, 0, 50, 50);
_sprite.graphics.endFill();
}
private function viewSetup():void
{
addChild(_bitmap);
addChild(_sprite);
addChild(_cursor);
generateCursor();
_sprite.x = _bitmap.width;
}
private function resolveHex(hex:String):String
{
var string:String;
if(hex.length < 2)
{
string = "0" + hex;
}
else
{
string = hex;
}
return string;
}
private function generateCursor():void
{
_cursor.graphics.beginFill(0xFF0000, .2);
_cursor.graphics.drawRect(0, 0, 50, 50);
_cursor.graphics.endFill();
}
}
}