ImageMagick's composite on HSL (not HSB nor HSV)

858 Views Asked by At

Just I want to do is to replace Photoshop's HSL-based blend modes (color/hue/saturation/luminosity) by writing a CUI tool. Better if I can do it via RMagick.

ImageMagick can manage HSL colorspace, but ImageMagick's composite operators Colorize/Hue/Saturation/Luminize are hard-coded to be based on HSB colorspace.

Is there any workaround without writing pixel-by-pixel processing code? Thanks.

1

There are 1 best solutions below

0
On

I tried the separate-and-combine approach. Then a story has begun.

ImageMagick-6.6.9-7 has a pinpointing bug with rgb<->hsl calculation. Ubuntu 12.04 LTS's package repository provides it... grrrr (ImageMagick itself, fixed at r4431 and good with >= 6.6.9-9)

Then I sit down and do the math, to obtain a simple -fx expression.

colorize_hsl.fx:

ul = u.lightness; vl = v.lightness;
bias = (ul < .5 ? ul : 1 - ul)/(vl < .5 ? vl : 1 - vl);
(v - vl)*bias + ul

That is an rgb-based formula to set new lightness and preserve its hue and saturation. To get luminize_hsl, exchange u and v.

Temporary vars (ul, vl and bias) are common in all channels, but -fx engine might try it 3 times. It's not enough...