如何为所有对黑色不透明的像素着色

时间:2011-08-05 16:01:43

标签: flex actionscript-3 colormatrixfilter

我在Flex中的Image上使用ColorMatixFilter。我真的很想得到我需要的过滤器。基本上用户上传的任何PNG文件我希望所有不透明的像素都是黑色的。我有一个设置“亮度”的功能,所以我只是通过一个非常大的负数,比如-1000而且它完成了工作,但问题是任何像素对它们有任何alpha,比如0.9或者以下都会结束我稍后在服务器上编码PNG文件时会变白。

这是我目前正在使用的代码

public static function setBrightness(value:Number):ColorMatrixFilter
    {
        value = value * (255 / 250);

        var m:Array = new Array();
        m = m.concat([1, 0, 0, 0, value]); // red
        m = m.concat([0, 1, 0, 0, value]); // green
        m = m.concat([0, 0, 1, 0, value]); // blue
        m = m.concat([0, 0, 0, 1, 0]); // alpha

        return new ColorMatrixFilter(m);
    }

我希望所有像素都是纯黑色,除非像素是完全透明的,并且不确定如何调整值以使其得到它。

1 个答案:

答案 0 :(得分:6)

你应该看看BitmapData.threshold(),因为它几乎完全符合你的要求。在链接上解释示例,你应该做这样的事情:

// png is your PNG BitmapData
var bmd:BitmapData = new BitmapData(png.width, png.height, true, 0xff000000);
var pt:Point = new Point(0, 0);
var rect:Rectangle = bmd.rect;
var operation:String = "<";
var threshold:uint = 0xff000000;
var color:uint = 0x00000000;
var maskColor:uint = 0xff000000;
bmd.threshold(png, rect, pt, operation, threshold, color, maskColor, true);

我们在此处设置的是调用threshold()来检查png的每个像素,如果该像素的alpha值不是100%,则用黑色替换像素颜色( 0xff的)。

在这种情况下,threshold0xff000000ARGB值),对应于100%透明度的黑色。我们的蒙版颜色也设置为0xff000000,告诉threshold()我们只对每个像素的alpha(ARGB中的'A')值感兴趣。我们operation的值“小于”意味着如果通过应用maskColor确定的像素值低于threshold,则将其替换为color