#version 150

uniform vec2 dt;
uniform vec4 selected, color;
uniform sampler2D t0;
uniform float fill;

void main(void) {
	ivec2 tc = ivec2(gl_FragCoord.xy);
	vec4 ds0 = abs(texelFetchOffset(t0, tc, 0, ivec2(-1, 0)) - selected);
	vec4 ds1 = abs(texelFetchOffset(t0, tc, 0, ivec2( 1, 0)) - selected);
	vec4 ds2 = abs(texelFetchOffset(t0, tc, 0, ivec2(0, -1)) - selected);
	vec4 ds3 = abs(texelFetchOffset(t0, tc, 0, ivec2(0,  1)) - selected);
	float d0 = dot(ds0, vec4(1., 1., 1., 1.)), d1 = dot(ds1, vec4(1., 1., 1., 1.)), d2 = dot(ds2, vec4(1., 1., 1., 1.)), d3 = dot(ds3, vec4(1., 1., 1., 1.));
	float vs = step(1e-6, d0 + d1 + d2 + d3);
	float vm = step(1e-3, (d0 * 25.) * (d1 * 25.) * (d2 * 255.) * (d3 * 255.));
	float v = mix(vs - vm, vs - vm - vm + 1, fill);
	//qgl_FragData[0] = vec4(1,0,0,0.5);//vec4(color.rgb, v * color.a);
	gl_FragColor = v*vec4(color.bgr, v * color.a);
	//qgl_FragData[0] = vec4(1);
}
