texture al_tex;
sampler2D s = sampler_state {
   texture = <al_tex>;
};

bool enableRecolour;
bool baseColAny;
float4 baseCol;
float4 newCol;

bool doDarkCol;

bool outline;
float4 outlineCol;

float width, height;

const float rng = 0.1;	// range

float4 ps_main(VS_OUTPUT Input) : COLOR0
{
	float4 pixel = tex2D(s, Input.TexCoord.xy);
	if (enableRecolour)
	{
		if (pixel.a == 1 && (baseColAny || (pixel.a == 1 && pixel.r >= baseCol.r && pixel.r <= baseCol.r + rng && pixel.g >= baseCol.g && pixel.g <= baseCol.g + rng && pixel.b >= baseCol.b && pixel.b <= baseCol.b + rng)))
		{
			pixel = newCol;
		}
		else if (doDarkCol && pixel.r >= 0.685 && pixel.r <= 0.695)
		{
			float4 newColDark = float4(max(newCol.r - 0.06, 0), max(newCol.g - 0.06, 0), max(newCol.b - 0.06, 0), 1);
			pixel = newColDark;
		}
	}

	if (pixel.a == 0 && outline)
	{
	   //int width, height;
	   //al_tex.GetDimensions(width, height);
	   const float pixelXSize = 1.0 / width;
	   const float pixelYSize = 1.0 / height;
	   float x2 = max(0, Input.TexCoord.x - pixelXSize);
	   float y2 = max(0, Input.TexCoord.y - pixelYSize);
	   //for (float y2 = max(0, y - pixelYSize); y2 <= min(height - 1, y + pixelYSize); y2 += pixelYSize)
		  // for (float x2 = max(0, x - pixelXSize); x2 <= min(width - 1, x + pixelXSize); x2 += pixelXSize)
	   for (int i = 0; i < 3; ++i)
	   {
			for (int j = 0; j < 3; ++j)
			{
				float4 pixel2 = tex2D(s, float2(x2, y2));
				if (!(i == 1 && j == 1) && pixel2.a == 1 && (baseColAny || (pixel2.r >= 0.74 && pixel2.r <= 0.76)))	// TODO
				{
					pixel = outlineCol;
					return pixel;
				}
				x2 += pixelXSize;
			}
			y2 += pixelYSize;
			x2 = max(0, Input.TexCoord.x - pixelXSize);
	   }
	}

	return pixel;
}
