precision highp float;

uniform sampler2D uSampler;
uniform vec2 uResolution;

varying vec2 vTextureCoord;

const vec4 params = vec4(0.748, 0.874, 0.241, 0.241);
const vec3 W = vec3(0.299,0.587,0.114);

vec2 blurCoordinates[12];

float hardLight(float color) {
    if(color <= 0.5) {
        color = color * color * 2.0;
    } else {
        color = 1.0 - ((1.0 - color)*(1.0 - color) * 2.0);
    }
    return color;
}

void main() {
    vec3 centralColor = texture2D(uSampler, vTextureCoord).rgb;
    if(centralColor.r > 0.2 ){
        blurCoordinates[0] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(0.0, -10.0);
        blurCoordinates[1] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(0.0, 10.0);
        blurCoordinates[2] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(-10.0, 0.0);
        blurCoordinates[3] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(10.0, 0.0);
        blurCoordinates[4] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(5.0, -8.0);
        blurCoordinates[5] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(5.0, 8.0);
        blurCoordinates[6] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(-5.0, 8.0);
        blurCoordinates[7] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(-5.0, -8.0);
        blurCoordinates[8] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(8.0, -5.0);
        blurCoordinates[9] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(8.0, 5.0);
        blurCoordinates[10] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(-8.0, 5.0);
        blurCoordinates[11] = vTextureCoord.xy + vec2(0.00185, 0.00104) * vec2(-8.0, -5.0);

        float sampleColor = centralColor.g * 12.0;
        sampleColor += texture2D(uSampler, blurCoordinates[0]).g;
        sampleColor += texture2D(uSampler, blurCoordinates[1]).g;
        sampleColor += texture2D(uSampler, blurCoordinates[2]).g;
        sampleColor += texture2D(uSampler, blurCoordinates[3]).g;
        sampleColor += texture2D(uSampler, blurCoordinates[4]).g;
        sampleColor += texture2D(uSampler, blurCoordinates[5]).g * 2.0;
        sampleColor += texture2D(uSampler, blurCoordinates[6]).g * 2.0;
        sampleColor += texture2D(uSampler, blurCoordinates[7]).g * 2.0;
        sampleColor += texture2D(uSampler, blurCoordinates[8]).g * 2.0;
        sampleColor += texture2D(uSampler, blurCoordinates[9]).g * 2.0;
        sampleColor += texture2D(uSampler, blurCoordinates[10]).g * 2.0;
        sampleColor += texture2D(uSampler, blurCoordinates[11]).g * 2.0;
        sampleColor = sampleColor / 31.0;

        float highPass = centralColor.g - sampleColor + 0.5;

        for (int i = 0; i < 3;i++)
        {
            highPass = hardLight(highPass);
        }
        float luminance = dot(centralColor, W);
        float alpha = pow(luminance, 0.33);

        vec3 smoothColor = centralColor + (centralColor-vec3(highPass))*alpha*0.1;

        gl_FragColor = vec4(mix(smoothColor.rgb, max(smoothColor, centralColor), alpha), 1.0);
    }else{
        gl_FragColor = vec4(centralColor.rgb,1.0);
    }
}