Skip to content

Instantly share code, notes, and snippets.

@JKring
Created December 18, 2013 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JKring/8016152 to your computer and use it in GitHub Desktop.
Save JKring/8016152 to your computer and use it in GitHub Desktop.
colors!!
var colorUtils = {
existingColors: {},
red: [5.5, 0.765, 0.80],
convertHSVtoRGB: function(h, s, v) {
var r, g, b, i, f, p, q, t;
h = h / 360;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return "rgb(" + [Math.floor(r * 255), Math.floor(g * 255), Math.floor(b * 255)].join(",") + ")";
},
};
function getColorForKey(key){
if (!colorUtils.existingColors[key]){
colorUtils.existingColors[key] = colorUtils.convertHSVtoRGB(parseInt(Math.random() * 255), colorUtils.red[1], colorUtils.red[2]);
}
return colorUtils.existingColors[key];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment