如何在 AS2 中为导入图像设置透明度颜色



如何设置颜色 BLACK:0x000000是透明的,通常魔术粉红色是透明的,但我想将黑色设置为透明。

如果您不明白:http://j.imagehost.org/0829/WoodyGX_0.jpg

我有那个图像,在转换 80x80 精灵时,我希望背景是透明的,这意味着:没有背景,只有角色。

此时,您可能最好将其放入Fireworks中,使用魔杖选择黑色像素,删除它们,然后将其保存为透明的png。然后使用它。

但是,如果你想让你的生活比它需要的更难,你可以使用 getPixel 来获取所有的黑色像素,然后使用 setPixel 将它们设置为透明。但块传输的全部意义在于速度,而不是缓慢的逐像素操作。

注意:这是 ActionScript 3 中的一个答案,以防您决定迁移到 ActionScript 3,但对于其他人和一般信息更是如此。



您可以从源BitmapData创建新BitmapData,并删除黑色像素(转换为 Alpha 通道)。

我为您创建了这个函数:

// Takes a source BitmapData and converts it to a new BitmapData, ignoring
// dark pixels below the specified sensitivity.
function removeDarkness(source:BitmapData, sensitivity:uint = 10000):BitmapData
{
    // Define new BitmapData, with some size constraints to ensure the loop
    // doesn't time out / crash.
    // This is for demonstration only, consider creating a class that manages
    // portions of the BitmapData at a time (up to say 50,000 iterations per
    // frame) and then dispatches an event with the new BitmapData when done.
    var fresh:BitmapData = new BitmapData(
        Math.min(600, source.width),
        Math.min(400, source.height),
        true, 0xFFFFFFFF
    );
    fresh.lock();
    // Remove listed colors.
    for(var v:int = 0; v < fresh.height; v++)
    {
        for(var h:int = 0; h < fresh.width; h++)
        {
            // Select relevant pixel for this iteration.
            var pixel:uint = source.getPixel(h, v);
            // Check against colors to remove.
            if(pixel <= sensitivity)
            {
                // Match - delete pixel (fill with transparent pixel).
                fresh.setPixel32(h, v, 0x00000000);
                continue;
            }
            // No match, fill with expected color.
            fresh.setPixel(h, v, pixel);
        }
    }

    // We're done modifying the new BitmapData.
    fresh.unlock();

    return fresh;
}

如您所见,它需要:

  • 要从中删除较暗像素的位图数据。
  • uint 表示您要删除多少黑色/灰色阴影。

下面是使用源图像的演示:

var original:Loader = new Loader();
original.load( new URLRequest("http://j.imagehost.org/0829/WoodyGX_0.jpg") );
original.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);

// Original image has loaded, continue.
function imageLoaded(e:Event):void
{
    // Capture pixels from loaded Bitmap.
    var obmd:BitmapData = new BitmapData(original.width, original.height, false, 0);
    obmd.draw(original);

    // Create new BitmapData without black pixels.
    var heroSheet:BitmapData = removeDarkness(obmd, 1200000);
    addChild( new Bitmap(heroSheet) );
}

最新更新