
#version 130


uniform sampler2D SamplerBGRA;
//uniform sampler2D SamplerTableTex1;
//uniform sampler2D SamplerTableTex2;
//uniform sampler2D SamplerBackTex1;
//uniform sampler2D SamplerBackTex2;

out     vec4 		outText;	// output sampler

uniform  float strength; // effect strength, blend with source, 0 -- 1, default is 1

void main(void)
{
	 vec2 v_texCoord = gl_TexCoord[0].st;

     vec3 src, dst;
     vec3 tmp_s;
    
     float fmax;
     float fmin;
     float fdelta;
        vec3 hsv;
        float fh_red = -0.7;
        float fh_yellow = 0.0;
        float fh_green = 0.5;
        float fh_cyan = 0.0;
        float fh_blue = -0.7; 
        float fh_magenta = -0.7;
        float fs_off;
    
    src.rgb = texture2D(SamplerBGRA, v_texCoord).rgb;        
    dst = src;
    
    //rgb2hsv
    fmax = max(dst.r,max(dst.g,dst.b));
    fmin = min(dst.r,min(dst.g,dst.b));
    fdelta = fmax - fmin;
    
    hsv.z = fmax;
    if(0.0 == fdelta)
    {
        hsv.x = 0.0;
        hsv.y = 0.0;
        }
        //hue calculate
        else
    {
        hsv.y = fdelta/fmax;
        if(fmax == dst.r)
        {
            if(dst.g >= dst.b)
            {
                //0
                hsv.x = (dst.g - dst.b)/fdelta;
                fs_off = (fh_yellow - fh_red)*hsv.x + fh_red;
                //saturation adjust
                hsv.y = hsv.y*(1.0 + fs_off);
                hsv.y = min(1.0 , hsv.y);
                hsv.y = max(0.0 , hsv.y);
                //rgb2hsv end            
                    
                //hsv2rgb
                dst.r = hsv.z;
                dst.b = hsv.z*(1.0 - hsv.y);
                dst.g = hsv.z*(1.0 - hsv.y + hsv.y*hsv.x);
            }
            else
            {
                //5
                //hsv.x = (dst.b - dst.g)/fdelta;
                hsv.x = (dst.r - dst.b)/fdelta;
                fs_off = (fh_red - fh_magenta)*hsv.x + fh_magenta;
                //saturation adjust
                hsv.y = hsv.y*(1.0 + fs_off);
                hsv.y = min(1.0 , hsv.y);
                hsv.y = max(0.0 , hsv.y);
                //rgb2hsv end
                //hsv2rgb
                dst.r = hsv.z;
                dst.g = hsv.z*(1.0 - hsv.y);
                dst.b = hsv.z*(1.0 - hsv.y*hsv.x);
            }
        }
        else if(fmax == dst.g)
        {
            
            if(dst.r > dst.b)
            {
                //1
                //hsv.x = (dst.r - dst.b)/fdelta;
                hsv.x = (dst.g - dst.r)/fdelta;
                fs_off = (fh_green - fh_yellow)*hsv.x + fh_yellow;
                //saturation adjust
                hsv.y = hsv.y*(1.0 + fs_off);
                hsv.y = min(1.0 , hsv.y);
                hsv.y = max(0.0 , hsv.y);
                //rgb2hsv end
                //hsv2rgb
                dst.g = hsv.z;
                dst.r = hsv.z*(1.0 - hsv.y*hsv.x);
                dst.b = hsv.z*(1.0 - hsv.y);
            }
            else
            {
                //2
                hsv.x = (dst.b - dst.r)/fdelta;
                fs_off = (fh_cyan - fh_green)*hsv.x + fh_green;
                //saturation adjust
                hsv.y = hsv.y*(1.0 + fs_off);
                hsv.y = min(1.0 , hsv.y);
                hsv.y = max(0.0 , hsv.y);
                //rgb2hsv end
                //hsv2rgb
                dst.g = hsv.z;
                dst.r = hsv.z*(1.0 - hsv.y);
                dst.b = hsv.z*(1.0 - hsv.y + hsv.y*hsv.x);
            }
        }
        else
        {
            if(dst.g > dst.r)
            {
                //3
                //hsv.x = (dst.g - dst.r)/fdelta;
                hsv.x = (dst.b - dst.g)/fdelta;
                fs_off = (fh_blue - fh_cyan)*hsv.x + fh_cyan;
                //saturation adjust
                hsv.y = hsv.y*(1.0 + fs_off);
                hsv.y = min(1.0 , hsv.y);
                hsv.y = max(0.0 , hsv.y);
                //rgb2hsv end
                //hsv2rgb
                dst.b = hsv.z;
                dst.r = hsv.z*(1.0 - hsv.y);
                dst.g = hsv.z*(1.0 - hsv.y*hsv.x);
            }
            else
            {
                //4
                hsv.x = (dst.r - dst.g)/fdelta;
                fs_off = (fh_magenta - fh_blue)*hsv.x + fh_blue;
                //saturation adjust
                hsv.y = hsv.y*(1.0 + fs_off);
                hsv.y = min(1.0 , hsv.y);
                hsv.y = max(0.0 , hsv.y);
                //rgb2hsv end
                //hsv2rgb
                dst.b = hsv.z;
                dst.r = hsv.z*(1.0 - hsv.y + hsv.y*hsv.x);
                dst.g = hsv.z*(1.0 - hsv.y);
            }
        }
    }
        tmp_s.r = 0.29410;
        tmp_s.g = 0.55292;
        tmp_s.b = 0.06665;
        tmp_s.r = min(1.0,dst.r<=0.5?(2.0*dst.r*tmp_s.r):(2.0*(dst.r+tmp_s.r) - 2.0*(dst.r*tmp_s.r) - 1.0));
        tmp_s.g = min(1.0,dst.g<=0.5?(2.0*dst.g*tmp_s.g):(2.0*(dst.g+tmp_s.g) - 2.0*(dst.g*tmp_s.g) - 1.0));
        tmp_s.b = min(1.0,dst.b<=0.5?(2.0*dst.b*tmp_s.b):(2.0*(dst.b+tmp_s.b) - 2.0*(dst.b*tmp_s.b) - 1.0));

        dst = dst*0.75 + tmp_s*0.25;

    outText = vec4(mix(src, dst, strength),1.0);
}