8
$\begingroup$

I am currently working on some simple pixel shader in HLSL. I send to shader texture and I want to make it more colorful (something like in the picture below).

enter image description here

In the picture 1 there is original texture. Picture 2 shows an effect that I want to achieve. Is there some mathematical formula to do that? My input is the RGBA value of each pixel.

EDIT: I'll try to write more concrete.

Let's say I want to make that garden texture more red. I suppose that what I need to do is:

OutputR = InputR * X,

OuputG = InputG * Y,

OutputB = InpputB * Z

But how do I find X, Y and Z?

$\endgroup$
6
  • $\begingroup$ When you say "more colorful" do you mean specifically as if looking through tinted glass? $\endgroup$ Commented Sep 2, 2015 at 11:48
  • $\begingroup$ Yes, that's what i mean $\endgroup$ Commented Sep 2, 2015 at 11:50
  • $\begingroup$ Do you also want to take the tint colour as a separate input, or do you just want an arbitrary tint colour that happens to be cheap to implement? $\endgroup$ Commented Sep 2, 2015 at 11:51
  • $\begingroup$ Sorry, I think I don't understand your question. Could you explain that more? $\endgroup$ Commented Sep 2, 2015 at 11:53
  • $\begingroup$ If Kostas Anagnostou's answer is what you want then ignore my comments. I was just asking whether you wanted that or a hardcoded colour. $\endgroup$ Commented Sep 2, 2015 at 15:32

3 Answers 3

6
$\begingroup$

Extending Kostas Anagnostou's answer, a commonly used formula for desaturation is

float value = 0.3 * InputR + 0.59 * InputG + 0.11 * InputB;

This accomodates the fact that different color hues are perceived with a different intensity by a human observer.

Further following the example, you would then define some tint color that is multiplied with the desaturated value. Also you could mix the tinted image with your original to get a less drastic effect. In this case, we're mixing 80% of the desaturated image tinted with (1.0, 0.2, 0.2) with 20% of the original:

float3 tint = float3(1.0, 0.2, 0.2);
float tintMix = 0.8;
OutputR = tintMix * value * tint.r + (1.0 - tintMix) * InputR;
OutputG = tintMix * value * tint.g + (1.0 - tintMix) * InputG;
OutputB = tintMix * value * tint.b + (1.0 - tintMix) * InputB;
$\endgroup$
1
  • 1
    $\begingroup$ Note that (0.3, 0.59, 0.11) are the luma coefficients for Rec. 601 (aka Standard Def. NTSC), whereas Rec. 709 (aka High Def. ATSC) would be (0.2126, 0.7152, 0.0722). And sRGB images (which is likely what you're dealing with) are closer to Rec. 709 than Rec. 601. $\endgroup$ Commented May 19, 2017 at 2:45
13
$\begingroup$

(XYZ) can be the RGB colour you want to tint your scene by. For the above scene it can be a red colour (1.0, 0.0, 0.0) or something similar with a strong red component.

Bear in mind that since you are applying the colour in a multiplicative way it will act as a filter suppressing original colour components. So if your scene is mostly green but you apply (i.e. multiply it with) a red filter (1,0,0) the resulting scene will be very dark.

A trick you could potentially do in such cases is to desaturate the original scene and then multiply it by the tint colour. That way you will keep the overall image intensity and achieve the colour tint you require.

For example, here is the image multiplied by (1, 0, 0), (1, 0.2, 0.2), and (1, 0.5, 0.5) from left to right:

enter image description here enter image description here enter image description here

$\endgroup$
3
  • $\begingroup$ Also you might want to tint something other than 100% pure one channel color. $\endgroup$
    – joojaa
    Commented Sep 2, 2015 at 19:14
  • 3
    $\begingroup$ I went ahead and added some example images to the post - hope you don't mind! $\endgroup$ Commented Sep 3, 2015 at 5:29
  • $\begingroup$ That is exactly what I meant. Thanks for effort and examples :) $\endgroup$ Commented Sep 3, 2015 at 16:17
3
$\begingroup$

Choose the color RGB of your colorful glasses and choose how transparent they are by choosing an alpha value A. Then alpha composite the glasses on top of the input image:

OutputR = R * A + InputR * (1-A)
OutputG = G * A + InputG * (1-A)
OutputB = B * A + InputB * (1-A)
$\endgroup$
3
  • $\begingroup$ What do you mean by "composite"? Is there some mathematical operation that lets me to composite two colours? $\endgroup$ Commented Sep 2, 2015 at 12:00
  • $\begingroup$ That's what the example equations in his post do. $\endgroup$
    – yuriks
    Commented Sep 2, 2015 at 12:51
  • 2
    $\begingroup$ I don't think this is what he wants. If you wear red-tinted glasses, you see red wavelengths at basically full strength, and very little from other wavelengths. That's not a great match for alpha compositing. $\endgroup$ Commented Sep 2, 2015 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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