Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Masking for BitmapData Methods

Masking for BitmapData Methods

Two methods of the BitmapData object accept bit masks as parameters: Threshold and getColorBoundsRect. In both cases, the mask specifies which bits in the color value the method should use in its comparisons. For example, if you wanted to use getColorBoundsRect to find the area containing all pixels which had a specific value for only the green channel, you could do the following:

var greenValueToMatch:Number = 0x80;
// set up a mask with only the green channel bits turned on:
var colorMask:Number = 0x0000FF00;
// call the method, and don't forget to shift the green bits
// into the correct position:
rect = myBitmapData.getColorBoundsRect(colorMask,greenValueToMatch<<8);

The getColorBoundsRect method uses the AND operator internally to apply the mask. It essentially checks each pixel in the bitmap to see whether it matches using a formula similar to the following:

(bitMask & colorValueToMatch) == (bitMask & sourcePixelColorValue)

Threshold uses bit masks in a nearly identical way. The following code makes all pixels with a red channel value greater than 0x99 turn white (0xFFFFFFFF):

// set up a mask with only the red channel bits turned on:
var colorMask:Number = 0x00FF0000;
// call the method

For more specific information on these methods, check out the following pages in the Flash 8 LiveDocs: