Javascript String toColor()

Description

Javascript String toColor()


String.prototype.toColor = function () {
  // str to hash//  w w w.ja  va 2  s .  c  om
  for (var i = 0, hash = 0; i < this.length; hash = this.charCodeAt(i++) + ((hash << 5) - hash))

  // int/hash to hex
  for (var i = 0, color = '#'; i < 3; colour += ('00' + ((hash >> i++ * 8) & 0xFF).toString(16)).slice(-2))

  return color
}



PreviousNext

Related