Luminize, Colorize, HCL

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Luminize, Colorize, HCL

Post by GreenKoopa » 2013-07-22T22:10:02-07:00

Photoshop has the layer blend modes Luminosity and Color. Does anyone know if these are IM's Luminize and Colorize compose methods, or what colorspace Luminize and Colorize work in (HSB/HSV, HSL, HSLuma)?

EDIT: subject
Last edited by GreenKoopa on 2013-07-23T09:15:56-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: layer compose methods

Post by fmw42 » 2013-07-22T23:04:12-07:00

They are supposed to be the same operations, but I do not believe the colorspaces are the same. -compose colorize does not seem to work the same as in PS and does not seem to match doing it manually with changing colorspaces and swapping channels.

IM has a HCL which is close but not the same as PS HCL. But I think IM still uses HSL for -compose colorize and there does not seem to be a define to allow one to choose the colorspace.

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Luminize, Colorize, HCL

Post by GreenKoopa » 2013-07-23T09:28:37-07:00

The documentation for Luminize compose is unclear. Hopefully I can research, experiment, and stumble my way to some answers. My hope is that such a compose mode (or color space?) could replace some of my uses of HSB/HSV, HSL, and Lab. Please correct me where I am wrong.

Photoshop's Luminosity and Color blend modes work in a HSL-like color space (HCL?). H is as usual (HSB/HSV, HSL). L is Rec601Luma, which has obvious perceptual benefits over MAX or (MAX+MIN)/2. S is based on chroma (C=MAX-MIN) like usual, but I am unclear how the stretching and clipping is done.

Luminosity compose is not commutative, meaning the layer order is important. Color compose is simply a swapping of the layers (like Lighten vs Darken or Overlay vs Hard Light).

It looks like -colorspace HCL is related. How is it different from HCLp? Does anyone know the formula for the C channel? It is not in the documentation with the other colorspaces.

This test shows what to expect and that IM's Luminize and Colorize are the same thing as PS. Note that I did not test the S/C channel or a case of clipping because I don't know what to expect or have PS to compare results.

Code: Select all

: create background and overlay test layers
convert -size 640x640 ^
xc: -sparse-color Bilinear "0,0 #000 640,0 #880 0,640 #080 640,640 #808" ^
xc:#999 ^
in.png

: Luminize composite the layers
convert in-0.png in-1.png -compose Luminize -composite out.png

: test the output
convert out.png -colorspace Rec601Luma out-Luma.png
compare -metric PAE in-1.png out-Luma.png null:
convert in-0.png -colorspace HSL -channel R -separate in-0-Hue.png
convert out.png  -colorspace HSL -channel R -separate out-Hue.png
compare -metric PAE in-0-Hue.png out-Hue.png null:

Output:
1 (1.5259e-005)
1 (1.5259e-005)

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Luminize, Colorize, HCL

Post by fmw42 » 2013-07-23T09:40:13-07:00

It looks like -colorspace HCL is related. How is it different from HCLp? Does anyone know the formula for the C channel? It is not in the documentation with the other colorspaces.
You can look at the code from the source download. Look in gems.c (also composite.c) in the magick folder

Also see http://en.wikipedia.org/wiki/HSL_and_HSV

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Luminize, Colorize, HCL

Post by GreenKoopa » 2013-07-23T10:33:00-07:00

Thank you Fred. You always know where to read next.

C is simply chroma (MAX-MIN). HSB/HSV and HSL stretch chroma into saturation to fill the interval to create a cylinder. HCL does not stretch C to fill the interval. This means less distortion, but also leaves invalid values in the HCL space. RGB to HCL is safe, so HCL and HCLp are the same in this direction. HCL to RGB causes clipping of these invalid values, and HCL and HCLp handle this differently.

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Luminize, Colorize, HCL

Post by GreenKoopa » 2013-07-23T12:44:49-07:00

HCL to RGB
Invalidly large values of C are ignored. This results in RGB values out of range in both directions. These are clamped.

