Created
October 18, 2019 03:15
-
-
Save yo1995/c3228317190d7d1eb332c304b166bdec to your computer and use it in GitHub Desktop.
Swift any string to HEX color 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
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