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
nice work Mario! Always wanted to know how colorblind people see images... thanks a lot !
Very Nice! I can't to try this out! I really think the applyColorDeficiency() method could be promising.
Looking good Mario.
Will be making use of your class....
thnx!
Keep up the good work!
:) I re-wrote this last week for our use.
Can you send me a mail bud?
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?
Strange - which tool gives you that error? I'm using Flex Builder and it does neither give me errors nor warnings with this class.
That's strange. FDT 3 gave the error.
Ah okay - might be that FDT is too strict here. Maybe Nico should have a look at it.
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]);
}
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.
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.
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
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.