Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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