Guilherme Garcia da Rosa
Guilherme Garcia da Rosa

Reputation: 1030

How can I set a color to transparency for an import image in AS2

How can I set the color BLACK: 0x000000 to be transparent, normaly the magic pink is transparent but i want to set BLACK to be.

If you don't understand: http://j.imagehost.org/0829/WoodyGX_0.jpg

I have that image, and when converting the 80x80 sprite I want the background to be transparent, meaning: no background, only the character.

Upvotes: 1

Views: 279

Answers (2)

Marty
Marty

Reputation: 39458

Notice: This is an answer in ActionScript 3, in case you either decide to migrate to ActionScript 3, but moreso for other people and general info.



You can create new BitmapData from the source BitmapData with the black pixels removed (converted to alpha channel).

I've created this function for you:

// 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;
}

As you can see, it takes:

  • BitmapData that you want to remove darker pixels from.
  • uint representing how many shades of black / grey you want to have removed.

And here's a demo using your source image:

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

Upvotes: 1

Amy Blankenship
Amy Blankenship

Reputation: 6961

You're probably better off at this point just taking it into Fireworks, using the magic wand to select the black pixels, deleting them, and saving it out as a transparent png. Then use that.

However, if you want to make your life lits harder than it needs to be, you could potentially use getPixel to get all your black pixels and then use setPixel to set them to transparent. But the whole point of blitting is speed, not slow pixel-by-pixel operations.

Upvotes: 2

Related Questions