# How to mix pixel colors in Shader?

I have a pixel that have a colour RGB. This color is calculated by the shader and can be anything.

How can I override this color by a colour I choose. If my pixel is white it's simple, I can do this:

half3 original = half3(1, 1, 1);
half3 mycolor = half3(1, 0, 0);
half3 result = original * mycolor;


But what can I do if:

half3 original = half3(0.36, 0.74, 0.18);
half3 mycolor = half3(1, 0, 0);
half3 result = ?;


What operation or function should I apply to my original pixel color to override it ?

• I don't get the problem here. What is the problem with "result = mycolor;"
– Arne
Jan 30 '14 at 23:09

Just

half3 result = (original + mycolor) / 2;


does what you'd expect because the arithmetic operators are overridden for the half3 type.

It was very easy finally. Here is the answer. I am using an external value.

half3 original = half3(0.36, 0.74, 0.18);
half3 mycolor = half3(1, 0, 0);
half value = 0.5;
half3 result = original * value + mycolor * (1 - value);


You could also use mix(); or lerp(); depending on your platform.

• result = mix(mycolor, original, value); Jan 29 '14 at 15:14
• The mix function doesn't appears in the NVidia developer zone... This function is only available in OpengGL, no ?
– MaT
Jan 29 '14 at 15:21
• GLSL is just my flavor of choice, in HLSL it's called lerp() Jan 29 '14 at 15:27