Created
May 12, 2014 10:46
-
-
Save yi/9151fdd93883c7b7f106 to your computer and use it in GitHub Desktop.
JS: generate a color value from arbitery given string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hashCode(str) { // java String#hashCode | |
var hash = 0; | |
for (var i = 0; i < str.length; i++) { | |
hash = str.charCodeAt(i) + ((hash << 5) - hash); | |
} | |
return hash; | |
} | |
function intToARGB(i){ | |
var resualt = "" | |
var str = ((i>>24)&0xFF).toString(16); | |
if(str.length < 2)str = "0"+str; | |
resualt += str; | |
str = ((i>>16)&0xFF).toString(16); | |
if(str.length < 2)str = "0"+str; | |
resualt += str; | |
str = ((i>>8)&0xFF).toString(16); | |
if(str.length < 2)str = "0"+str; | |
resualt += str; | |
return resualt; | |
} | |
var color = intToARGB(hashCode("xwr中文opoops怎发发么样?")) | |
$("#koko").css("background-color", "#"+color) | |
$("#koko").text(color) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment