From 857a94a27c47da83c7a6cd2e2803dbecb3e723ce Mon Sep 17 00:00:00 2001 From: hellerve Date: Mon, 5 Jun 2017 17:17:46 -0400 Subject: [PATCH] basic: added hsb illustration; utilities: added hsb utilities --- basic/hsb.glsl | 40 ++++++++++++++++++++++++++++++++++++++++ utilities/hsb.glsl | 23 +++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 basic/hsb.glsl create mode 100644 utilities/hsb.glsl diff --git a/basic/hsb.glsl b/basic/hsb.glsl new file mode 100644 index 0000000..ee8db2b --- /dev/null +++ b/basic/hsb.glsl @@ -0,0 +1,40 @@ +#ifdef GL_ES +precision mediump float; +#endif + +uniform vec2 u_resolution; +uniform vec2 u_mouse; + +vec3 rgb2hsb( in 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 hsb2rgb( in vec3 c ){ + vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0), + 6.0)-3.0)-1.0, + 0.0, + 1.0 ); + rgb = rgb*rgb*(3.0-2.0*rgb); + return c.z * mix(vec3(1.0), rgb, c.y); +} + +void main(){ + vec2 st = gl_FragCoord.xy/u_resolution; + vec3 color = vec3(0.0); + + // by moving the mouse upwards, we increase the saturation + color = hsb2rgb(vec3(st.x,u_mouse.y,st.y)); + + gl_FragColor = vec4(color,1.0); +} diff --git a/utilities/hsb.glsl b/utilities/hsb.glsl new file mode 100644 index 0000000..a63348e --- /dev/null +++ b/utilities/hsb.glsl @@ -0,0 +1,23 @@ +vec3 rgb2hsb(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 hsb2rgb(vec3 c){ + vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0), + 6.0)-3.0)-1.0, + 0.0, + 1.0 ); + rgb = rgb*rgb*(3.0-2.0*rgb); + return c.z * mix(vec3(1.0), rgb, c.y); +}