HCLp to RGB
When RGB values are out of range, they are scaled in such a way that C is clamped so that H and L may be preserved. Very clever!

-compose Luminize uses HCL. I have read that PS's Luminosity uses HCLp. It is also mentioned in this topic. The changelog states
2012-07-31 6.7.8-8 Support HCL colorspace.
2012-08-14 6.7.9-0 HCL -> RGB conversion enhancement
2012-11-10 6.8.0-5 Revert HCL saturation strategy patch, it improperly clips white to black.

Is HCLp broken? That would explain why two HCLs exist.

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Luminize, Colorize, HCL

Post by GreenKoopa » 2013-07-23T13:42:09-07:00

I did a round trip test for HCLp using a hald.

RGB -> HCLp -> RGB
Worked great!

HCLp -> RGB -> HCLp
L is preserved.
H is preserved correctly. When hue is undefined (gray, when C=0), it is reset to 0. When L is 0 or QuantumRange, C is clamped to 0, which puts us in the previous case. H is circular, so #FFFF may become #0000.

I discovered this colorspace only yesterday, but I love it already. HCL is a confusing name though!

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Luminize, Colorize, HCL

Post by fmw42 » 2013-07-23T14:29:37-07:00

HCLp is a modification of HCL to try to match Photoshop. Not exactly the same, though, but closer.

see viewtopic.php?f=2&t=21646

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Luminize, Colorize, HCL

Post by fmw42 » 2013-07-23T14:31:59-07:00

Also see http://www.imagemagick.org/Usage/color_ ... colorwheel for Anthony's test images to show what is going on in various colorspaces. It is not complete yet, but you can use the same images with any colorspace to generate a color wheel. I did that some time ago, myself.

Code: Select all

convert angular.png solid.png radial.png -set colorspace RGB \
-combine -set colorspace HSL -colorspace RGB \
-set colorspace sRGB colorwheel_HSL.png

convert angular.png solid.png radial.png -set colorspace RGB \
-combine -set colorspace HSB -colorspace RGB \
-set colorspace sRGB colorwheel_HSB.png

convert angular.png solid.png radial.png -set colorspace RGB \
-combine -set colorspace HSV -colorspace RGB \
-set colorspace sRGB colorwheel_HSV.png

convert angular.png solid.png radial.png -set colorspace RGB \
-combine -set colorspace HSI -colorspace RGB \
-set colorspace sRGB colorwheel_HSI.png

convert angular.png solid.png radial.png -set colorspace RGB \
-combine -set colorspace HCL -colorspace RGB \
-set colorspace sRGB colorwheel_HCL.png

convert angular.png solid.png radial.png -set colorspace RGB \
-combine -set colorspace HCLp -colorspace RGB \
-set colorspace sRGB colorwheel_HCLp.png

convert angular.png solid.png radial.png -set colorspace RGB \
-combine -set colorspace HCLab -colorspace RGB \
-set colorspace sRGB colorwheel_HCLab.png

convert angular.png solid.png radial.png -set colorspace RGB \
-combine -set colorspace HCLuv -colorspace RGB \
-set colorspace sRGB colorwheel_HCLuv.png

convert radial.png solid.png angular.png -set colorspace RGB \
-combine -set colorspace LCH -colorspace RGB \
-set colorspace sRGB colorwheel_LCH.png

convert radial.png solid.png angular.png -set colorspace RGB \
-combine -set colorspace LCHab -colorspace RGB \
-set colorspace sRGB colorwheel_LCHab.png

convert radial.png solid.png angular.png -set colorspace RGB \
-combine -set colorspace LCHuv -colorspace RGB \
-set colorspace sRGB colorwheel_LCHuv.png

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Luminize, Colorize, HCL

Post by fmw42 » 2013-07-23T15:09:44-07:00

Here are some tests I did to compare to Photoshop

Original:
Image

Photoshop black-red gradient colorization:
Image


