ポスタリゼーション (1)
//////////////////////////////////////////////////////////////////////////////
[AS3.0] BitmapDataUtilsクラスに挑戦! (3)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1101
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zvxT
*/
////////////////////////////////////////////////////////////////////////////////
// [AS3.0] BitmapDataUtilsクラスに挑戦! (3)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1101
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.events.Event;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private static var basePath:String = "http://www.project-nya.jp/images/flash/";
private var loader:PhotoLoader;
private var bitmap:Bitmap;
private var menu:Menu;
private var photoList:Array;
private var photoID:uint = 0;
private var toneMenu:Menu;
private var tonesList:Array;
private var tones:uint = 2;
public function Main() {
//Wonderfl.capture_delay(1);
init();
}
private function init():void {
loader = new PhotoLoader();
loader.x = 152;
loader.y = 30;
loader.scaleX = loader.scaleY = 0.5;
addChild(loader);
loader.addEventListener(PhotoLoader.COMPLETE, complete, false, 0, true);
var label:Label = new Label();
addChild(label);
label.x = 82;
label.y = 155;
label.text = "posterize";
label.textColor = 0x333333;
bitmap = new Bitmap();
bitmap.x = 72;
bitmap.y = 180;
addChild(bitmap);
photoList = new Array();
photoList.push({label: "cat", path: "bitmapDataUtils_photo.jpg"});
photoList.push({label: "photo1", path: "bitmapDataUtils_photo1.jpg"});
photoList.push({label: "photo2", path: "bitmapDataUtils_photo2.jpg"});
photoList.push({label: "photo3", path: "bitmapDataUtils_photo3.jpg"});
photoList.push({label: "photo4", path: "bitmapDataUtils_photo4.jpg"});
menu = new Menu();
addChild(menu);
menu.x = 162;
menu.y = 5;
menu.init({label: "photo"});
menu.dataProvider = photoList;
menu.initialize(photoID);
menu.addEventListener(CompoEvent.SELECT, select, false, 0, true);
load(photoID);
tonesList = new Array();
tonesList.push({label: "2", value: 2});
tonesList.push({label: "3", value: 3});
tonesList.push({label: "4", value: 4});
tonesList.push({label: "5", value: 5});
tonesList.push({label: "8", value: 8});
tonesList.push({label: "10", value: 10});
tonesList.push({label: "16", value: 16});
tonesList.push({label: "32", value: 32});
toneMenu = new Menu();
addChild(toneMenu);
toneMenu.x = 277;
toneMenu.y = 155;
toneMenu.init({label: "tones"});
toneMenu.dataProvider = tonesList;
toneMenu.initialize(0);
toneMenu.addEventListener(CompoEvent.SELECT, selectTone, false, 0, true);
}
private function select(evt:CompoEvent):void {
loader.unload();
photoID = evt.value;
load(photoID);
}
private function load(id:uint):void {
var filePath:String = photoList[id].path;
loader.load(basePath + filePath, true);
}
private function complete(evt:Event):void {
reset();
var content:Bitmap = PhotoLoader(evt.target).content;
bitmap.bitmapData = content.bitmapData;
apply();
}
private function selectTone(evt:CompoEvent):void {
tones = tonesList[evt.value].value;
apply();
}
private function apply():void {
var content:Bitmap = loader.content;
bitmap.bitmapData = BitmapDataUtils.posterize(content.bitmapData, tones);
}
private function reset():void {
toneMenu.initialize(0);
tones = tonesList[0].value
}
}
}
//////////////////////////////////////////////////
// BitmapDataUtilsクラス
//////////////////////////////////////////////////
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.filters.ColorMatrixFilter;
class BitmapDataUtils {
private static var point:Point = new Point();
private static var rLuma:Number = 0.212671;
private static var gLuma:Number = 0.71516;
private static var bLuma:Number = 0.072169;
public static const THRESHOLD_LOW:String = "<=";
public static const THRESHOLD_HIGH:String = ">=";
public function BitmapDataUtils() {
}
public static function grayscale(bd:BitmapData):BitmapData {
var bitmapData:BitmapData = new BitmapData(bd.width, bd.height, false);
var colorMatrixFilter:ColorMatrixFilter = new ColorMatrixFilter([rLuma, gLuma, bLuma, 0, 0, rLuma, gLuma, bLuma, 0, 0, rLuma, gLuma, bLuma, 0, 0, 0, 0, 0, 1, 0]);
var rect:Rectangle = new Rectangle(0, 0, bd.width, bd.height);
bitmapData.applyFilter(bd, rect, point, colorMatrixFilter);
return bitmapData;
}
public static function binarize(bd:BitmapData, operation:String, threshold:uint):BitmapData {
var bitmapData:BitmapData = new BitmapData(bd.width, bd.height, true, 0xFFFFFFFF);
var rect:Rectangle = new Rectangle(0, 0, bd.width, bd.height);
bitmapData.threshold(bd, rect, point, operation, threshold, 0xFF000000, 255, false);
return bitmapData;
}
public static function posterize(bd:BitmapData, tones:uint):BitmapData {
if (tones < 2) return bd;
var bitmapData:BitmapData = new BitmapData(bd.width, bd.height, false);
var rect:Rectangle = new Rectangle(0, 0, bd.width, bd.height);
var rPalette:Array = new Array();
var gPalette:Array = new Array();
var bPalette:Array = new Array();
var min:uint = 0;
var max:uint = 255;
for (var t:uint = 1; t <= tones; t++) {
max = Math.floor(256*t/tones);
var color:uint = Math.floor(255*(t-1)/(tones-1));
for (var n:uint = min; n < max; n++) {
rPalette[n] = color << 16;
gPalette[n] = color << 8;
bPalette[n] = color;
}
min = max;
}
bitmapData.paletteMap(bd, rect, point, rPalette, gPalette, bPalette);
return bitmapData;
}
}
//////////////////////////////////////////////////
// PhotoLoaderクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.display.Bitmap;
import flash.system.LoaderContext;
class PhotoLoader extends Sprite {
private var loader:Loader;
private var info:LoaderInfo;
public var content:*;
private var smoothing:Boolean;
public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
public static const INIT:String = Event.INIT;
public static const COMPLETE:String = Event.COMPLETE;
public function PhotoLoader() {
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String, s:Boolean = false):void {
smoothing = s;
info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
info.addEventListener(Event.INIT, initialize, false, 0, true);
info.addEventListener(Event.COMPLETE, complete, false, 0, true);
try {
loader.load(new URLRequest(file), new LoaderContext(true));
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
private function ioerror(evt:IOErrorEvent):void {
loader.unload();
dispatchEvent(new Event(PhotoLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(PhotoLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(PhotoLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
if (smoothing) {
content = Bitmap(info.content);
content.smoothing = true;
} else {
content = info.content;
}
dispatchEvent(new Event(PhotoLoader.INIT));
}
private function complete(evt:Event):void {
info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
info.removeEventListener(Event.INIT, initialize);
info.removeEventListener(Event.COMPLETE, complete);
addChild(loader);
dispatchEvent(new Event(PhotoLoader.COMPLETE));
}
public function centerize():void {
content.x = -uint(content.width*0.5);
content.y = -uint(content.height*0.5);
}
}
//////////////////////////////////////////////////
// Menuクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
class Menu extends Sprite {
public var id:uint;
private var selectedID:uint;
private var selectedItem:TextField;
private var tab:Sprite;
private var base:Shape;
private var txt:TextField;
private var label:String = "";
private static var fontType:String = "_ゴシック";
private var _width:uint = 60;
private static var _height:uint = 20;
private static var tHeight:uint = 20;
private static var bColor:uint = 0xFFFFFF;
private static var cColor:uint = 0x3165B5;
private static var upColor:uint = 0x000000;
private static var overColor:uint = 0xFFFFFF;
private static var offColor:uint = 0x999999;
private static var bColorTrans:ColorTransform;
private static var cColorTrans:ColorTransform;
private var child:MenuChild;
private var dataList:Array;
private var _enabled:Boolean = true;
private var _selected:Boolean = false;
public function Menu() {
}
public function init(option:Object):void {
if (option.id != undefined) id = option.id;
if (option.label != undefined) label = option.label;
if (option.width != undefined) _width = option.width;
draw();
}
private function draw():void {
bColorTrans = new ColorTransform();
bColorTrans.color = bColor;
cColorTrans = new ColorTransform();
cColorTrans.color = cColor;
selectedItem = new TextField();
tab = new Sprite();
base = new Shape();
txt = new TextField();
addChild(selectedItem);
addChild(tab);
tab.addChild(base);
tab.addChild(txt);
selectedItem.x = _width + 5;
selectedItem.y = 1;
selectedItem.width = _width;
selectedItem.height = _height - 1;
selectedItem.type = TextFieldType.DYNAMIC;
selectedItem.selectable = false;
//selectedItem.embedFonts = true;
//selectedItem.antiAliasType = AntiAliasType.ADVANCED;
var tfl:TextFormat = new TextFormat();
tfl.font = fontType;
tfl.size = 12;
tfl.align = TextFormatAlign.LEFT;
selectedItem.defaultTextFormat = tfl;
createBox(base, _width, _height);
txt.y = 1;
txt.width = _width;
txt.height = _height - 1;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 12;
tf.align = TextFormatAlign.CENTER;
txt.defaultTextFormat = tf;
txt.text = label;
_up();
enabled = true;
tab.mouseChildren = false;
}
private function rollOver(evt:MouseEvent):void {
_over();
}
private function rollOut(evt:MouseEvent):void {
up();
}
private function press(evt:MouseEvent):void {
_over();
}
private function release(evt:MouseEvent):void {
_over();
}
private function click(evt:MouseEvent):void {
_over();
child.opencloseMenu();
}
private function up():void {
if (_selected) {
_over();
} else {
_up();
}
}
private function _up():void {
txt.textColor = upColor;
base.transform.colorTransform = bColorTrans;
}
private function _over():void {
txt.textColor = overColor;
base.transform.colorTransform = cColorTrans;
}
private function _off():void {
txt.textColor = offColor;
base.transform.colorTransform = bColorTrans;
}
public function set dataProvider(list:Array):void {
dataList = list;
if (dataList.length > 0) addChildren();
}
private function addChildren():void {
child = new MenuChild(dataList, this);
addChild(child);
child.y = tHeight;
child.visible = false;
child.addEventListener(MouseEvent.CLICK, select, false, 0, true);
}
private function mouseDown(evt:MouseEvent):void {
if (!hitTestPoint(stage.mouseX, stage.mouseY, true)) {
child.closeMenu();
}
}
public function initialize(param:uint):void {
selectedID = param;
child.selectItem(selectedID);
show();
}
private function select(evt:MouseEvent):void {
selectedID = evt.target.id;
var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, selectedID);
dispatchEvent(e);
show();
}
public function get selected():Boolean {
return _selected;
}
public function set selected(param:Boolean):void {
_selected = param;
if (_enabled) up();
show();
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(param:Boolean):void {
_enabled = param;
tab.buttonMode = _enabled;
tab.mouseEnabled = _enabled;
tab.useHandCursor = _enabled;
if (_enabled) {
_up();
tab.addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
tab.addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
tab.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
tab.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
tab.addEventListener(MouseEvent.CLICK, click, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown, false, 0, true);
} else {
_off();
tab.removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
tab.removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
tab.removeEventListener(MouseEvent.MOUSE_DOWN, press);
tab.removeEventListener(MouseEvent.MOUSE_UP, release);
tab.removeEventListener(MouseEvent.CLICK, click);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
}
private function createBox(target:Shape, w:uint, h:uint):void {
target.graphics.clear();
target.graphics.beginFill(bColor);
target.graphics.drawRect(0, 0, w, h);
target.graphics.endFill();
}
private function show():void {
var str:String = dataList[selectedID].label;
selectedItem.text = String(str);
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
class MenuChild extends Sprite {
private var _width:uint = 100;
private var _height:uint;
private static var tHeight:uint = 20;
private static var bColor:uint = 0xFFFFFF;
private static var sColor:uint = 0x000000;
private var dataList:Array;
private var max:uint;
private var itemList:Array;
private var maxWidth:uint = 0;
private var back:Shape;
private var shade:DropShadowFilter;
private var menu:Menu;
private var opened:Boolean = false;
private var selectedID:uint;
public function MenuChild(list:Array, m:Menu) {
dataList = list;
max = dataList.length;
_height = tHeight*max;
menu = m;
init();
}
private function init():void {
back = new Shape();
addChild(back);
shade = new DropShadowFilter(1, 90, sColor, 0.5, 4, 4, 2, 3, false, false);
back.filters = [shade];
itemList = new Array();
for (var n:uint = 0; n < max; n++) {
var item:MenuItem = new MenuItem({id: n, label: dataList[n].label});
addChild(item);
item.y = tHeight*n;
itemList.push(item);
item.addEventListener(MouseEvent.CLICK, select, false, 0, true);
resizeWidth(item);
}
}
private function select(evt:MouseEvent):void {
closeMenu();
selectedID = MenuItem(evt.currentTarget).id;
checkItem();
}
public function selectItem(id:uint):void {
selectedID = id;
checkItem();
}
public function opencloseMenu():void {
if (!opened) {
openMenu();
} else {
closeMenu();
}
}
private function openMenu():void {
opened = true;
visible = true;
menu.selected = true;
}
public function closeMenu():void {
opened = false;
visible = false;
menu.selected = false;
}
private function checkItem():void {
for (var n:uint = 0; n < itemList.length; n++) {
var item:MenuItem = itemList[n];
if (n == selectedID) {
item.selected = true;
} else {
item.selected = false;
}
}
}
private function resizeWidth(item:MenuItem):void {
if (item._width > maxWidth) maxWidth = item._width;
if (itemList.length >= max) resize();
}
private function resize():void {
_width = maxWidth;
createBox(back, _width, _height);
for (var n:uint = 0; n < max; n++) {
var item:MenuItem = itemList[n];
item._width = maxWidth;
item.txt.width = item._width - 20;
createBox(item.base, item._width, item._height);
}
}
private function createBox(target:Shape, w:uint, h:uint):void {
target.graphics.clear();
target.graphics.beginFill(bColor);
target.graphics.drawRect(0, 0, w, h);
target.graphics.endFill();
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
class MenuItem extends Sprite {
public var id:uint;
private var item:Sprite;
public var base:Shape;
public var txt:TextField;
private var check:TextField;
public var _width:uint = 100;
public var _height:uint = 20;
private var label:String = "";
private static var fontType:String = "_ゴシック";
private var mark:String = String.fromCharCode(10003);
private static var checkType:String = "_ゴシック";
private static var bColor:uint = 0xFFFFFF;
private static var cColor:uint = 0x3165B5;
private static var upColor:uint = 0x000000;
private static var overColor:uint = 0xFFFFFF;
private static var bColorTrans:ColorTransform;
private static var cColorTrans:ColorTransform;
private var _selected:Boolean = false;
public function MenuItem(option:Object) {
if (option.id != undefined) id = option.id;
if (option.label) label = option.label;
init();
}
private function init():void {
bColorTrans = new ColorTransform();
bColorTrans.color = bColor;
cColorTrans = new ColorTransform();
cColorTrans.color = cColor;
item = new Sprite();
base = new Shape();
txt = new TextField();
check = new TextField();
addChild(item);
item.addChild(base);
item.addChild(txt);
item.addChild(check);
txt.x = 20;
txt.y = 1;
txt.width = _width;
txt.height = _height - 1;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 12;
tf.align = TextFormatAlign.LEFT;
txt.defaultTextFormat = tf;
txt.text = label;
_width = txt.textWidth + 35;
check.x = 3;
check.y = -1;
check.width = 14;
check.height = 22;
check.type = TextFieldType.DYNAMIC;
check.selectable = false;
//check.embedFonts = true;
//check.antiAliasType = AntiAliasType.ADVANCED;
var tfc:TextFormat = new TextFormat();
tfc.font = checkType;
tfc.size = 12;
tfc.align = TextFormatAlign.CENTER;
check.defaultTextFormat = tfc;
check.text = mark;
check.visible = false;
buttonMode = true;
mouseEnabled = true;
useHandCursor = true;
_up();
mouseChildren = false;
addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
addEventListener(MouseEvent.CLICK, click, false, 0, true);
}
private function rollOver(evt:MouseEvent):void {
_over();
}
private function rollOut(evt:MouseEvent):void {
_up();
}
private function press(evt:MouseEvent):void {
_over();
}
private function release(evt:MouseEvent):void {
_up();
}
private function click(evt:MouseEvent):void {
_up();
}
private function _up():void {
txt.textColor = upColor;
check.textColor = upColor;
base.transform.colorTransform = bColorTrans;
}
private function _over():void {
txt.textColor = overColor;
check.textColor = overColor;
base.transform.colorTransform = cColorTrans;
}
public function get selected():Boolean {
return _selected;
}
public function set selected(param:Boolean):void {
_selected = param;
check.visible = _selected;
}
}
//////////////////////////////////////////////////
// CompoEventクラス
//////////////////////////////////////////////////
import flash.events.Event;
class CompoEvent extends Event {
public static const SELECT:String = "select";
public static const CHANGE:String = "change";
public var value:*;
public function CompoEvent(type:String, value:*) {
super(type);
this.value = value;
}
public override function clone():Event {
return new CompoEvent(type, value);
}
}
//////////////////////////////////////////////////
// Labelクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
class Label extends Sprite {
private var txt:TextField;
private static var fontType:String = "_ゴシック";
private static var _height:uint = 20;
public function Label() {
draw();
}
private function draw():void {
txt = new TextField();
addChild(txt);
txt.height = _height - 1;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 12;
tf.align = TextFormatAlign.LEFT;
txt.defaultTextFormat = tf;
}
public function set text(param:String):void {
txt.text = param;
}
public function set textColor(param:uint):void {
txt.textColor = param;
}
}