varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
uniform sampler2D u_texture;
uniform float time;

uniform vec3[10] planet_colors;

uniform int[10] planet_colors_r;
uniform int[10] planet_colors_g;
uniform int[10] planet_colors_b;

uniform int planet_colors_count;

void main(void)
{
    const float tolerance = 0.01;

    vec4 c = texture2D(u_texture, v_texCoord);
    vec4 final_color = vec4(0);

    //Check if this color matches any of the planet colors
    if(c.a > 0.0)
    {
        for(int i = 0; i < planet_colors_count; i++)
        {
            //vec3 planet_color = planet_colors[i];
            //vec3 planet_color = vec3(255.0/255.0, 66.0/255.0, 217/255.0);

            int r = planet_colors_r[i];
            int g = planet_colors_g[i];
            int b = planet_colors_b[i];

            //vec3 planet_color = vec3(float(r)/255.0, float(g)/255.0, float(b)/255.0);

            int r_dif = abs(int(c.r*255.0) - r);
            int g_dif = abs(int(c.g*255.0) - g);
            int b_dif = abs(int(c.b*255.0) - b);
/*


            float c_interval_r_lower = planet_color.r - tolerance;
            float c_interval_r_upper = planet_color.r + tolerance;

            float c_interval_g_lower = planet_color.g - tolerance;
            float c_interval_g_upper = planet_color.g + tolerance;

            float c_interval_b_lower = planet_color.b - tolerance;
            float c_interval_b_upper = planet_color.b + tolerance;*/

/*            if(c.r >= c_interval_r_lower && c.r <= c_interval_r_upper
                && c.g >= c_interval_g_lower && c.g <= c_interval_g_upper
                && c.b >= c_interval_b_lower && c.b <= c_interval_b_upper)
            {
                c = vec4(planet_color,1.0);
            }*/
            
            if((r_dif < 2) && (g_dif < 2) && (b_dif < 2))
            {   
                final_color = c;//vec4(planet_color,1.0);
                break;
            }
  /*          else
            {
                final_color = vec4(0,0,1,1);
            }*/
        }
    }

/*    float luminace = (0.2126*c.r + 0.7152*c.g + 0.0722*c.b);
    const float luminace_limit = 0.55;

    if(luminace < luminace_limit)
    {
        c = vec4(0);
    }*/

    gl_FragColor = final_color;
}