# Creating a chroma key effect in a bitmap

**URL:** <https://forum.kirupa.com/t/creating-a-chroma-key-effect-in-a-bitmap/285328>\
**Category:** Uncategorized\
**Created:** [March 30, 2009, 3:20pm UTC](https://forum.kirupa.com/t/creating-a-chroma-key-effect-in-a-bitmap/285328 "2009-03-30T15:20:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mammy](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@mammy](https://forum.kirupa.com/u/mammy)\
**Post date:** [March 30, 2009, 3:20pm UTC](https://forum.kirupa.com/t/creating-a-chroma-key-effect-in-a-bitmap/285328/1 "2009-03-30T15:20:24Z")

</div>

hello all,  
i’m trying to create a chroma key effect in a bitmap to have an irregular shape bitmap appear on any background.

my sense is that the best way to do this is with BitmapData.threshold(). chime in if there’s a better way perhaps with DisplayObject.BlendMode.ALPHA or a Bitmap filter.

the below code gets me 95% of the way there but the problem with it is that i can’t get the threshold to be just one pixel color rather than a range. so the problem with the below code is that some of the anti aliased pixels around the outside of the image don’t get keyed out since they’re close to the chroma key color. how to isolate the .threshold() to just the one chroma key color?

// code  
var loadedImage:Bitmap = //load 50x50 bitmap;  
var bmd\_orig:BitmapData = loadedImage.bitmapData.clone();  
var bmd\_keyed:BitmapData = new BitmapData(50, 50, true, 0xFFCCCCCC);  
var pt:Point = new Point(0, 0);  
var rect:Rectangle = new Rectangle(0, 0, 50, 50);  
var threshold:uint = 0x00800000;  
var color:uint = 0x00FF0000;  
var maskColor:uint = 0x00FF0000;  
bmd\_keyed.threshold(bmd\_orig, rect, pt, “\>”, threshold, color, maskColor, true); // operation probably should be “==” to the one pixel value  
var bitmap\_keyed:Bitmap = new Bitmap(bmd\_keyed);  
addChild(bitmap\_keyed);

thx  
michael
