sorrex
sorrex

Reputation: 55

Color matrix to change dark color

I try to make a function to colorize image - desaturate and replace dark color with supplied RGB color.

I have found similar function from Mario Klingemann AS3 ColorMatrix library, which replace light color while preserving dark.

public function colorize(rgb:int, amount:Number=1):void
    {
        const LUMA_R:Number = 0.212671;
        const LUMA_G:Number = 0.71516;
        const LUMA_B:Number = 0.072169;

        var r:Number;
        var g:Number;
        var b:Number;
        var inv_amount:Number;

        r = (((rgb >> 16) & 0xFF) / 0xFF);
        g = (((rgb >> 8) & 0xFF) / 0xFF);
        b = ((rgb & 0xFF) / 0xFF);
        inv_amount = (1 - amount);

        concat([(inv_amount + ((amount * r) * LUMA_R)), ((amount * r) * LUMA_G), ((amount * r) * LUMA_B), 0, 0, 
                ((amount * g) * LUMA_R), (inv_amount + ((amount * g) * LUMA_G)), ((amount * g) * LUMA_B), 0, 0, 
                ((amount * b) * LUMA_R), ((amount * b) * LUMA_G), (inv_amount + ((amount * b) * LUMA_B)), 0, 0, 
                0, 0, 0, 1, 0]);
    }

I tried to modify the matrix but with no success. Please could you provide me any help or link to information or code that make me move forward a bit.

Upvotes: 0

Views: 312

Answers (2)

sorrex
sorrex

Reputation: 55

After all I found the trick in this result:

            concat([(LUMA_R), (LUMA_G), (LUMA_B), 0, amount*255*r, 
                (LUMA_R), (LUMA_G), (LUMA_B), 0, amount*255*g, 
                (LUMA_R), (LUMA_G), (LUMA_B), 0, amount*255*b, 
                0, 0, 0, 1, 0]);

It does exactly what I needed. Thanks to Quasimondo for his great AS3 library. Helped me a lot in understanding the color matrix transformations.

Upvotes: 0

Quasimondo
Quasimondo

Reputation: 2545

You could try to use the method above, but before you apply it you invert the image and the color. Then after applying the matrix, you invert the image again.

Upvotes: 1

Related Questions