[HLSL CODE] Desaturate specific color channels

share shaders here
Post Reply
  • Author
  • Message
Offline
*sensei*
Posts: 372
Joined: 28 Jul 2013, 23:26

[HLSL CODE] Desaturate specific color channels

Simple concept, use as you see fit. I didn't add GUI/DNI functionality

Defines

Code: Select all

#define desat_red	0		//Range 0-inf.
#define desat_green	0		//Range 0-inf.
#define desat_blue	0		//Range 0-inf.
Funky helper function. Picked this since it's a bit better dealing with blues.

Code: Select all

float WeightedLuma(float3 color)
{
return sqrt( 0.299*( color.r*color.r ) + 0.587*( color.g*color.g ) + 0.114*( color.b*color.b ));
}
Pixel shader code part

Code: Select all

color.rgb = saturate( color.rgb ); //comment out if not using LDR, but this effect should run in LDR.
float gr = WeightedLuma( color.rgb );
float r = color.r;
float g = color.g;
float b = color.b;
float sr = saturate( desat_red * ( r-b-g+r ));
float sg = saturate( desat_green * ( g-r-b+g ));
float sb = saturate( desat_blue * ( b-r-g+b ));

color.rgb = lerp( color.rgb, gr, saturate( sr + sg + sb ));
Some people may find use for this fighting too high saturation in specific color. Would require some extra coding if also wants controls over cyan, magenta and yellows but same concept applies.

Example magenta (just out of top of my head, didn't test but should be fairly close)
float sm = saturate( desat_magenta * ( min(r,b)-2*max(r,b)-g+r+b ));

Cheers,
prod80
Post Reply