1

Hi I would like to replace a specific color in a bitmap with another one. The BitmapData.threshold() function works fine if the threshold color value has no alpha.

var threshold:uint = 0xFF004d5e; //  this works, however if I use this  0x37004d5e, ie the same color with an alpha then it doesnt
var baseColour:uint = 0x3700c5e0;
var targetColour:uint = 0x37F38900;
var inputBitmapData:BitmapData = new BitmapData(200, 200, true, baseColour); 
inputBitmapData.fillRect(new Rectangle(10, 10, 180, 180), threshold); 
var input:Bitmap = new Bitmap(inputBitmapData); 
addChild(input); 

replaceColour(inputBitmapData,threshold,targetColour); 
function replaceColour(bitmapData:BitmapData,colorToReplace:uint, newColor:uint):void 
{ 
trace(" replaced:"+ bitmapData.threshold(bitmapData, new Rectangle(0, 0, bitmapData.width, bitmapData.height), new Point(), "==", colorToReplace, newColor, 0xFFFFFFFF)); 
}

The above works, unless you are trying to change a color that has an alpha, then it doesnt. ie change the threshold variable's value from 0xFF004d5e to 0x37004d5e, ie to change a color which has an alpha channel, and then it wont work, but that is what I want! Anyone has any ideas, how to make the BitmapData.threshold() function work when the threshold color has an alpha??

Martin K
  • 765
  • 1
  • 6
  • 15
  • Have you tried setting mask to 0x00FFFFFF rather than 0xFFFFFFFF? –  Feb 15 '13 at 14:41
  • Yes I have tried, and it makes no difference. In theory it would be wrong anyway to look for the rgb component only, as I want to target a specific color **with a specific alpha**, ie If we were to ignore the alpha channel via the mask, then that would mean that it would find&replace colors with different alphas(but same rgb channels) which I wouldnt't want. **But that is just theory, in practice even that doesn't work.** BTW you can try the code I've pasted above, all you need is to paste it onto a new project and compile it to see if your suggestions would work. Thanks! – Martin K Feb 15 '13 at 14:54
  • That is very strange. With `threshold = 0x37004d5e`, tracing `inputBitmapData.getPixel32(10, 10).toString(16)` gives `37004f5d`, as if some precision is being lost. But even changing threshold so that the value is exactly the same doesn't work. An alpha of FE *does* work, but FD doesn't. – David Mear Feb 15 '13 at 20:36
  • as an alternative solution, have you considered creating a pixel bender filter? –  Feb 16 '13 at 02:20

0 Answers0