Page 1 of 1

GIMP style dynamic colorizing

Posted: Thu Oct 05, 2017 7:05 pm
by CaptainMaelstrom
Hello all,

I'm trying to colorize images dynamically, during run-time.
For example, I have some mostly gray images that I want to color blue (like what's shown inside the black rectangle).
colorize_example.png
colorize_example.png (3.83 KiB) Viewed 1112 times
I'd love to do this with setColor and setBlendMode or similar. But I think I may have to use shaders. So I wrote one to try and achieve the desired effect.

It first converts the set color and the texel to HSV space. The output color will have same hue of set color. I've set the saturation to be the luminance of the image, but I'm not sure what to do about the value part. Copying directly from the image texel seems to result a colorized image that is too dark. So I've multiplied and clamped the value but it's still not quite right..

Anyone have any ideas? See shader code attached (I'm specifically referring to shaders.colorify2).

Re: GIMP style dynamic colorizing

Posted: Thu Oct 05, 2017 8:05 pm
by zorg
H should be static since you want blue; S should also be static since you want the graphics to be saturated to some degree, i.e. you want color;
the only thing you want to adjust is the last remaining parameter, which could be V, which, in the case of the HSL model, would be luminance, but here, it's value, which is kinda similar, but not quite.

Re: GIMP style dynamic colorizing

Posted: Thu Oct 05, 2017 11:25 pm
by raidho36
I use RGB colored image for this, and so I could use up to 3 different colors. The shader would compute saturation and value of every pixel, and generate a new color from a selectable hue and existing saturation and value. Basically it allows you to recolor solid green, solid red and solid blue to any 3 colors respectively while keeping saturation and value.

Re: GIMP style dynamic colorizing

Posted: Mon Oct 09, 2017 1:37 am
by CaptainMaelstrom
zorg: That was what I thought at first too. But after trying that, my colorize function still doesn't behave like Gimp's. For instance, white pixels don't stay white.

raidho36: Mind sharing the shader? Does it also colorize grayscale images?

So far, my efforts have gotten me this far:
https://imgur.com/a/kWGce

As you can see, still not producing intended results. My shader code is here:
https://gist.github.com/anonymous/aacbe ... 3bacbb8a0f

The source code for the Gimp Colorize function is here (so far as I can tell):
https://github.com/GNOME/gimp/blob/mast ... colorize.c

My shader works well for when the input saturation is 100%, but I'm still trying to find formulas that will work for any inputs (H, S and, V [0, 1]).