Last active
June 12, 2018 15:19
-
-
Save mikesteele/2b9bd8aec73bb901f8bc to your computer and use it in GitHub Desktop.
Hash string to UIColor in Swift
This file contains hidden or 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
func hashStringToColor(string: String) -> UIColor { | |
var hash: Int = string.hashValue | |
var r: Int = (hash & 0xFF0000) >> 16 | |
var g: Int = (hash & 0x00FF00) >> 8 | |
var b: Int = (hash & 0x0000FF) | |
return rgbaToUIColor(r: r, g: g, b: b, a: 1.0) | |
} | |
func rgbaToUIColor(#r: Int, #g: Int, #b: Int, #a: Float) -> UIColor { | |
let floatRed = CGFloat(r) / 255.0 | |
let floatGreen = CGFloat(g) / 255.0 | |
let floatBlue = CGFloat(b) / 255.0 | |
return UIColor(red: floatRed, green: floatGreen, blue: floatBlue, alpha: 1.0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment