Skip to content

Instantly share code, notes, and snippets.

@behreajj
Last active October 30, 2019 15:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behreajj/d75b88d2a29d2819fbe4f326cb04f9f5 to your computer and use it in GitHub Desktop.
Save behreajj/d75b88d2a29d2819fbe4f326cb04f9f5 to your computer and use it in GitHub Desktop.
Color Gradient V2_33
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
uniform float step;
uniform vec2 origin;
uniform vec2 destination;
uniform vec2 center;
uniform vec2 dimensions;
uniform vec4 acolor;
uniform vec4 bcolor;
vec4 hsbToRgb(vec4 clr) {
vec4 rslt = vec4(0.0, 0.0, 0.0, clr.w);
if(clr.y == 0.0) {
rslt.x = rslt.y = rslt.z = clr.z;
} else {
float hue = clr.x * 6.0;
float sector = floor(hue);
float tint1 = clr.z * (1.0 - clr.y);
float tint2 = clr.z * (1.0 - clr.y * (hue - sector));
float tint3 = clr.z * (1.0 - clr.y * (1.0 + sector - hue));
if(sector == 1.0) {
rslt.x = tint2; rslt.y = clr.z; rslt.z = tint1;
} else if(sector == 2.0) {
rslt.x = tint1; rslt.y = clr.z; rslt.z = tint3;
} else if(sector == 3.0) {
rslt.x = tint1; rslt.y = tint2; rslt.z = clr.z;
} else if(sector == 4.0) {
rslt.x = tint3; rslt.y = tint1; rslt.z = clr.z;
} else if(sector == 5.0) {
rslt.x = clr.z; rslt.y = tint1; rslt.z = tint2;
} else {
rslt.x = clr.z; rslt.y = tint3; rslt.z = tint1;
}
}
return rslt;
}
vec4 rgbToHsb(vec4 clr) {
float max = clr.x > clr.y ?
clr.x > clr.z ?
clr.x : clr.z :
clr.y > clr.z ?
clr.y : clr.z;
float min = clr.x < clr.y ?
clr.x < clr.z ?
clr.x : clr.z :
clr.y < clr.z ?
clr.y : clr.z;
float delta = max - min;
float hue = 0.0;
if (delta != 0.0) {
if (clr.x == max) {
hue = (clr.y - clr.z) / delta;
} else if (clr.y == max) {
hue = 2.0 + (clr.z - clr.x) / delta;
} else {
hue = 4.0 + (clr.x - clr.y) / delta;
}
hue /= 6.0;
if (hue < 0.0) {
hue += 1.0;
}
}
return vec4(hue, max == 0.0 ? 0.0 : (max - min) / max, max, clr.w);
}
float project(vec2 origin, vec2 dest, vec2 point) {
vec2 od = dest - origin;
vec2 op = point - origin;
return clamp(dot(op, od) / dot(od, od), 0.0, 1.0);
}
float radial(vec2 coord, vec2 center, vec2 dimensions, float damping) {
return length(center - coord) / length(dimensions) / damping;
}
float smootherStep(float t) {
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
}
vec4 smootherStepHsb(vec4 a, vec4 b, float t) {
if(t <= 0.0) { return a; }
else if(t >= 1.0) { return b; }
vec4 c = rgbToHsb(a);
vec4 d = rgbToHsb(b);
float delta = d.x - c.x;
if(delta < -0.5) { d.x += 1.0; }
else if(delta > 0.5) { c.x += 1.0; }
vec4 e = c + ((d - c) * smootherStep(t));
e.x = mod(e.x, 1.0);
return hsbToRgb(e);
}
vec4 smootherStepRgb(vec4 a, vec4 b, float t) {
if(t <= 0.0) { return a; }
else if(t >= 1.0) { return b; }
return a + ((b - a) * smootherStep(t));
}
void main(void) {
/*gl_FragColor = smootherStepRgb(acolor, bcolor,*/
gl_FragColor = smootherStepHsb(acolor, bcolor,
step * radial(gl_FragCoord.xy, center, dimensions, .25));
/*project(origin, destination, gl_FragCoord.xy));*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment