O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Modifying a Bitmap

The basic tools for assigning new colors to the pixels of an existing bitmap exactly mirror those for examining a bitmap. They are: setPixel32( ), setPixel( ), and setPixels( ). The setPixel32( ) method assigns a new 4-channel color value to a pixel as a 32-bit unsigned integer. It takes the following form:

thatBitmapDataObject.setPixel32(x, y, color)

where theBitmapDataObject is the BitmapData instance containing the pixel whose color value will be changed, x and y are the horizontal and vertical location of that pixel, and color is the new color value to assign to the pixel. For example, the following code creates a blue, square bitmap, and then sets the color value of its top-left pixel to white:

var imgData:BitmapData = new BitmapData(20, 20, false, 0xFF0000FF);
imgData.setPixel32(0, 0, 0xFFFFFFFF);

By contrast, the setPixel( ) method, which takes the same general form as setPixel32( ), sets only the Red, Green, and Blue channels of a pixel's color value, leaving the pixel's original Alpha channel unaltered. For example, the following code creates a partially transparent blue, square bitmap, and then sets the color value of its top-left pixel to white. Because setPixel( ) is used instead of setPixel32( ), the top left pixel retains its original Alpha channel value (0x66):

var imgData:BitmapData = new BitmapData(20, 20, true, 0x660000FF);
imgData.setPixel(0, 0, 0xFFFFFF);

After the setPixel( ) operation completes, the top-left pixel's color value is 0x66FFFFFF.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required