Shaders to regulate image lightness like in PhotoShop(shader实现Photoshop的亮度调节效果,按钮变暗效果)

We can regulate image lightness in photoshop like this (Ctrl+U):


I find it results in different end lightness for different original lightness and for different lighness offset you want to regulate.

And I make a formula which I have described in previous article here.

And the ratio of three channels of rgb is fixed when we only ajust lightness.

so here is the shader implement the lightness regulation function(image dim effect):

#ifdef GL_ES
precision mediump float;
#endif

#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif

float L(float l, float x)
{
	return clamp((x + abs(x)) * 0.5 + l * (1.0 - abs(x)), 0.0, 1.0);
}

vec3 adjustLightness(vec3 rgb, float offset)
{
	float l = max(rgb.r, max(rgb.g, rgb.b));
	float e = 1.0e-6;
	return clamp(rgb*L(l,offset)/(l+e), 0.0, 1.0);
}

void main()
{
	vec4 color = texture2D(CC_Texture0, v_texCoord);
	color.rgb = adjustLightness(color.rgb,-0.2);
	gl_FragColor = color;
}


你可能感兴趣的:(Cocos2d-x,Computer,Technology)