#define PI 3.14159

void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
    vec2 c = 2.5 * (fragCoord - 0.5 * iResolution.xy) / min(iResolution.y, iResolution.x);

    float angle = iTime;
    c = mat2(cos(angle), -sin(angle), sin(angle), cos(angle)) * c;

    vec2 z = c;
    
    float maxIter = 90.0;
    float iter;
    
    for(iter = 0.0; iter < maxIter; iter++)
    {
        if(dot(z, z) > 1.0) break;
        
        float tmp = log(dot(z, z));
        z.x = log(abs(tmp - c.x));
        z.y = log(abs(tmp - c.y));
    }
    
    // Coloring
    vec3 color;
    if(iter < maxIter)
    {
        float smoothColor = iter - log(log(dot(z, z))) / log(1.5);
        float color1 = 1.5 * smoothColor / maxIter + 2.5 * atan(z.y, z.x) / PI;
        float color2 = 0.5 * smoothColor / maxIter - 3.5 * atan(z.y, z.x) / PI;
        
        color = vec3(
            1.0 + 0.5 * cos(3.0 * PI * (color1 + color2 + iTime / 4.0)),
            0.5 + 0.1 * cos(3.1 * PI * (color1 - color2 + iTime / 4.0)),
            0.9 + 0.5 * cos(3.2 * PI * (color1 + iTime / 4.0))
        );
    }
    else
    {
        color = vec3(0.0);
    }
    
    fragColor = vec4(color, 1.0);
}
