我怎么颜色全部是不透明的黑色像素像素、不透明、颜色、黑色

2023-09-08 14:35:37 作者:你还欠我一个微笑

我使用ColorMatixFilter Flex中的图像。我真的很接近得到想我需要从过滤器中。基本上任何PNG文件我想所有非透明的为黑色像素,用户上传。我有一个功能,设置了亮度已经让我刚刚经历了一场真正的大负数,它像-1000和它的工作,但问题是,有什么阿尔法给他们的任何像素,说0.9或以下全部结束是白色的,当我连接code上的服务器后,我的PNG文件。

I am using ColorMatixFilter on an Image in Flex. I am really close to getting want I need out of the filter. Basically any PNG file the user uploads I want all pixels that are not transparent to be colored black. I have a function that sets the "brightness" already so I just through a really large negative number at it like -1000 and it does the job but the problem is any pixels that have any alpha to them, say 0.9 or below all end up being white when I encode my PNG file on the server later.

下面是我目前使用的code

here is the code I am currently using

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

我想所有的像素是纯黑色,除非像素是完全透明的,不知道如何来调整值获取出来。

I would like all pixels to be solid black unless the pixel is completely transparent and not sure how to tweak the values to get that out of it.

推荐答案

您应该看看 BitmapData.threshold()因为它pretty的多,你想要什么。意译链路上的例子中,你应该做的是这样的:

You should take a look at BitmapData.threshold() as it does pretty much exactly what you want. Paraphrasing the example on the link you should do something like this:

// 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);

我们已经建立了这里是调用阈值()将检查的每个像素为png 和与黑色替换像素的颜色,如果该像素的alpha值是不是100%(0xff的)。

What we've set up here is a call to threshold() that will examine each pixel of png and replace the pixel color with with black if the alpha value for that pixel is not 100% (0xff).

在这种情况下,阈值 0xff000000 (一的 ARGB 值),这相当于用黑色在100%的透明度。我们的面具颜色也设置为 0xff000000 它告诉阈值(),我们只关心阿尔法(以下简称 A'在ARGB)值对每个像素。我们对操作值小于的意思,如果 maskColor 通过应用确定的像素值低于阈值将其替换为颜色

In this case threshold is 0xff000000 (an ARGB value) which corresponds with black at 100% transparency. Our mask color is also set to 0xff000000 which tells threshold() that we are only interested in the alpha (the 'A' in ARGB) values for each pixel. Our value for operation is "less than" meaning if the pixel value determined by applying maskColor is below threshold replace it with color.