Skip to content

Instantly share code, notes, and snippets.

@hageldave
Last active November 8, 2016 12:31
Show Gist options
  • Save hageldave/d02a9062bf0691644470e8821b3bc969 to your computer and use it in GitHub Desktop.
Save hageldave/d02a9062bf0691644470e8821b3bc969 to your computer and use it in GitHub Desktop.
static int hotColdMapping(double f){
f*=Math.PI;
if(f < Math.PI/2){
float r = (float) Math.cos(f);
float g = (float) (1-Math.sin(f));
return Pixel.rgb_fromNormalized(r, g, 0);
} else {
float b = (float) -Math.cos(f);
float g = (float) (1-Math.sin(f));
return Pixel.rgb_fromNormalized(0, g, b);
}
}
static int rgbMapping(double f, int color0, int color1, int ...colors){
int[] c = new int[colors.length+2];
c[0] = color0;
c[1] = color1;
System.arraycopy(colors, 0, c, 2, colors.length);
//
int idx = (int)(f*(c.length-1));
double stepSize = 1.0/(c.length-1);
f = (f-idx*stepSize)/stepSize;
int c0 = c[idx];
int c1 = c[Math.min(idx+1, c.length-1)];
return Pixel.rgb_fromNormalized(
interpolateLinear((float)f, Pixel.r_normalized(c0), Pixel.r_normalized(c1)),
interpolateLinear((float)f, Pixel.g_normalized(c0), Pixel.g_normalized(c1)),
interpolateLinear((float)f, Pixel.b_normalized(c0), Pixel.b_normalized(c1)));
}
static float interpolateLinear(float m, float v0, float v1){
return Math.min(1, v0+m*(v1-v0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment