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.
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:
What we’ve set up here is a call to
threshold()that will examine each pixel ofpngand replace the pixel color with with black if the alpha value for that pixel is not 100% (0xff).In this case
thresholdis0xff000000(an ARGB value) which corresponds with black at 100% transparency. Our mask color is also set to0xff000000which tellsthreshold()that we are only interested in the alpha (the ‘A’ in ARGB) values for each pixel. Our value foroperationis “less than” meaning if the pixel value determined by applyingmaskColoris belowthresholdreplace it withcolor.