MovieClip Saturation Class – ActionScript 2

This ActionScript 2.0 Class enables you to easily adjust the colour saturation of any Flash MovieClip during runtime. It requires Flash Player version 8 or above, and uses the ColourTransformationMatrix and some clever jiggery-pokery to adjust the RGBA channels of your MovieClips. In short, it’s sweet – and fills a gap in the built-in flash filter classes.

Have a look at this flash movie (below) to see the end result, all generated from the same MovieClip.

Now, before I go much further, I should point out that the functions that make this possible are down to senocular, whose equally awesome code this is based on. All I really did was figure out a way to turn it into an ActionScript 2.0 Class. – Technically it doesn’t even need to be in a class – but it just makes things easier.

Using this Class

Firstly, you’re going to want to download the source files. It’s as basic as it comes – using it ‘out-the-box’ requires that you take the ‘mmt’ folder, and paste it into the same directory as your .fla file. And then, by typing;

mmt.SaturationManager.setSaturation(your_movieclip, the_saturation);

You’ll be able to modify the colour saturation of the MovieClip. “your_movieclip” is obviously a reference to the MovieClip, and “the_saturation” is a value from 0 (No Colour) to 100 (Normal Colour). You can apply values beyond 0 and 100, and oversaturate the colours. In our example (above) the [1st on the left, bottom row] MovieClip is oversaturated, at 150%.

Downloads

Download the source files, containing example .fla and .as files [ZIP, 488KB]
Download only the .as file [ActionScript File, 1KB]

, , ,

