Skip to content

Instantly share code, notes, and snippets.

@adammagana
Created April 4, 2016 14:05
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 adammagana/7f258739a9c4a6927ce717496fb7a42a to your computer and use it in GitHub Desktop.
Save adammagana/7f258739a9c4a6927ce717496fb7a42a to your computer and use it in GitHub Desktop.
(function () {
'use strict';
var colorList = [
'#da1f2d',
'#00a550',
'#00acdc',
'#f37720',
'#fbaf17',
'#5c5da9',
'#c74398',
'#00ae9d',
'#085866',
'#3d5958',
'#8cc63e',
'#633a1c'
],
colorCache = {},
colorIndex = 0;
function clearKeyAndColorCache() {
colorCache = {};
}
function getAndCreateColorForKey(authorId) {
if (colorCache.hasOwnProperty(authorId) === false) {
colorCache[authorId] = colorList[colorIndex];
if (colorList.length - 1 === colorIndex) {
colorIndex = 0;
} else {
colorIndex += 1;
}
}
return colorCache[authorId];
}
module.exports = {
clearKeyAndColorCache: clearKeyAndColorCache,
getAndCreateColorForKey: getAndCreateColorForKey
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment