// Color Target v1.3 by rwp80
// Released 13th July 2008
#region UICode
int Amount1=0;   //[0,255]Color Tolerance
int Amount2=255;   //[0,255]Brightness Tolerance
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra PrimaryColor = (ColorBgra)EnvironmentParameters.PrimaryColor;
    ColorBgra CurrentPixel;
    double R, G, B, A, PCR, PCG, PCB, PCA, GtR, GtB, PCGtR, PCGtB;
    double ColTol = (double)Amount1;    
    double BriTol = (double)Amount2;    
        
    for(int y = rect.Top; y < rect.Bottom; y++)
    {
        for (int x = rect.Left; x < rect.Right; x++)
        {
            CurrentPixel = src[x,y];
            B = (double)CurrentPixel.B;
            G = (double)CurrentPixel.G;
            R = (double)CurrentPixel.R;
            A = (double)CurrentPixel.A;            
            PCB = (double)PrimaryColor.B;
            PCG = (double)PrimaryColor.G;
            PCR = (double)PrimaryColor.R;
            PCA = (double)PrimaryColor.A;            
            GtR = R - G;
            GtB = B - G;
            PCGtR = PCR - PCG;
            PCGtB = PCB - PCG;
                                                
            if ((GtR <= PCGtR - ColTol) || (GtR >= PCGtR + ColTol) || (GtB <= PCGtB - ColTol) || (GtB >= PCGtB + ColTol) || (B <= PCB - BriTol) || (B >= PCB + BriTol) || (G <= PCG - BriTol) || (G >= PCG + BriTol) || (R <= PCR - BriTol) || (R >= PCR + BriTol)) 
            {
                A = 0;                
            }                    
                                                                    
            CurrentPixel = ColorBgra.FromBgra(Utility.ClampToByte(B),Utility.ClampToByte(G),Utility.ClampToByte(R),Utility.ClampToByte(A));
            dst[x,y] = CurrentPixel;
        }
    }
}
