get the color of movieClip

跟風遠走 提交于 2020-01-05 04:10:09

问题


I want to get the color from the movieclip. I am Using Following Code:

for (var j = 0; j <bmpd.width; j++)
{
    for (var k = 0; k <bmpd.height; k++)
    {
        trace("0x"+bmpd.getPixel(j,k).toString(16))
    }
}

Here Circle is a MovieClip.please guide me


回答1:


To get a color on your MovieClip at a certain point or range, you should create a transparent BitmapData of required capacity (1x1 for point), fill it with 0x0 color, then create a transform matrix aligning (0,0) of BitmapData to upper left corner of your region, then draw that MC on the bitmap, then you can query its pixels. An example (a point):

private static var hitTestBD:BitmapData=new BitmapData(1,1,true,0);
private static vat hitTestMatrix:Matrix=new Matrix(); 
public static function getMCColor(mc:DisplayObject,tx:Number,ty:Number):uint {
    hitTestMatrix.identity();
    hitTestMatrix.translate(-1*tx,-1*ty); // aligning
    hitTestBD.fillRect(hitTestBD.rect,0x0);
    hitTestBD.draw(mc,hitTestMatrix);
    return hitTestBD.getPixel32(0,0);
}



回答2:


You have to extract the color channels:

for (var j = 0; j < bmpd.width; j++) {
    for (var k = 0; k < bmpd.height; k++) {
        // Read the pixel color
        var color:uint = bmpd.getPixel(j, k);

        // Read color channel values
        var alpha:uint = color >> 24 & 0xFF;
        var red:uint   = color >> 16 & 0xFF;
        var green:uint = color >> 8 & 0xFF;
        var blue:uint  = color & 0xFF;

        // Reassemble the color
        trace("color: 0x" + red.toString(16) + green.toString(16) + blue.toString(16));
        trace("alpha: 0x" + alpha.toString(16));
    }
}


来源:https://stackoverflow.com/questions/15967166/get-the-color-of-movieclip

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