#version 130

uniform sampler2D tex0;

in vec2 TC1;
out vec4 outColor;

uniform vec4 col;
uniform vec4 col2;
uniform int typ;
uniform vec2 ud;
uniform ivec2 udi;
uniform int grayscale;

void main()
{
	vec4 color = texture2D(tex0,TC1);

	if (typ == 1)
	{
		if( distance(color.rgb, col2.rgb) < 0.01 )
		color.a = 0.0;
	}
	else
	if (typ == 2)
	{
		if ((udi.x == 1) && (color.r > 0.1) && (color.r <= 0.5))
		{
			color = vec4(0,0,0,smoothstep(0.1,0.5,color.r));
		}
		else
		{
			color = vec4(1,1,1,color.r);
		}
	}
	else
	if (typ == 3)
	{
		color = vec4(mix(col, col2, TC1.y));
	}
	if (typ == 4)
	{
		color = vec4(1,1,1,smoothstep(ud.x,ud.y,color.r));	
	}
	else
	if (typ == 5)
	{
		color = vec4(1,1,1,color.r);
	}

	if (typ != 3)
	{
		color = color*col;
	}

	if (grayscale == 1)
	{
		float gray = (color.r+color.g+color.b)/3;
		outColor = vec4(gray,gray,gray,color.a);
	}
	else
	{
		outColor = color;
	}
}