Skip to content

Instantly share code, notes, and snippets.

@3ki5tj
Last active August 29, 2015 14:10
Show Gist options
  • Save 3ki5tj/fcdd096c5ffa918f113a to your computer and use it in GitHub Desktop.
Save 3ki5tj/fcdd096c5ffa918f113a to your computer and use it in GitHub Desktop.
Random color
function random_color(cmin, cmax)
{
var x = Math.random() * 6;
var i = Math.floor( x ), r = 0, g = 0, b = 0;
if ( cmin == undefined || cmin == null ) cmin = 0;
if ( cmax == undefined || cmax == null ) cmax = 255;
var cvar = cmax - cmin + 1;
x -= i;
if ( i < 1 ) { // red to yellow
r = cmax;
g = cmin + Math.floor( cvar * x );
} else if ( i < 2 ) { // yellow to green
r = cmin + Math.floor( cvar * (1 - x) );
g = cmax;
} else if ( i < 3 ) { // green to cyan
g = cmax;
b = cmin + Math.floor( cvar * x );
} else if ( i < 4 ) { // cyan to blue
g = cmin + Math.floor( cvar * (1 - x) );
b = cmax;
} else if ( i < 5 ) { // blue to magenta
b = cmax;
r = cmin + Math.floor( cvar * x );
} else {
b = cmin + Math.floor( cvar * (1 - x) );
r = cmax;
}
return [r, g, b];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment