how do I color all pixels that are NOT transparent to black

冷暖自知 提交于 2019-12-11 00:26:28

问题


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.

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.


回答1:


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

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).

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.



来源:https://stackoverflow.com/questions/6959211/how-do-i-color-all-pixels-that-are-not-transparent-to-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!