How do I set a certain color to be transparent?
I'm using copyPixels
to copy a parts of a larger Bitmap to smaller Bitmaps to use for individual MovieClips. However, around there is still some extra space white space and corners around the Bitm开发者_JAVA百科aps' edges left over. How do I set the color white in the Bitmap to be transparent so I won't see these unsightly edges?
You can use the BitmapData.threshold method. This code creates a BitmapData with a red square on a blue background, then uses the threshold method to make the red pixels transparent.
var inputBitmapData:BitmapData = new BitmapData(200, 200, true, 0xFF0000FF);
inputBitmapData.fillRect(new Rectangle(10, 10, 180, 180), 0xFFFF0000);
var outputBitmapData:BitmapData = new BitmapData(200, 200, true);
var destPoint:Point = new Point(0, 0);
var sourceRect:Rectangle = new Rectangle(0, 0, outputBitmapData.width, outputBitmapData.height);
var threshold:uint = 0xFFFF0000;
var color:uint = 0x00000000;
outputBitmapData.threshold(inputBitmapData, sourceRect, destPoint, "==", threshold, color, 0xFFFFFFFF, true);
var input:Bitmap = new Bitmap(inputBitmapData);
addChild(input);
var output:Bitmap = new Bitmap(outputBitmapData);
output.x = input.x + input.width + 10;
addChild(output);
you can use BitmapData.colorTransform()
or draw()
instead of copyPixels()
to adjust color there
精彩评论