Skip to content

Instantly share code, notes, and snippets.

@yo1995
Created October 18, 2019 03:15
Show Gist options
  • Save yo1995/c3228317190d7d1eb332c304b166bdec to your computer and use it in GitHub Desktop.
Save yo1995/c3228317190d7d1eb332c304b166bdec to your computer and use it in GitHub Desktop.
Swift any string to HEX color string
import Foundation
func stringColorCode(_ s: String?) -> String {
guard let IDString = s else {
return "000000"
}
let hashInt = IDString.hashValue
let c = hashInt & 0xFFFFFF
let hexString = String(format:"%06x", c)
return hexString.suffix(6).uppercased()
}
print(stringColorCode(nil))
print(stringColorCode("a"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment