views:

216

answers:

1

Is there a way to apply a colorTransform to a BitmapData in a circle rather than in a rectangle?

Instead of erasing rectangular parts of an image by reducing the alpha channel as in the code below, I'd like to do it in circles.

 _bitmap.colorTransform(new Rectangle(mouseX-d/2, mouseY-d/2, d, d),
 new ColorTransform(1, 1, 1, .5, 0, 0, 0, 1));

I do have some code which loops through the pixels, extracts the alpha value and uses setPixel but it seams significantly slower than the colorTransform function.

+1  A: 

Try creating a circle using the drawing API (flash.display.Graphics) and then drawing that onto the bitmap data with BlendMode.ERASE. That might solve your problem, if I understand it correctly.

var circle : Shape = new Shape;
circle.graphics.beginFill(0xffcc00, 1);
circle.graphics.drawEllipse(-50, -50, 100, 100);

// Create a transformation matrix for the draw() operation, with
// a translation matching the mouse position.
var mtx : Matrix = new Matrix();
mtx.translate(mouseX, mouseY);

// Draw circle at mouse position with the ERASE blend mode, to
// set affected pixels to alpha=0.
myBitmap.draw(circle, mtx, null, BlendMode.ERASE);

I'm not 100% sure that the ERASE blend mode works satisfyingly with the draw() command, but I can't see why it shouldn't. Please let me know how it works out!

richardolsson
Works a charm ! The only change I made was to set the fill of the circle to alpha .5 or less to achieve the same affect as the colorTransform I used. That is to erase the image more slowly. Many thanks.
Chris Porter
Another way to do this is to use the 3rd, ColorTransform parameter to the draw method _bitmap.draw(circle, mtx, new ColorTransform(1, 1, 1, .5), 0, 0, 0, 1), BlendMode.ERASE)
Chris Porter
I'm glad! With regards to using a ColorTransform just to decrease the alpha, my gut feeling tells me that it can't possible perform as well as simply having a < 1 alpha on the circle fill. Simply because the ColorTransform will be applied (and in your sample code, instantiated) each time you draw, which means one extra operation.
richardolsson
It might not perform as well but it looks slightly better in that you don't get the circle edges showing up as much. Thanks again for your help.
Chris Porter