IM Processing:
infile="sunsets-lightroom-split-toning_srgb.jpg"
inname=`convert "$infile" -format "%t" info:`

IM HCL colorize:
convert $infile -define modulate:colorspace=HCL -modulate 100,0,100 -set colorspace sRGB \
\( -clone 0 +level-colors black,red \) \
-compose colorize -composite \
${inname}_highlight_HCL_levelcolors_colorize.jpg
Image


IM HCLp colorize:
convert $infile -define modulate:colorspace=HCLp -modulate 100,0,100 -set colorspace sRGB \
\( -clone 0 +level-colors black,red \) \
-compose colorize -composite \
${inname}_highlight_HCLp_levelcolors_colorize.jpg
Image

But using -define modulate: only changes a little. IM -colorize still works in HCL (or possibly HSL) and is not affected by the -define and so does not maintain the brighter whites. It needs to have a -define so that one can choose which colorspace to use or at least use HCLp. Though that still does not guarantee that it will match perfectly to PS as we do not know exactly what colorspace they use.

Here is another approach that tries to do it manually:


convert $infile \( -clone 0 +level-colors black,red -set colorspace RGB -colorspace HCLp -separate \) \
\( -clone 0 -set colorspace RGB -colorspace HCLp -separate \) \
-delete 0,3,4,5 -set colorspace HCLp -combine -colorspace RGB -set colorspace sRGB \
${inname}_highlight_HCLp_levelcolors_colorize_non-linear_linear.jpg
Image


infile="sunsets-lightroom-split-toning_srgb.jpg"
convert $infile \( -clone 0 +level-colors black,red -set colorspace RGB -colorspace HCLp -separate \) \
\( -clone 0 -set colorspace RGB -colorspace HCLp -separate \) \
-delete 0,3,4,5 -set colorspace HCLp -combine -colorspace sRGB \
${inname}_highlight_HCLp_levelcolors_colorize_non-linear_linear2.jpg
Image

So I have never really been able to duplicate the Photoshop result.
Last edited by fmw42 on 2013-07-23T21:01:56-07:00, edited 2 times in total.

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Luminize, Colorize, HCL

Post by GreenKoopa » 2013-07-23T20:45:12-07:00

I cannot reproduce the white turns black using HCL (or HCLp) problem mentioned in that post and the changelog. It must have been fixed.

Fred, what steps did you do in PS to produce that image?

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Luminize, Colorize, HCL

Post by fmw42 » 2013-07-23T20:57:22-07:00

Fred, what steps did you do in PS to produce that image?
Open PS (my very old PS CS)
Load the image maintaining sRGB colorspace
Select New Adjustment Layer -- Gradient Map
Click on grayscale gradient and change the white to red
Set layer blending mode to Color
Save the image as PNG

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Luminize, Colorize, HCL

Post by GreenKoopa » 2013-07-23T21:14:27-07:00

I guess I don't know enough about PS to know what a Gradient Map is. Is it identical to +level-colors?

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Luminize, Colorize, HCL

Post by fmw42 » 2013-07-23T21:16:32-07:00

GreenKoopa wrote:I guess I don't know enough about PS to know what a Gradient Map is. Is it identical to +level-colors?
That was my thought when I tried to recreate it above using +level-colors. But it does not match exactly. I do not know if PS does something different or IM is not using the exact same colorspace as PS.

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Luminize, Colorize, HCL

Post by fmw42 » 2013-07-23T21:31:56-07:00

This seems a bit closer without using linear RGB.


infile="sunsets-lightroom-split-toning_srgb.jpg"
inname=`convert $infile -format "%t" info:`

convert $infile \( -clone 0 +level-colors black,red -set colorspace RGB -colorspace HCLp -separate \) \
\( -clone 0 -colorspace HCLp -separate \) \
-delete 0,3,4,5 -set colorspace HCLp -combine -colorspace sRGB \
${inname}_highlight_HCLp_levelcolors_colorize_non-linear.jpg
Image

Photoshop:
Image

Post Reply