<p>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)).</p>

<p>You can always look at the <a href="https://www.gimp.org/source/#gimp-source-code" rel="nofollow noreferrer">source code</a> since it's an open source package.</p>

CC BY-SA 3.0
2017-10-09T01:29:59.570Z
I'm getting close. Using the source I was able to write a shader that approximates the intended results. See here:  
  
https://imgur.com/a/kWGce  
  
Here is my GLSL fragment shader code:  
  
https://gist.github.com/anonymous/aacbe1aafe2fac75216b113bacbb8a0f
    
And here is the GimpColorizeOperation source code I'm drawing from:  
  
https://github.com/GNOME/gimp/blob/master/app/operations/gimpoperationcolorize.c  
  
Still some ways off..
carlr
CC BY-SA 3.0
2017-10-17T00:47:47.027Z
Maybe use hsl instead of hsv/hsb?
Sebastián Mestre
CC BY-SA 3.0
2017-10-08T02:07:17.910Z
2017-10-08T02:07:17.910Z
user1118321
<p>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.</p>

<p><a href="https://i.sstatic.net/gDWgl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gDWgl.png" alt="GIMP&#39;s Colorize"></a></p>

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

<pre><code>pix_h, pix_s, pix_v
</code></pre>

<p>And I also define the inputs of the colorize function as:</p>

<pre><code>col_h, col_s, col_v
</code></pre>

<p>What calculations can I perform to determine the output pixel hsv?</p>

<p>I think I can get:</p>

<pre><code>out_h = col_h
</code></pre>

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

<p><strong>EDIT:</strong>
Where lum is the luminance of the input pixel:</p>

<pre><code>out_s = clamp( (1 - pix_v) * 2, 0, 1)

out_v = clamp( pix_v * 2, 0, 1)
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2017-10-07T15:15:37.360Z
Have you tried out_s = col_s and out_v = in_v?
Sebastián Mestre
CC BY-SA 3.0
2017-10-07T16:53:14.073Z
@SebastiánMestre Yes, this was my first thought. Unfortunately it does not replicate the Colorize behavior.
carlr
CC BY-SA 3.0
2017-10-07T23:04:59.933Z
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?
gallickgunner
<transformations><glsl><color><2d><pixel-shader>
How can I implement GIMP's "Colorize" function?
carlr
# How can I implement GIMP's "Colorize" function?

(Asked by: username_0 on 2017-10-07)

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](https://i.sstatic.net/gDWgl.png)](https://i.sstatic.net/gDWgl.png)

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.

**Tags:** transformations, glsl, color, 2d, pixel-shader

> **Comments:**
> **username_1**: Have you tried out_s = col_s and out_v = in_v?  
> **username_0**: @username_1 Yes, this was my first thought. Unfortunately it does not replicate the Colorize behavior.  
> **username_2**: 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?  

---

## 1 answers

### Answer by username_3 on 2017-10-08. Score: 2

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](https://www.gimp.org/source/#gimp-source-code) since it's an open source package.

> **Comments:**
> **username_0**: I'm getting close. Using the source I was able to write a shader that approximates the intended results. See here:      https://imgur.com/a/kWGce      Here is my GLSL fragment shader code:      https://gist.github.com/anonymous/aacbe1aafe2fac75216b113bacbb8a0f      And here is the GimpColorizeOperation source code I'm drawing from:      https://github.com/GNOME/gimp/blob/master/app/operations/gimpoperationcolorize.c      Still some ways off..  
> **username_1**: Maybe use hsl instead of hsv/hsb?  

---

