Skip to content

Instantly share code, notes, and snippets.

@sauvikatinnofied
Last active July 3, 2020 14:03
Show Gist options
  • Save sauvikatinnofied/8a48c5d9eb3175f8f13f356bd47c606c to your computer and use it in GitHub Desktop.
Save sauvikatinnofied/8a48c5d9eb3175f8f13f356bd47c606c to your computer and use it in GitHub Desktop.
MediumBlog Color Constant
extension UIColor {
/**
Creates an UIColor from HEX String in "#363636" format
- parameter hexString: HEX String in "#363636" format
- returns: UIColor from HexString
*/
convenience init(hexString: String) {
let hexString: String = (hexString as NSString).trimmingCharacters(in: .whitespacesAndNewlines)
let scanner = Scanner(string: hexString as String)
if hexString.hasPrefix("#") {
scanner.scanLocation = 1
}
var color: UInt32 = 0
scanner.scanHexInt32(&color)
let mask = 0x000000FF
let r = Int(color >> 16) & mask
let g = Int(color >> 8) & mask
let b = Int(color) & mask
let red = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
self.init(red:red, green:green, blue:blue, alpha:1)
}
/**
Creates an UIColor Object based on provided RGB value in integer
- parameter red: Red Value in integer (0-255)
- parameter green: Green Value in integer (0-255)
- parameter blue: Blue Value in integer (0-255)
- returns: UIColor with specified RGB values
*/
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
}
@dx777
Copy link

dx777 commented Jul 3, 2020

'scanLocation' was deprecated in iOS 13.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment