Skip to content

Instantly share code, notes, and snippets.

@NocturnDragon
Created January 28, 2016 22:53
Show Gist options
  • Save NocturnDragon/fe815ba8cb40330f938c to your computer and use it in GitHub Desktop.
Save NocturnDragon/fe815ba8cb40330f938c to your computer and use it in GitHub Desktop.
// Simple color picker by @rianflo
// http://twitter.com/rianflo
// public domain
#ifdef GL_ES
precision highp float;
#endif
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
uniform sampler2D bb;
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);
}
float sdRect(vec2 p, vec2 t, vec2 b)
{
vec2 d = abs(p-t)-b;
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
}
float sdCircle(vec2 p, vec2 t, float r)
{
return length(p-t)-r;
}
const float PI = 3.14159265358979323846264;
void main()
{
vec2 huePos = vec2(0.35, 0.0);
mat2 ar = mat2(
cos(time), sin(time),
-sin(time), cos(time)
);
huePos *= ar;
vec2 shadePos = vec2(0.17, 0.17);
shadePos *= sin(time*0.2);
vec2 p = (gl_FragCoord.xy/resolution*2.0-1.0) * vec2(1.0, resolution.y/resolution.x);
float sd = sdRect(p, vec2(0.0), vec2(0.17));
float osd = max(sdCircle(p, vec2(0.0), 0.4), -sdCircle(p, vec2(0.0), 0.3));
float a = min(1.0, length(p) / 0.25);
vec2 d = normalize(p);
float b = (atan(d.y, d.x) + (PI)) / (2.0*PI);
// Hue
float L = (atan(huePos.y, huePos.x) + (PI)) / (2.0*PI);
float hsd = max(sdCircle(p, huePos, 0.058), -sdCircle(p, huePos, 0.04));
// Shade
float ssd_inner = sdCircle(p, shadePos, 0.045);
float ssd = min(sdCircle(p, shadePos, 0.058), ssd_inner);
vec3 color = vec3(0.0);
if (osd < 0.0)
{
color = hsv2rgb(vec3(b, 1.0, 1.0));
}
else if (sd < 0.0)
{
color.rgb = hsv2rgb(vec3(L, 0.5+0.5*clamp(vec2(p)/(0.17), vec2(-1.0), vec2(1.0))));
}
vec3 selectedColor = hsv2rgb(vec3(L, 0.5+0.5*clamp(vec2(shadePos)/(0.17), vec2(-1.0), vec2(1.0))));
sd = min(ssd, min(hsd, min(sd, osd)));
color = smoothstep(0.0, -2.0/resolution.y, sd)*color;
color.rgb = mix(vec3(1.0), color, smoothstep(-2.0/resolution.y, 0.0, hsd));
color.rgb = mix(vec3(1.0), color, smoothstep(-2.0/resolution.y, 0.0, ssd));
color.rgb = mix(selectedColor, color, smoothstep(-2.0/resolution.y, 0.0, ssd_inner));
float mask = smoothstep(2.0/resolution.y, 0.0, -sd);
gl_FragColor = vec4(color, 1.0-mask);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment