Created
April 15, 2011 13:50
-
-
Save bxt/921725 to your computer and use it in GitHub Desktop.
Processing sketch (Java-like) to display and experiment with various gradient methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int barWidth=50; | |
GradientI[] grads=new GradientI[4]; | |
void setup() { | |
size(1600, 300); | |
colorMode(HSB, 360, 100, 1.0); | |
noStroke(); | |
background(0); | |
grads[0]=new LiearGradient(); /* lin */ | |
grads[1]=new LiearGradient(1.1); /* clip_a */ | |
grads[2]=new CirculatGradient(); /* f(x) */ | |
grads[3]=new LiearGradient(1.2); /* clip_b */ | |
} | |
void draw() { | |
int start=0; | |
int step=height/grads.length; | |
for (int gid=0;gid<grads.length;gid++) { | |
for (int i=0;i<width;i+=barWidth) { | |
fill(30, 100, grads[gid].perentageDark((float)i/(float)width) ); | |
rect(i, start, barWidth, start+step); | |
} | |
start+=step; | |
} | |
} | |
/** | |
* Simple linar function, optionally clipped | |
*/ | |
class LiearGradient implements GradientI { | |
float clipping; | |
LiearGradient() { | |
this(1.0); | |
} | |
LiearGradient(float clipping) { | |
this.clipping=clipping; | |
} | |
float perentageDark(float percentageAdvanced) { | |
return max(0.0,min(1.0, clipping*(percentageAdvanced-(1-1/clipping)/2))); | |
} | |
} | |
/** | |
* Shadow gradient of circular lights | |
*/ | |
class CirculatGradient implements GradientI { | |
float perentageDark(float percentageAdvanced) { | |
return (2*sqrt(-(percentageAdvanced-1)*percentageAdvanced)*(2*percentageAdvanced-1)+acos(1-2*percentageAdvanced))/PI; | |
} | |
} | |
/** | |
* Gradient Interface | |
*/ | |
interface GradientI { | |
/** | |
* Calculate the mixing factor | |
*/ | |
float perentageDark(float percentageAdvanced); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment