July 09, 2008
ColorMatrix Class for AS3

NOTICE: you will find the latest version of ColorMatrix in the Quasimondo Libs repository on Google code.

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

great work. your library is a must

I have some problem with the colorize method. it actually changes the luminance.
I'm working on a painting program that would tint the image where you pass the brush. but it becomes darker after a few passes because the luminance is not correct.

maybe I did something wrong or there's a workaround =)

well, thanks anyway

Posted by: mxx on October 30, 2008 12:42 PM

The colorize method is not supposed to keep the luminance of a pixel. I even think that doing what you want to achieve with a single colormatrix is not possible since you'd have to read every single pixel, convert it to HSL and then only replace the H and the S component.

Posted by: Mario Klingemann on October 30, 2008 01:07 PM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?



Thank you!

Most Visited Entries
Sketches, Works & Source Code
Lectures
Contact
Backlog
In Love with
Powered by
Movable Type 2.661

© Copyright Mario Klingemann

Syndicate this site:
RSS 1.0 - RSS 2.0

Quasimondo @ flickr
Quasimondo @ LinkedIn
Quasimondo @ Twitter
Quasimondo @ Facebook
Quasimondo @ MySpace
Quasimondo is a Bright
Citizen of the TRansnational Republic
My other blog in german
Impressum


My family name is written Klingemann,
not Klingelmann, Klingeman, Klingaman, Kingemann,
Kindermann, Killingaman, Klingman, Klingmann, Klingonman
Klingemman, Cleangerman, Klingerman or Kleangerman

profile for Quasimondo at Stack Overflow, Q&A for professional and enthusiast programmers