apache-flex - 如何将所有不透明的像素着色为黑色

标签 apache-flex actionscript-3 colormatrixfilter

我正在 Flex 中的图像上使用 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);
    }

我希望所有像素都是纯黑色,除非像素完全透明并且不确定如何调整值以将其从中取出。

最佳答案

您应该看看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)。

在本例中,threshold0xff000000(ARGB 值),对应于 100% 透明度的黑色。我们的 mask 颜色也设置为 0xff000000,它告诉 threshold() 我们只对每个像素的 alpha(ARGB 中的“A”)值感兴趣。我们的 operation 值是“小于”,这意味着如果通过应用 maskColor 确定的像素值低于 threshold,请将其替换为 color.

关于apache-flex - 如何将所有不透明的像素着色为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6959211/

相关文章:

apache-flex - 如何清除或重置 Adob​​e Flash Builder 的首选项?

apache-flex - 当应用程序繁忙时,我们如何在 Flex 中实现 "Please wait ...."屏幕

actionscript-3 - Actionscript 3 和动态蒙版

actionscript-3 - 如何检查文件是否存在?

android - 使用 ColorMatrix 时如何理解和选择值?

apache-flex - 在 Flex 中调整容器大小的最佳方法是仅遵循父容器的显式尺寸

apache-flex - 通过 ref 传递引用类型 - Flex/Actionscript

c# - 在 C# 中使用正则表达式更改字符串(基于 ActionScript 函数)

ffmpeg - 如何获得 ffmpeg 的 colorchannelmixer 的颜色矩阵