26 Responses to “MovieClip Saturation Class – ActionScript 2”

  1. Steven Archer 6th October, 2007 at 3:00 pm # Reply

    Thanks very usefull.

  2. Jadon 27th December, 2007 at 1:02 am # Reply

    Thanks mate.
    Came in handy :)

  3. Wout 8th February, 2008 at 10:15 pm # Reply

    Thanks, I was looking for some saturation formulas to program one myself, but since I’m on a tight schedule for the moment, this class comes in more than handy!

    Great job!

  4. Tim 25th February, 2008 at 12:11 pm # Reply

    You legend – this worked perfectly. I Googled a lot on the matter and this is by far the best example. Thanks again and keep up the good work!

  5. Martin Bartels 28th February, 2008 at 4:20 pm # Reply

    Is it possible to use your class when I first load an external jpg file into a movieclip and then try to change the saturation?

  6. James 29th February, 2008 at 9:20 am # Reply

    @Martin – it should be fine, you can apply the saturation changes at any point in your timeline / code.

  7. Sniffer 1st March, 2008 at 8:17 pm # Reply

    I have a jpg into a movie, with 26602 pixels wide and 100 pixels tall (719Kb in disk).
    And it doesn’t work.
    Do you know something about size limitations (file size and/or image size)?
    Thank you.

    PS.: I’ve tested with normal images and it worked just fine.

  8. Sniffer 1st March, 2008 at 8:31 pm # Reply

    Duh! I’ve found: there is a 2880 pixels limit.
    Thanks anyway.

  9. Up to my ears in Flash 6th May, 2008 at 8:05 pm # Reply

    a million thank yous for such a great design and
    such ease of implementation.
    more more!

  10. C-Check 22nd May, 2008 at 10:32 pm # Reply

    I’m using your class for an e-vite I’m making. It’s a great idea, but I’m worried that the setup of the class is causing an error with a movieClip that’s in motion; I have a movieClip containing an image and an AS function which creates an onEnterFrame to desaturate the image over the course of 30 frames or so. On the main timeline, this movieClip is sliding from right to left with a tween. If I call the function in the movieClip to begin desaturation, the saturation class works, but the movieClip becomes stationary, ignoring the tween. I assume the problem is that a tween can’t access an mC that’s being acted on by the saturation class (because it can act on the mC if the onEnterFrame doesn’t actually call the saturation class but merely traces numbers, or something). Am I missing something obvious or can you think of an easy fix? It’s been a long day and I might just have missed something or not understand something…

  11. James 23rd May, 2008 at 8:26 am # Reply

    @C-Check – That’s an interesting one, and one I haven’t come across before. I would imagine that the main issue is coming from the combination of Timeline and Actionscript effects, although that’s just a supposition.

    Actually, a Google on ColorMatrixFilter (the basis for this class) confirms that there might be a few issues with Tweening this filter altogether.

    Have you tried applying the class on a Keyframe-by-keyframe basis, rather than scripting it? I know it’s not pretty, but it might work!

    I’ll have a look at improving the class in the meantime, but I make no promises about timescales…

  12. eerkmans 28th May, 2008 at 3:39 pm # Reply

    Just what I needed! thanks!

  13. Umar 17th June, 2008 at 9:54 am # Reply

    guys this is really useful post but can anyone please email me how to use this if i want to use saturation for a movie clip and it should change the color with tween, i will be really very much thankful

  14. Brad 2nd July, 2008 at 7:20 pm # Reply

    C-Check – I ran into the same issue when trying to use an onEnterFrame to decrease saturation while using a frame-based tween, so I decided to use the same onEnterFrame to move the movieclip, (as well as desaturate) and it worked like a charm.

    Example:

    var saturation:Number = 100;
    function decreaseSaturation()
    {
    test_mc.onEnterFrame = function()
    {
    if( saturation > 0 )
    {
    saturation -= 2;
    }
    else
    {
    saturation = 0;
    }
    setSaturation(this, saturation)
    this._x += ( 400 – this._x ) *.05;
    this._xscale += ( 50 – this._xscale ) *.2;
    this._yscale += ( 50 – this._yscale ) *.2;
    }
    }

  15. taylor 19th December, 2008 at 5:48 pm # Reply

    Is there any way to import this class from the Classes folder and call it with a function? That way I can use it without having to always copy the class to my current project folder.

    ex.:
    import com.mmt.*;

    SaturationManager.setSaturation(graphic, 15);

  16. James 19th December, 2008 at 9:21 pm # Reply

    @taylor – you can always set class library paths in the Flash IDE, through the preferences screen. The default preferences map to the same location as the .fla file, but you can specifiy new locations through Edit > Preferences > Actionscript > Actionscript settings

  17. mincartoon 2nd March, 2009 at 2:23 pm # Reply

    Thanks man it’s so useful!!! :)

  18. Lartar 31st July, 2009 at 11:42 pm # Reply

    is there a way to make the saturation script work on mc more than 2880 of width??? I love this script cause i try to make one before without no luck…

    If there a way to make that please i will love the help… thanks…

  19. James 3rd August, 2009 at 9:13 pm # Reply

    Lartar – I don’t think you can get this to work with images over 2880 width – that’s the technical limit of the Flash Player pre-version 9. To get around that limit you’d need to build your code for Flash Player 10 and in Actionscript 3.

    The alternative is building a clever bitmap tiling solution.

  20. Lartar 3rd August, 2009 at 10:23 pm # Reply

    Thanks anyway, i make the class working with a very nice script, but the cpu work is too hard on my project so i make a different effect to make it done.

    Very nice class :)

    Thanks

  21. Frequency650 4th September, 2009 at 3:07 pm # Reply

    Great tool!…even at 2 years old.

  22. Seth Taylor 29th September, 2009 at 11:36 pm # Reply

    Thanks. This helped a lot in the flash project of desaturating a bunch of logos.

  23. mikod 3rd February, 2010 at 3:01 pm # Reply

    This saved sooooooo much time, Thanx thanx thanx

  24. Falcon 4th May, 2010 at 12:40 pm # Reply

    Thanks..I used your great script with my video player..

  25. Waldorf 1st January, 2011 at 7:25 am # Reply

    Awesome!!! Direct and straight forward!!

  26. Dante 19th October, 2011 at 7:09 am # Reply

    Awesome i love it, thank you very much!

Leave a Reply

Note: This post is over 4 years old. You may want to check later in this blog to see if there is new information relevant to your comment.