【问题标题】:how do I color all pixels that are NOT transparent to black如何为所有对黑色不透明的像素着色
【发布时间】:2011-08-05 16:01:42
【问题描述】:

我在 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);
    }

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

【问题讨论】:

  • 我测试了ColorMatrix,它看起来对我来说是正确的。问题可能与您如何编码为 png 有关。许多编码器在此过程中丢失了 Alpha 通道。你能提供更多细节吗?另外,为什么不在客户端编码? ImageSnapshot 可以做 png 编码。
  • 就像 Jacob Eggers 所说,当透明像素失去其 Alpha 通道时,白色通常是默认颜色

标签: apache-flex actionscript-3 colormatrixfilter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 2016-06-14
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    相关资源
    最近更新 更多