O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

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

Using Threshold

Problem

You want to change the value of some pixels in a bitmap, based on their current value.

Solution

Use the threshold() method of the BitmapData class.

Discussion

The threshold() method is probably the most complex in the BitmapData API, but quite powerful once you understand how it works. The method uses two BitmapData objects:

  • destBitmap, which is the bitmap that will be altered.

  • sourceBitmap, which is the other bitmap that gets passed in as a parameter. The method uses this bitmap’s pixel data for its calculations.

The method compares each pixel in sourceBitmap against a specified value, using one of six comparison operators. If the comparison fails, the corresponding pixel in destBitmap is set to a specified color value. If it passes the comparison, the pixel is either not changed, or you have the option of copying over the sourceBitmap’s value for that pixel.

Here is the syntax for the method:

destBitmap.threshold(sourceBitmap,
                     sourceRect,
                     destPoint,
                     operation,
                     threshold,
                     color,
                     mask,
                     copySource)

We’ve already covered destBitmap and sourceBitmap. The next parameter, sourceRect, is an instance of the flash.geom.Rectangle class. It defines what portion of the sourceBitmap you want to use for comparison. If you want to use the entire bitmap, you can pass in sourceBitmap.rect as a value to this parameter.

The destPoint parameter specifies the point in the destBitmap at which the pixels start to be affected. Picture the sourceBitmap overlaid on destBitmap, with its ...

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