I wanted to be able to consistently label an arbitrary number of objects on my site. It's pretty simple, but I if there's a more clever way...
// Usage:
// ColorTable.getColor('my link name');
ColorTable = {
'_colors' : [
'#F4BB2E',
'#2DE656',
'#66CCFF',
'#6666FF',
'#CC66FF',
'#FF6FCF',
'#FC4048'
],
'tableSize' : function() { return this['_colors'].length; },
'nextIndex' : function() {
if (localStorage["ColorTable:_index"]===undefined) {
localStorage["ColorTable:_index"] = -1;
}
rv = (parseInt(localStorage["ColorTable:_index"]) + 1) % this.tableSize();
localStorage["ColorTable:_index"] = rv;
return rv;
},
'getIndex' : function(key) {
if (!localStorage["ColorTable:" + key]) {
localStorage["ColorTable:" + key] = this.nextIndex();
}
return localStorage["ColorTable:" + key];
},
'setIndex' : function(key, index) {
if (index >= 0 && index < this.tableLength()) {
localStorage["ColorTable:" + key] = index;
return localStorage["ColorTable:" + key];
} else {
return false;
}
},
'getColor' : function(key) {
return this['_colors'][this.getIndex(key)];
}
};