Skip to content

Instantly share code, notes, and snippets.

@jeshuamaxey
Last active November 23, 2015 23:16
Show Gist options
  • Save jeshuamaxey/8a64af974e2b428d9e33 to your computer and use it in GitHub Desktop.
Save jeshuamaxey/8a64af974e2b428d9e33 to your computer and use it in GitHub Desktop.
takes a value from 0-1 and returns an rgb color string
/**
* takes a value from 0-1 and returns an rgb color string
*/
function floatToColor(val) {
var c = Math.floor(5*255*val);
var r = 255, g = 255, b = 255;
while(c > 0) {
if(r!=0 && g==255 && b==255) r--; //rgb(255,255,255) -> rgb(0,255,255)
if(r==0 && g==255 && b!=0) b--; //rgb(0,255,0) -> rgb(0,255,0)
if(r!=255 && g==255 && b==0) r++; //rgb(0,255,0) -> rgb(255,255,0)
if(r==255 && g!=0 && b==0) g--; //rgb(255,255,0) -> rgb(255,0, 0)
if(r==255 && g==0 && b!=255) b++; //rgb(255,0,0) -> rgb(255,0,255)
c--;
}
var color = 'rgb(' + r + ',' + g + ',' + b + ')';
return color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment