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

mat*_*ace 2 apache-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);
    }
Run Code Online (Sandbox Code Playgroud)

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

Noo*_*le2 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);
Run Code Online (Sandbox Code Playgroud)

我们在这里设置的是调用threshold()它将检查每个像素png并用黑色替换像素颜色,如果该像素的alpha值不是100%(0xff).

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