Skip to content

Instantly share code, notes, and snippets.

@mpneuried
Created May 22, 2014 11:53
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 mpneuried/5c893dc46c6d28b94f40 to your computer and use it in GitHub Desktop.
Save mpneuried/5c893dc46c6d28b94f40 to your computer and use it in GitHub Desktop.
Generate a color hash out of a string
hashFnv32a = (str) ->
#jshint bitwise:false
hval = 0x811c9dc5
i = 0
l = str.length
while i < l
hval ^= str.charCodeAt(i)
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24)
i++
# Convert to 8 digit hex string
return hval >>> 0
colorHash = _.memoize ( str )->
hash = hashFnv32a(str)
r = (hash & 0xFF0000) >> 16
g = (hash & 0x00FF00) >> 8
b = hash & 0x0000FF
return "#" + ("0" + r.toString(16)).substr(-2) + ("0" + g.toString(16)).substr(-2) + ("0" + b.toString(16)).substr(-2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment