« Cast to int issue
» toString() vs xmlToString()

ActionScript

Two Simple Ways to Invert the Colors of an Image

Andrei Ionescu | 23.01.08 | Comment?

The following code inverts the colors of an image and returns the inverted image as an Image object. As parameter it needs the container of the image (a canvas).

private function invertImageColor(canvas:Canvas):Image
{
        var newImage:Image = new Image();
        newImage.source = Image(
                canvas.getChildAt(canvas.numChildren-1)).source;
        newImage.blendMode = BlendMode.INVERT;
        return newImage;
}

Afterwards you can add it to what ever container you want.

Another approach is the following: “adding the new inverted image to the same container of the original image”.

private function invertImageColorAdded(canvas:Canvas):Image
{
    if (canvas.numChildren > 1) {
        canvas.removeChildAt(1);
        return canvas.getChildAt(0);
    } else {
        var newImage:Image = canvas.addChild(new Image()) as Image;
        newImage.source = Image(canvas.getChildAt(0)).source;
        newImage.blendMode = BlendMode.INVERT;
        return canvas.getChildAt(1)
    }
}

This also receives the canvas as parameter then checks to see if an inversion is already present and removing it if so.

This is it.

Share and Enjoy:
  • Technorati
  • StumbleUpon
  • del.icio.us
  • NewsVine
  • Reddit
  • Digg
  • Furl
  • co.mments
  • blogmarks
  • Slashdot
  • description
  • Taggly
  • YahooMyWeb
  • connotea
  • Webride




Tags: , ,

This post was written by Andrei Ionescu

Views: 1208

related

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

:

:


« Cast to int issue
» toString() vs xmlToString()