uniform float time;
uniform float hue;

vec3 rgb2hsv(vec3 c) {
    vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

vec3 hsv2rgb(vec3 c) {
    vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}


#define M_PI 3.1415926535897932384626433832795


float rand() {
  return fract(sin(time+dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453);
}

vec4 plasma()
{
    vec2 scale = vec2(1.,1.);
    vec2 coord = gl_FragCoord.xy * scale;
    float d = 0.0;
    d+=sin(coord.x+time*4.)*0.3;
    d+=cos(coord.y+time*4.)*0.8;
    d-=sin(coord.x+coord.y+time*4.);
    coord += vec2(sin(coord.x+time*2.), cos(coord.y+time*3.));
    d += sin(sqrt(d*10.+coord.x*coord.x+coord.y*coord.y)+time);
    d = (cos(M_PI*1.4*d)*0.5+0.5);

    return vec4(vec3(clamp(d, 0.0, 1.0)*0.6+clamp(0.4*rand(), 0.0, 0.4)), 1.);
}

void drawSyringeFluid()
{
    if (gl_FragColor.g > (gl_FragColor.b+gl_FragColor.r)/2.0) {
      gl_FragColor = mix(gl_FragColor, clamp(gl_FragColor*plasma(), vec4(0.0), vec4(1.0)), 0.4);

      vec3 hsv = rgb2hsv(gl_FragColor.rgb);
      hsv.x = hue;
      hsv.z = hsv.z*0.9 + clamp((sin((gl_FragCoord.x-gl_FragCoord.y)*100.0)*0.5+0.5)*0.1,0.0,0.1);
      gl_FragColor.rgb = hsv2rgb(hsv);
    }
}
