开发者

Get dominant or average color in getPixels ByteArray

开发者 https://www.devze.com 2022-12-16 11:26 出处:网络
So, I\'ve got this code: import flash.display.BitmapData; import flash.display.Bitmap; import flash.geom.Rectangle;

So, I've got this code:

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.utils.ByteArray;

var bmd:BitmapData = new BitmapData(10, 10, true);开发者_C百科
var seed:int = int(Math.random() * int.MAX_VALUE);
bmd.noise(seed);

var bounds:Rectangle = new Rectangle(0, 0, bmd.width, bmd.height);
var pixels:ByteArray = bmd.getPixels(bounds);

Is there a way to get, efficiently and quickly, the dominant color and/or the average color in pixels ByteArray.

noise is used for the example here. I'll be having something else drawn on that BitmapData instead.

I found some ways to extract the average color palate from a BitmapData, but this isn't enough for me since I want the average from a rect over that image.

Thanks in advance!


The average color is easy, just resize the bitmapData by draw()ing it in to a 1x1 bitmap, flash will average the color in the copy process.

var m: Matrix = new Matrix();
m.scale( 1 / bmd.width, 1 / bmd.height);
var averageColorBmd:BitmapData = new BitmapData(1, 1);
averageColorBmd.draw(bmd, m);
var averageColor:uint = averageColorBmd.getPixel(0,0);

Something like that!


FYI this actually does not average or pick the dominant color.

doing it this way is relying on flash to shrink the image down to one pixel and then decide which color to use. I'm not sure how this color is chosen, but through tests it is NOT the average NOR is it the dominant color.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号