Something I've been meaning to do for a while now is port our ColorMatrix class to AS3. It's a fairly simple conversion, but is still a handy utility to have on hand.
If you haven't used the ColorMatrix class before, here is the description from when we first released it in 2005:
ColorMatrix provides a way to adjust Brightness, Contrast, Saturation and Hue based on a range of numeric values as well as multiply matrices. The ColorMatrix can then be passed into ColorMatrixFilter to apply color adjustments. The added bonus of ColorMatrix is that it uses the same calculations to generate matrix values as the Flash 8 IDE (with the exception of contrast adjustment which uses linear interpolation to provide a bit more granularity).
Download the updated source here
Here is a quick sample of it in use. This demo is included in the download package.

Comments (27)
This class is absolutely brilliant - I've been needing a class like this for a long time. Good work!
Posted by: Dark Vyper at December 12, 2007 03:33 PMURL: http://www.fatal-exception.co.uk
Sliders would be better for demo purposes ;)
Posted by: Valters Boze at December 13, 2007 01:13 AMThanks for sharing!
URL: http://blog.valtersboze.com
Good work! This class is perfect. I've been needing a class like this.
Posted by: Maksus at December 13, 2007 02:07 AMURL: http://max.kr.ua
Great work!
Has anyone ever attempted to translate Flash color matrices to other languages?
Specifically I've got a project where we're allowing a User to modify an image within Flash but would like to accurately translate the final effect to ImageMagick for export.
Posted by: Ferris at December 13, 2007 07:19 AMURL:
This is Brad from the CN talk you gave in Atlanta. Matrix manipulation confounds me so this is really helpful. Thanks for posting it.
Posted by: brad at December 14, 2007 08:46 AMURL: http://www.bulletpattern.com
Ferris,
It should be fairly easy to translate Flash Color matrices to other languages. The only issue is that Flash uses 4x5 matrices whereas most languages use 5x5 matrices. You'll note that the ColorMatrix class actually has to "pad" the matrix to 5x5 in order to carry out matrix multiplication.
Posted by: Grant Skinner at December 17, 2007 10:14 AMURL: http://gskinner.com/blog/
Great - this class is perfect. Thanks - I'll be back soon, bye !
Posted by: Mielno at December 28, 2007 03:54 PMURL: http://www.jacek.ta.pl
Using the adjustColor class is removing any filters I had set with AS (but not the ones set on the stage) and vice versa if I re-apply the filter it strips off the color effects...any suggestions for getting around this?
Posted by: Josh at January 18, 2008 10:12 PMURL:
Oops, disregard the part about it not affecting some filters...those were unaffected becuase they were on a nested object...but any time they are applied to the same object the new effect removes the previous one.
Posted by: Josh at January 18, 2008 10:19 PMURL:
Super travail, merci !!!!
Posted by: Cyril at February 25, 2008 01:30 PMURL:
line 80 x = 127+p_val/100*127
expected ; at end of line
Posted by: ladon at June 4, 2008 10:00 PMURL:
very nice and thanks for sharing.
Posted by: S.Ali at July 6, 2008 03:00 PMURL: http://kabulinteractive.com/blog/
Simple and useful. Thanks.
Posted by: Anna at July 28, 2008 09:10 AMURL:
Is there some magic implementation of the ColorMatrix that would handle colorization?
Like: Every paint programs hue/saturation filter panel has that 'colorize' check box that seems to inject a specific hue into grey scale values.
Being able to turn a white icon blue while maintaining gradients/antialiasing would be a handy tool to have in your back pocket.
Aaron
Posted by: Aaron at August 22, 2008 11:35 AMURL: http://www.wheelerstreet.com
Re: "Ferris,
It should be fairly easy to translate Flash Color matrices to other languages. The only issue is that Flash uses 4x5 matrices whereas most languages use 5x5 matrices. You'll note that the ColorMatrix class actually has to "pad" the matrix to 5x5 in order to carry out matrix multiplication."
-- Later versions of ImageMagick (6.3+ I think) have a -recolor option which in theory can take the same 5x5 matrix (with the bottom row being padded with the identity row (0 0 0 0 1)). It does not seem to be able to handle a 5x5 matrix correctly though, but it will recognize a 3x3 matrix which is a subset of the 5x5:
eg: For 5x5 matrix:
[RR RG RB RA RO
GR GG GB GA GO
BR BB BB BA BO
AR AB AB AA AO]
You can input
[RR RG RB
GR GG GB
BR BG BB]
into the ImageMagick command line convert utility will yield the same color translation:
Eg: convert original.jpg -recolor '.3 .59 .11 .3 .59 .11 .3 .59 .11' greyscale.jpg
Note: All of the values in the 3x3 matrix must be positive numbers. For an inversion like '-.3 -.59 -.11 -.3 -.59 -.11 -.3 -.59 -.11' just use '.3 .59 .11 .3 .59 .11 .3 .59 .11' as the matrix but add the -negate flag to the command:
convert original.jpg -recolor '.3 .59 .11 .3 .59 .11 .3 .59 .11' -negate xray.jpg
Note: If anyone can figure out how to get the ImageMagick convert program to take the 5th column and/or 4th row values I would appreciate if you post the solution here.
Thanks,
Kevin
Posted by: Kevin at September 9, 2008 04:23 PMURL: http://www.sarolta.com/
Works like a charm! Thanks you saved me alot of time. :)
Posted by: Bryce at September 13, 2008 12:42 PMURL:
Thanks !
Posted by: Sam at September 20, 2008 09:58 AMURL: http://visualiser.fr
sweet, thanks a lot for this!
Posted by: Gabor at November 4, 2008 01:51 PMURL: http://www.flashguy.de
I was wondering if it is possible to change the color of an image. What I want to be able to do is send a function RGB colors and then have it color the image. Tint sort of does that but changes the background and everything inside of the image. I want it to function like Hue but be able to send an RGB rather then a Hue percentage.
Posted by: Jared Christensen at November 6, 2008 01:28 PMURL: http://jareddesign.com
I can ColorMatrix Class in AS3 example of the source code it?
Posted by: jialong at November 15, 2008 12:46 AMThanks!
URL:
Good work! Cheers :)
Posted by: Rich at December 11, 2008 04:06 AMURL: http://www.agecalculator.co.uk
Very simple and helping.
Posted by: aminusia at January 21, 2009 12:39 AMThanks!
URL:
Hi,
superb class, really needed something exactly like this. Very nice work.
Thanks
Posted by: Nick Polet at January 23, 2009 05:49 AMURL: http://www.spinee.com
Hmm... I found a BAD bug..
Zoom in several times. your color transform will break and go back to regular colors.
Posted by: Justin Reid at March 31, 2009 09:39 AMURL: http://moveanimation.deviantart.com
Hi, I'm a newbie to Flash but I really like this! I was wondering if anyone could help me understanding if I can add a .mid file or otherwise how can I make it work...Should I take from the zip file some .as file and work on that?What should I do to reseize the equalizer and add a mp3?I know it's basic, but please, help with that, I'd appreciate it!
Posted by: Tania at May 5, 2009 03:03 AMURL:
Totaly awesome class! Thanks a bunch!
Posted by: Bill at October 23, 2009 09:50 AMURL: http://www.wuup.co.uk/
Hi Grant,
Thanks for writing and sharing the ColorMatrix class, it is very useful. I have used it in a Flash based image editor.
My client has now asked me if they can have Levels (like there is in PhotoShop). Have you ever seen this done? Any thoughts on how I should go about it?
Cheers,
Adrian
Posted by: Adrian Parr at January 28, 2010 03:18 AMURL: http://www.adrianparr.com