topical media & game development
#graphic-flex-image-effects-04-Flex-ThresholdTest.ax
#graphic-flex-image-effects-04-Flex-ThresholdTest.ax
[swf]
[flash]
flex
package {
import flash.display.BitmapData;
import flash.filters.BevelFilter;
import flash.filters.BitmapFilter;
import flash.filters.BlurFilter;
import flash.filters.ColorMatrixFilter;
import flash.geom.Point;
[SWF(width=300, height=300, backgroundColor=0xFFEFEE)]
Demonstrates use of BitmapData's threshold() to isolate a color or brightness value. This example
uses threshold() to copy all pixels under a certain brightness into a new transparent bitmap,
then applies a filter to the resulting image.
public class @ax-graphic-flex-image-effects-04-Flex-ThresholdTest extends graphic_flex_image_effects_04_Flex_AbstractImageLoader {
private static const THRESHOLD:uint = 0xFF660000;
private static const PAINT_COLOR:uint = 0xFF883300;
Constructor. Passes path of image to load to super class.
public function @ax-graphic-flex-image-effects-04-Flex-ThresholdTest() {
super("graphic-flex-image-effects-04-assets-goat.jpg");
}
Run after the image loads in super class. This creates a new transparent bitmap data and copies over pixels
under a certain brightness into it. Then two filters applied to produce a pooling paint effect.
override protected function runPostImageLoad():void {
var bitmapData:BitmapData = _loadedBitmap.bitmapData;
// new data should be transparent by default
var copiedData:BitmapData = new BitmapData(bitmapData.width, bitmapData.height, true, 0x00000000);
// make loaded data grayscale
desaturate(bitmapData);
// copy in all pixels under a certain brightness from grayscale image into transparent image;
// we only need to look at one channel since in a grayscale image all channels hold the same data
copiedData.threshold(
bitmapData,
bitmapData.rect,
new Point(),
"<",
THRESHOLD,
PAINT_COLOR,
0xFFFF0000,
false
);
// apply a blur and bevel for the final effect
applyFilter(copiedData, new BlurFilter(1.2, 1.2));
applyFilter(copiedData, new BevelFilter());
// copy in new data
_loadedBitmap.bitmapData = copiedData;
addChild(_loadedBitmap);
bitmapData.dispose();
}
Applies the specified filter to the bitmap data.
parameter: bitmapData The data to which to apply the filter.
parameter: filter The bitmap filter to apply to the data.
private function applyFilter(bitmapData:BitmapData, filter:BitmapFilter):void {
bitmapData.applyFilter(
bitmapData,
bitmapData.rect,
new Point(),
filter
);
}
Applies a ColorMatrixFilter to desaturate the image.
parameter: bitmapData The data to which to apply the filter.
private function desaturate(bitmapData:BitmapData):void {
var matrix:Array = [
0.3, 0.59, 0.11, 0, 0,
0.3, 0.59, 0.11, 0, 0,
0.3, 0.59, 0.11, 0, 0,
0, 0, 0, 1, 0
];
applyFilter(bitmapData, new ColorMatrixFilter(matrix));
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.