Forked from: bkzen 's 画像の平均色 diff:36 forked from: 画像の平均色 Saqoosha forked:1 favorite:19 lines:93 license : MIT License modified : 2010-07-07 17:31:31 // forked from bkzen's 画像の平均色 package { import com.bit101.components.Label; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.events.Event; import flash.filters.BlurFilter; import flash.geom.Matrix; import flash.geom.Point; import flash.geom.Rectangle; import flash.utils.ByteArray; import flash.utils.getTimer; public class FlashTest extends Sprite { private const moja: String = "moja"; public function FlashTest() { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // var sp: Sprite = new Sprite(); addChild(sp); var bmd: BitmapData = new BitmapData(100, 100, false); var baseXY: Number = 32; bmd.perlinNoise(baseXY, baseXY, 2, 0xFFFF * Math.random(), true, true, Math.random()*7+1); var bmdA: BitmapData = new BitmapData(bmd.width, bmd.height, false); var bmdB: BitmapData = new BitmapData(bmd.width, bmd.height, false); var bmdC:BitmapData = bmdA.clone(); sp.addChild(new Bitmap(bmd, "auto", true)); var c: uint, rgb: uint, r: uint, g: uint, b: uint, t: int; // 拡大縮小による平均 t = getTimer(); bmdA.draw(bmd, new Matrix(1/bmd.width, 0, 0, 1/bmd.height), null, null, null, true); bmdA.draw(bmdA, new Matrix(bmd.width, 0, 0, bmd.height)); c = bmdA.getPixel(10, 10); new Label(sp, 0, 210, c.toString(0x10).toUpperCase() + "\n" + (getTimer() - t) + " ms"); // 全ピクセル走査による平均 t = getTimer(); var bytes: ByteArray = bmd.getPixels(bmd.rect); bytes.position = 0; while (bytes.bytesAvailable) { rgb = bytes.readUnsignedInt() & 0xFFFFFF; r += (rgb >> 0x10) & 0xFF; g += (rgb >> 0x08) & 0xFF; b += (rgb >> 0x00) & 0xFF; } var cnt: uint = bmd.width * bmd.height; r = r / cnt; g = g / cnt; b = b / cnt; c = (r << 0x10) | (g << 0x08) | (b << 0x00); bmdB.fillRect(bmdB.rect, c); new Label(sp, 110, 210, c.toString(0x10).toUpperCase() + "\n" + (getTimer() - t) + " ms"); // histgram t = getTimer(); c = calcAverageColor(bmd); bmdC.fillRect(bmdC.rect, c); new Label(sp, 220, 210, c.toString(0x10).toUpperCase() + "\n" + (getTimer() - t) + " ms"); // var bmpA: Bitmap = new Bitmap(bmdA, "auto", true); var bmpB: Bitmap = new Bitmap(bmdB, "auto", true); var bmpC: Bitmap = new Bitmap(bmdC, 'auto', true); bmpB.x = 105; bmpC.x = 210; bmpA.y = bmpB.y = bmpC.y = 105; sp.addChild(bmpA); sp.addChild(bmpB); sp.addChild(bmpC); sp.x = 100; sp.y = 100; } public function calcAverageColor(image:BitmapData, rect:Rectangle = null):uint { var hist:Vector.<Vector.<Number>> = image.histogram(rect); var r:Number = 0; var g:Number = 0; var b:Number = 0; var a:Number = 0; for (var i:int = 0; i < 256; ++i) { r += hist[0][i] * i; g += hist[1][i] * i; b += hist[2][i] * i; a += hist[3][i] * i; } var p:int = rect ? rect.width * rect.height : image.width * image.height; r /= p; g /= p; b /= p; a /= p; return (a << 24) | (r << 16) | (g << 8) | b; } } } Code Fullscreen Preview Fullscreen aaafl hacker_aft9c.. kuma360 stwind tkinjo skn alumican_net.. demouth ProjectNya linktale paq ezshine_y nanlow hidrodixtion.. : bitmapdata zmaxlin : bitmapdata color clockmaker : BitmapData#histgramを利用した平均値算出。高速かつ単純平均と同値。 takaki8 : BitmapData color miniapp : BitmapData color bkzen : ヒストグラムを使用した平均色 BitmapData color toUpperCase toString Event.ADDED_TO_STAGE clone stage removeEventListener width height Rectangle addEventListener addChild Vector Event String Math.random uint Sprite int Number sort new page view favorite forked pv 0 forked from: forked from: 画像の平.. stwind forked:0 favorite:0 lines:93 ( diff:1 )