<jittershader name="contrast-interp">
	<description>Shader for for modifying image contrast by interpolation and extrapolation</description>
	<param name="tex0" type="int" default="0" />
	<language name="glsl" version="1.0">
		<bind param="tex0" program="fp" />
		<program name="vp" type="vertex">
<![CDATA[

varying vec2 texcoord0;

void main() {
    gl_Position = ftransform();
    texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);
}
]]>		
		</program>
		<program name="fp" type="fragment">
<![CDATA[

varying vec2 texcoord0;
uniform sampler2DRect tex0;

vec3 rgb2hsl(vec3 vrgb) {
	vec3 hsl;

	float vmin = min(min(vrgb.x, vrgb.y), vrgb.z);
	float vmax = max(max(vrgb.x, vrgb.y), vrgb.z);
	float delta = vmax-vmin;
	float deltasum = vmax+vmin;
	
	hsl.z = deltasum*0.5;

	if(delta == 0.) {
		// grey
		hsl.x = 0.;
		hsl.y = 0.;
	}
	else {
		if(hsl.z < 0.5 ) {
			hsl.y = delta/deltasum;
		}
		else {
			hsl.y = delta/(2.-vmax-vmin);
		}

		vec3 drgb = ((vmax-vrgb)*0.16666666666667 + 0.5*delta)/delta;
		
		if(vrgb.x == vmax) {
			hsl.x = drgb.z-drgb.y;
		}
		else if(vrgb.y == vmax) {
			hsl.x = 0.3333333333333 + drgb.x - drgb.z;
		}
		else {
			hsl.x = 0.6666666666667 + drgb.y - drgb.x;
		}
		
		
		if(hsl.x < 0.) {
			hsl.x += 1.;
		}
		else if(hsl.x > 1.) {
			hsl.x -= 1.;
		}
	}

	return hsl;
}

vec4 rgb2hsl(vec4 vrgba) {
	vec4 hsla;
	hsla.rgb = rgb2hsl(vrgba.rgb);
	hsla.a = vrgba.a;
	return hsla;
}

void main() {
	vec4 rgba = texture2DRect(tex0, texcoord0);
	gl_FragColor = rgb2hsl(rgba);
}
]]>		
		</program>		
	</language>
</jittershader>
