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!

Flash 8 Documentation: threshold (BitmapData.threshold method)

threshold (BitmapData.threshold method)

public threshold(sourceBitmap:BitmapData, sourceRect:Rectangle, destPoint:Point, operation:String, threshold:Number, [color:Number], [mask:Number], [copySource:Boolean]) : Number

Tests pixel values in an image against a specified threshold and sets pixels that pass the test to new color values. Using the threshold() method, you can isolate and replace color ranges in an image and perform other logical operations on image pixels.

The threshold test’s logic is as follows:

if ((pixelValue & mask) operation (threshold & mask)) then

   set pixel to color


   if (copySource) then

      set pixel to corresponding pixel value from sourceBitmap

The operation parameter specifies the comparison operator to use for the threshold test. For example, by using “==“, you can isolate a specific color value in an image. Or by using {operation: "<", mask: 0xFF000000, threshold: 0x7f000000, color: 0x00000000}, you can set all destination pixels to be fully transparent when the source image pixel’s alpha is less than 0x7F. You can use this technique for animated transitions and other effects.

Availability: ActionScript 1.0; Flash Player 8


sourceBitmap:flash.display.BitmapData – The input bitmap image to use. The source image can be a different BitmapData object or it can refer to the current BitmapData instance.

sourceRect:flash.geom.Rectangle – A rectangle that defnes the area of the source image to use as input.

destPoint:flash.geom.Point – The point within the destination image (the current BitmapData instance) that corresponds to upper-left corner of the source rectangle.

operation:String – One of the following comparison operators, passed as a String: “<“, “<=”, “>”, “>=”, “==”, “!=”

threshold:Number – The value that each pixel is tested against to see if it meets or exceeds the threshhold.

color:Number [optional] – The color value that a pixel is set to if the threshold test succeeds. The default is 0x00000000.

mask:Number [optional] – The mask to use to isolate a color component. The default value is 0xFFFFFFFF.

copySource:Boolean [optional] – A Boolean value. If the value is true, pixel values from the source image are copied to the destination when the threshold test fails. If the value is false, the source image is not copied when the threshold test fails. The default value is false.


Number – The number of pixels that were changed.


The following example shows how to change the color value of pixels whose color value is greater than or equal to a certain threshold.

import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;

var myBitmapData:BitmapData
= new BitmapData(100, 80, false, 0x00CCCCCC);

var mc:MovieClip
= this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());

myBitmapData.fillRect(new Rectangle(0, 0, 50, 80), 0x00FF0000);

mc.onPress = function() {
myBitmapData.threshold(myBitmapData, new Rectangle(0, 0, 100, 40),
new Point(0, 0), ">=", 0x00CCCCCC, 0x000000FF, 0x00FF0000, false);