Skip to content

Instantly share code, notes, and snippets.

@csytan
Created May 11, 2009 06:35
Show Gist options
  • Save csytan/109887 to your computer and use it in GitHub Desktop.
Save csytan/109887 to your computer and use it in GitHub Desktop.
random tag colors
function hsv_to_rgb(h, s, v) {
// http://nofunc.org/Color_Conversion_Library/
var R, G, A, B, C, S=s/255, V=v/255, H=h/255;
if(S>0) {
if(H>=1) H=0;
H=6*H; F=H-Math.floor(H);
A=Math.round(255*V*(1-S));
B=Math.round(255*V*(1-(S*F)));
C=Math.round(255*V*(1-(S*(1-F))));
V=Math.round(255*V);
switch(Math.floor(H)) {
case 0: R=V; G=C; B=A; break;
case 1: R=B; G=V; B=A; break;
case 2: R=A; G=V; B=C; break;
case 3: R=A; G=B; B=V; break;
case 4: R=C; G=A; B=V; break;
case 5: R=V; G=A; B=B; break;
}
return({r:R?R:0, g:G?G:0, b:B?B:0, a:1});
}
else return({r:(V=Math.round(V*255)), g:V, b:V, a:1});
};
rgb = hsv_to_rgb(202,48,191);
$(function(){
$('a.tag').each(function(){
var h = Math.min($(this).text().length * 25, 255);
var rgb = hsv_to_rgb(h,48,191);
$(this).css('background-color',
'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b+ ')');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment