#define D2R(d) radians(d)

#define ORIGIN (0.5 * size.xy)
float R;
float S;

uniform float angle;
uniform float angle2;
uniform float radius;

uniform sampler2D tex0;

uniform vec2 size;


vec4 rgb2cmyki(in vec3 rgb)
{
  /*float k = min(1.0 - rgb.r, min(1.0 - rgb.g, 1.0 - rgb.b));
  float c = (1.0 - rgb.r - k0) / (1.0 - k0);
  float m = (1.0 - rgb.g - k0) / (1.0 - k0);
  float y = (1.0 - rgb.b - k0) / (1.0 - k0);*/
  
  vec3 cmy = 1.0 - rgb;
  float k = 0.5 * min(cmy.x, min(cmy.y, cmy.z));
  
  //cmy -= k * 0.15;
    
  vec4 cmyk = vec4(cmy, k);
  
  return vec4(cmyk.x, cmyk.y, cmyk.z, cmyk.w);                   
}

vec3 cmyki2rgb(in vec4 c)
{
  float r = 1.0 - min(1.0, c.r * (1.0 - c.a) + c.a);
  float g = 1.0 - min(1.0, c.g * (1.0 - c.a) + c.a);
  float b = 1.0 - min(1.0, c.b * (1.0 - c.a) + c.a);
  
  return vec3(r,g,b);
}

vec2 px2uv(in vec2 px)
{
	return vec2(px / size.xy);
}

vec2 grid(in vec2 px)
{
	return px - mod(px,S);
}

float halftone(in vec2 fc,in mat2 m, int which)
{
  //       0
  //    3 smp 1
  //       2
  
  vec2 fc0 = vec2(0.0,  S) * m;
  vec2 fc1 = vec2( S, 0.0) * m;
  vec2 fc2 = vec2(0.0, -S) * m;
  vec2 fc3 = vec2(-S, 0.0) * m; 
  
  float radius = 0.7071;

	vec2 smp  = (grid(m * fc) + 0.5 * S) * m;
	vec2 smp0 = (grid(m * fc) + 0.5 * S) * m + fc0;
	vec2 smp1 = (grid(m * fc) + 0.5 * S) * m + fc1;
	vec2 smp2 = (grid(m * fc) + 0.5 * S) * m + fc2;
	vec2 smp3 = (grid(m * fc) + 0.5 * S) * m + fc3;
	
	float c  = rgb2cmyki(texture2D(tex0, px2uv(smp  + ORIGIN)).rgb)[which];
	float c0 = rgb2cmyki(texture2D(tex0, px2uv(smp0 + ORIGIN)).rgb)[which];
	float c1 = rgb2cmyki(texture2D(tex0, px2uv(smp1 + ORIGIN)).rgb)[which];
	float c2 = rgb2cmyki(texture2D(tex0, px2uv(smp2 + ORIGIN)).rgb)[which];
	float c3 = rgb2cmyki(texture2D(tex0, px2uv(smp3 + ORIGIN)).rgb)[which];
 
  float smooth = 0.9;
  float s  = smoothstep(smooth, 1.0, (min(length(fc - smp ) / (c  * radius * S), 1.0)));
  float s0 = smoothstep(smooth, 1.0, (min(length(fc - smp0) / (c0 * radius * S), 1.0)));
  float s1 = smoothstep(smooth, 1.0, (min(length(fc - smp1) / (c1 * radius * S), 1.0)));
  float s2 = smoothstep(smooth, 1.0, (min(length(fc - smp2) / (c2 * radius * S), 1.0)));
  float s3 = smoothstep(smooth, 1.0, (min(length(fc - smp3) / (c3 * radius * S), 1.0)));
  
  s = 1.0 - min(s, min(s0, min(s1, min(s2, s3))));
  
	return s;
}

mat2 rotm(in float r)
{
  float cr = cos(r);
	float sr = sin(r);
	return mat2(
		cr,-sr,
		sr,cr
	);
}

void main()
{
  R = D2R(angle);
  S = radius;
  
  
  float R2 = D2R(angle2);
	
	vec2 fc = gl_FragCoord.xy - ORIGIN;
	
	mat2 mc = rotm(R + D2R(15.0));
	mat2 mm = rotm(R + D2R(45.0));
	mat2 my = rotm(R2);
	mat2 mk = rotm(R2 + D2R(75.0));
	
	/*vec3 c = cmyki2rgb((vec4(
		halftone(fc, mc, 0),
		halftone(fc, mm, 1),
		halftone(fc, my, 2),
		halftone(fc, mk, 3)
	)));*/
    
  vec4 cmyk = vec4(
    halftone(fc, mc, 0),
    halftone(fc, mm, 1),
    halftone(fc, my, 2),
    halftone(fc, mk, 3)
  );
  vec3 c = cmyki2rgb(cmyk);
  //vec3 c = vec3(cmyk.b);
    
	gl_FragColor = vec4(c, 1.0);
}
