[Help]Correct saturation math?

share shaders here
Post Reply
  • Author
  • Message
Offline
Posts: 66
Joined: 12 Jun 2013, 07:19

[Help]Correct saturation math?

Initially I was using the same code as Boris:

Code: Select all

ncolor = normalize(color);
scolor = color/ncolor;
ncolor = pow(ncolor, saturation);
color = ncolor*scolor;
However this code not only change saturation but also have quite noticable brightness change. I guess this has something to do with human eyes percepts luminance differently for each color.
So I tried another math which I found by googling:

Code: Select all

	float sGray = dot(color.xyz, float3(0.27,0.67,0.06));
	color.xyz = saturation*color.xyz + (1.0-saturation)*sGray;
It works great for desaturation, but it oversaturates, polarize or even reverse colors when they hit [0,1] boundaries.

I also tried combining both: use pow to saturate then calculate and scale luma back to its original level, but the combining effect changes hue. Or I may have done something wrong, need to review the code again, did it in a haste.

I haven't tried converting to HSV. Because S in HSV also caps at [0,1], it's additional to adjust saturation while keep it restrained to a strict range. makes things complicated.

Any good ideas?

Offline
User avatar
*blah-blah-blah maniac*
Posts: 17444
Joined: 27 Dec 2011, 08:53
Location: Rather not to say

Re: [Help]Correct saturation math?

Apply contrast to luma, then correct colors according to changes of pixel. More expensive, but i think it's better.
_________________
i9-9900k, 64Gb RAM, RTX 3060 12Gb, Win7

Offline
Posts: 66
Joined: 12 Jun 2013, 07:19

Re: [Help]Correct saturation math?

Got it, use pow on normalized color then scale it back to original luma, now saturation won't change luma (not visible to my eyes at least):

Code: Select all

	float sVal = 1.0*tKey0;
	float sLuma = dot(color.xyz,float3(0.27, 0.67, 0.06));
	float3 sNcolor = normalize(color.xyz);
	sNcolor.xyz = pow(sNcolor.xyz, sVal);
	float sNluma = dot(sNcolor.xyz,float3(0.27, 0.67, 0.06));
	color.xyz = sNcolor.xyz*sLuma/sNluma;

Offline
User avatar
*blah-blah-blah maniac*
Posts: 17444
Joined: 27 Dec 2011, 08:53
Location: Rather not to say

Re: [Help]Correct saturation math?

Can't help with such things now, i'm not in the mood this week.
_________________
i9-9900k, 64Gb RAM, RTX 3060 12Gb, Win7
Post Reply