Skip to content

Instantly share code, notes, and snippets.

@ruiwen
Created August 6, 2013 09:33
Show Gist options
  • Save ruiwen/6163115 to your computer and use it in GitHub Desktop.
Save ruiwen/6163115 to your computer and use it in GitHub Desktop.
function rainbow(numOfSteps, step) {
// based on http://stackoverflow.com/a/7419630
// This function generates vibrant, "evenly spaced" colours (i.e. no clustering). This is ideal for creating easily distiguishable vibrant markers in Google Maps and other apps.
// Adam Cole, 2011-Sept-14
// HSV to RBG adapted from: http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
var r, g, b;
var h = step / numOfSteps;
var i = ~~(h * 6);
var f = h * 6 - i;
var q = 1 - f;
switch(i % 6){
case 0: r = 1, g = f, b = 0; break;
case 1: r = q, g = 1, b = 0; break;
case 2: r = 0, g = 1, b = f; break;
case 3: r = 0, g = q, b = 1; break;
case 4: r = f, g = 0, b = 1; break;
case 5: r = 1, g = 0, b = q; break;
}
var c = "#" + ("00" + (~ ~(r * 235)).toString(16)).slice(-2) + ("00" + (~ ~(g * 235)).toString(16)).slice(-2) + ("00" + (~ ~(b * 235)).toString(16)).slice(-2);
return (c);
}
@RC0D3
Copy link

RC0D3 commented Oct 4, 2021

awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment