I have an RGB pixel that I'd like to colorize in my program (I'm using GLSL) very similar to how the GIMP "Colors > Colorize" function works.

GIMP's Colorize

In my program, I can easily convert between RGB and HSV. I can therefore get:

pix_h, pix_s, pix_v

And I also define the inputs of the colorize function as:

col_h, col_s, col_v

What calculations can I perform to determine the output pixel hsv?

I think I can get:

out_h = col_h

But I'm a little lost on how to calculate output saturation and value.

EDIT: Where lum is the luminance of the input pixel:

out_s = clamp( (1 - pix_v) * 2, 0, 1)

out_v = clamp( pix_v * 2, 0, 1)

seems to produce good results when colorize inputs S=1 ([0, 1]) and V=.5 ([0, 1]). I'm still struggling to generalize these formulas for when S!=1 and V!=.5.

  • $\begingroup$ Have you tried out_s = col_s and out_v = in_v? $\endgroup$ – Sebastián Mestre Oct 7 '17 at 15:15
  • $\begingroup$ @SebastiánMestre Yes, this was my first thought. Unfortunately it does not replicate the Colorize behavior. $\endgroup$ – carlr Oct 7 '17 at 16:53
  • $\begingroup$ Isn't the colorize function kinda operating on the HSL space instead of HSV? What if you convert directly into HSL then what the above person said would work? $\endgroup$ – gallickgunner Oct 7 '17 at 23:04

It appears to me that in the image, the saturation of the original image is 0 since it is grayscale. (Unless I've misunderstood the question.) When the saturation is 0, the hue is undefined, but it appears that the GIMP is setting the hue to 0. It is then using the sliders from the dialog to modify the HSL. It adds 180° to the hue (so from 0 to 180 which gets you from red to cyan). The saturation is increased by 75 units, and the lightness is kept the same. It may have special protections for black and white, given that the white part of the letter "A" remains white in the final image (although even there, it appears to have an RGB value of (0.99, 1.0, 1.0), whereas on the right, it's fully white (1.0, 1.0, 1.0)).

You can always look at the source code since it's an open source package.


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.