forked from: かゆうま
写真の中の人ををゾンビっぽくします。
写真: Business meeting
http://www.flickr.com/photos/llawliet/2547595587/
Uploaded on June 3, 2008 by llawliet
http://creativecommons.org/licenses/by/3.0/deed.ja
// forked from osamX's かゆうま
/*
写真の中の人ををゾンビっぽくします。
写真: Business meeting
http://www.flickr.com/photos/llawliet/2547595587/
Uploaded on June 3, 2008 by llawliet
http://creativecommons.org/licenses/by/3.0/deed.ja
*/
package
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.DisplayObject;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.system.*;
public class zombi extends Sprite
{
private var loader:Loader;
public function zombi():void
{
var context:LoaderContext = new LoaderContext(true);
var url:String = "http://farm4.static.flickr.com/3557/3429603435_3748f1235a_m.jpg";
loader = new Loader();
loader.load(new URLRequest(url),context);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
}
//画像ロードに失敗したとき
private function onLoadError(event:IOErrorEvent):void
{
//trace("load error!");
}
//画像ロード完了時
private function onLoadComplete(event:Event):void
{
var SrcBmd:BitmapData = new BitmapData(loader.width, loader.height);
SrcBmd.draw(loader);
addChild(loader);
//ピクセル単位でRGBを調べる
for (var py:uint = 0; py <= SrcBmd.height; py++) {
for (var px:uint = 0; px <= SrcBmd.width; px++) {
var RGB:uint = SrcBmd.getPixel(px, py);
var HSV:Array = RGBtoHSV(RGB);
//肌色なら
if ( HSV[0] != -1 && HSV[0] < 40 ) {
var ZombiRGB:Array = HSVtoRGB(120, HSV[1], HSV[2]);
SrcBmd.setPixel(px, py, (ZombiRGB[0]<<16)|(ZombiRGB[1]<<8)|(ZombiRGB[2]));
}
}
}
var DestBmp:Bitmap = new Bitmap(SrcBmd);
DestBmp.y = SrcBmd.height;
addChild(DestBmp);
}
//RGBからHSVに変換 Wikipedia参照
//http://ja.wikipedia.org/wiki/HSV%E8%89%B2%E7%A9%BA%E9%96%93
private function RGBtoHSV (RGB:uint):Array
{
var Hue:Number, Saturation:Number, Value:Number;
var Red:Number = (RGB >> 16)/0xFF;
var Green:Number = ((RGB >> 8) & 0xFF)/0xFF;
var Blue :Number = (RGB & 0xFF)/0xFF;
var Max:Array = MAX(Red, Green, Blue);
var Min:Array = MIN(Red, Green, Blue);
//Hueを求める
if (Max[0] == 0 || Max[0] - Min[0] == 0) {
Hue = -1;
}else if (Max[1] == 1) { //Red
Hue = 60 * (Green - Blue) / (Max[0] - Min[0]) + 0;
}else if (Max[1] == 2) { //Green
Hue = 60 * (Blue - Red) / (Max[0] - Min[0]) + 120;
}else if (Max[1] == 3) { //Blue
Hue = 60 * (Red - Green) / (Max[0] - Min[0]) + 240;
}
if (Hue < 0) Hue += 360;
//Saturationを求める
Saturation = (Max[0] - Min[0]) / Max[0];
//Valueを求める
Value = Max[0];
return [Hue, Saturation, Value];
}
//最大値
private function MAX (Num1:Number, Num2:Number, Num3:Number):Array
{
var Color :uint = 1;//(R=1 G=2 B=3)
var Max:Number = Num1;
if (Num2 > Max) {
Max = Num2;
Color = 2;
}
if (Num3 > Max) {
Max = Num3;
Color = 3;
}
return [Max, Color];
}
//最小値
private function MIN (Num1:Number, Num2:Number, Num3:Number):Array
{
var Color :uint = 1;//(R=1 G=2 B=3)
var Min:Number = Num1;
if (Num2 < Min) {
Min = Num2;
Color = 2;
}
if (Num3 < Min) {
Min= Num3;
Color = 3;
}
return [Min, Color];
}
//HSVからRGBに変換 ここから拝借
//http://www.libspark.org/browser/as3/AbstractUI/trunk/libs/org/libspark/utils/ColorUtil.as?rev=1732
public static function HSVtoRGB( h:Number, s:Number, v:Number ):Array
{
var r:Number=0, g:Number=0, b:Number=0;
var i:Number, x:Number, y:Number, z:Number;
h /= 60;
i = h >> 0;
x = v * (1 - s);
y = v * (1 - s * (h - i));
z = v * (1 - s * (1 - h + i));
switch(i){
case 0 : r=v; g=z; b=x; break;
case 1 : r=y; g=v; b=x; break;
case 2 : r=x; g=v; b=z; break;
case 3 : r=x; g=y; b=v; break;
case 4 : r=z; g=x; b=v; break;
case 5 : r=v; g=x; b=y; break;
}
return [ r*255>>0, g*255>>0, b*255>>0 ];
}
}
}