[AS3]位图的色阶控制

2008-11-02 15:43:10

[img]attachments/month_0811/j2008112154151.gif[/img] 得到方法: 灰色化。把各个通道求平均。用ColorMatrixFilter即可。 用threshold来得到颜色分布。threshold的返回值为匹配的像素的个数。 代码: AS3的位图操作果然很强大 [code] package { import flash.display.*; import flash.filters.ColorMatrixFilter; import flash.geom.Point; public class TestBitmap extends Sprite { [Embed(source=]uot;023.jpg&#34;)] private var SampleImage:Class; public function TestBitmap() { var bmd:BitmapData = Bitmap(addChild(new SampleImage())).bitmapData; var s:Sprite = new Sprite(); cr&#101;ateHistogram(bmd,s); addChild(s).y = bmd.height + 10; } //生成色阶 private function cr&#101;ateHistogram(bmd:BitmapData, s:Sprite):void { //灰度画 var cmf:ColorMatrixFilter = new ColorMatrixFilter( [1 / 3, 1 / 3, 1 / 3, 0, 0, 1 / 3, 1 / 3, 1 / 3, 0, 0, 1 / 3, 1 / 3, 1 / 3, 0, 0] ); var bmd2:BitmapData = bmd.clone(); bmd2.applyFilter(bmd2, bmd2.rect, new Point(), cmf); //用threshold来得到颜色分布 var values:Array = [] for(var i:int = 0; i < 0x256; i++) { values = bmd2.threshold(bmd2, bmd2.rect, new Point(), &#34;==&#34;, i, 0, 0xff, false); } bmd2.dispose(); //画色阶 var max:int = bmd.width * bmd.height / 50; s.graphics.lineStyle(1); for(i = 0; i < 256; i++) { s.graphics.moveTo(i, 100); s.graphics.lineTo(i, Math.max(0, 100 - values / max * 100)); } } } } [/code]