July 09, 2008
ColorMatrix Class for AS3

Okay - after I just received the nth email of someone who offered to convert my AS2 ColorMatrix class to AS3 I finally have to get out this post: Of course I have created an AS3 version of it a long time ago, I just didn't find the time to document it or create a demo for it.

Anyways in order to save all those helpful people out there any more unnecessary work here is the barebones com.quasimondo.geom.ColorMatrix Version 2.1 for you to download - it's released under MIT License.

Compared to the AS2 version I have added several new methods and of course tried to use optimizations wherever possible. Two new methods that are especially interesting are:

rotateHue() which tries to preserve the luminance whilst changing the hue (which means this is the "correct" but calculation wise more complicated way of doing it).

applyColorDeficiency() which I ported back from the Javascript version that the guys at NoFunc created from my Actionscript version and which allows to preview images the way colorblind people would see it.

Posted at July 09, 2008 05:57 PM | Further reading
Comments

nice work Mario! Always wanted to know how colorblind people see images... thanks a lot !

Posted by: koen De Weggheleire on July 9, 2008 06:14 PM

Very Nice! I can't to try this out! I really think the applyColorDeficiency() method could be promising.

Posted by: Brett Widmann on July 10, 2008 02:13 AM

Looking good Mario.
Will be making use of your class....
thnx!

Keep up the good work!

Posted by: Rackdoll on July 11, 2008 10:39 AM

:) I re-wrote this last week for our use.

Can you send me a mail bud?

Posted by: Tink on July 11, 2008 11:20 AM

Great work! I will add it to my favorite util classes. I just get four Errors in the setChannels method. All four errors says that i can not combine Number and Boolean or i can not use Boolean here.
Any idea how to solve the problem?

Posted by: Kim on July 18, 2008 05:32 PM

Strange - which tool gives you that error? I'm using Flex Builder and it does neither give me errors nor warnings with this class.

Posted by: Mario Klingemann on July 18, 2008 05:40 PM

That's strange. FDT 3 gave the error.

Posted by: Kim on July 24, 2008 04:07 PM

Ah okay - might be that FDT is too strict here. Maybe Nico should have a look at it.

Posted by: Mario Klingemann on July 24, 2008 04:15 PM

I could solve the problem when i set paranthesis around each conditional operators. I hope, it will not affect the operation:

public function setChannels( r : int = 1, g : int = 2, b : int = 4, a : int = 8) : void
{
var rf : Number = ((((((r & 1) == 1)) ? 1 : 0 + ((((r & 2) == 2)) ? 1 : 0)) + ((((r & 4) == 4)) ? 1 : 0)) + ((((r & 8) == 8)) ? 1 : 0));
if (rf > 0)
{
rf = (1 / rf);
}

var gf : Number = ((((((g & 1) == 1)) ? 1 : 0 + ((((g & 2) == 2)) ? 1 : 0)) + ((((g & 4) == 4)) ? 1 : 0)) + ((((g & 8) == 8)) ? 1 : 0));
if (gf > 0)
{
gf = (1 / gf);
}

var bf : Number = ((((((b & 1) == 1)) ? 1 : 0 + ((((b & 2) == 2)) ? 1 : 0)) + ((((b & 4) == 4)) ? 1 : 0)) + ((((b & 8) == 8)) ? 1 : 0));
if (bf > 0)
{
bf = (1 / bf);
}

var af : Number = ((((((a & 1) == 1)) ? 1 : 0 + ((((a & 2) == 2)) ? 1 : 0)) + ((((a & 4) == 4)) ? 1 : 0)) + ((((a & 8) == 8)) ? 1 : 0));
if (af > 0)
{
af = (1 / af);
}

concat([(((r & 1) == 1)) ? rf : 0, (((r & 2) == 2)) ? rf : 0, (((r & 4) == 4)) ? rf : 0, (((r & 8) == 8)) ? rf : 0, 0, (((g & 1) == 1)) ? gf : 0, (((g & 2) == 2)) ? gf : 0, (((g & 4) == 4)) ? gf : 0, (((g & 8) == 8)) ? gf : 0, 0, (((b & 1) == 1)) ? bf : 0, (((b & 2) == 2)) ? bf : 0, (((b & 4) == 4)) ? bf : 0, (((b & 8) == 8)) ? bf : 0, 0, (((a & 1) == 1)) ? af : 0, (((a & 2) == 2)) ? af : 0, (((a & 4) == 4)) ? af : 0, (((a & 8) == 8)) ? af : 0, 0]);
}

Posted by: Kim on July 24, 2008 05:12 PM

Ah thanks! I guess I should adjust my code as well. so it looks like this is a case of operator precedence - the question remains which tool has got it wrong. Since the code works fine without the paranthesis I guess it's FDT that needs to be adjusted.

Posted by: Mario Klingemann on July 24, 2008 07:00 PM

Thanks heaps! I used the old AS2 as a base for my own AS3 version, so it's nice to see a real PRO conversion here. Thanks again for all your hard work and sharingness.

Posted by: Tarwin Stroh-Spijer on August 4, 2008 06:22 AM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?



Most Visited Entries
Experiments
Popular on Aviary