How do I set a certain color to be transparent?

懵懂的女人 提交于 2019-12-23 03:26:08

问题


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 Bitmaps' edges left over. How do I set the color white in the Bitmap to be transparent so I won't see these unsightly edges?


回答1:


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



回答2:


you can use BitmapData.colorTransform() or draw() instead of copyPixels() to adjust color there



来源:https://stackoverflow.com/questions/5382161/how-do-i-set-a-certain-color-to-be-transparent

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