How to use hex color values How to use hex color values ios ios

How to use hex color values


#ffffff are actually 3 color components in hexadecimal notation - red ff, green ff and blue ff. You can write hexadecimal notation in Swift using 0x prefix, e.g 0xFF

To simplify the conversion, let's create an initializer that takes integer (0 - 255) values:

extension UIColor {   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)   }   convenience init(rgb: Int) {       self.init(           red: (rgb >> 16) & 0xFF,           green: (rgb >> 8) & 0xFF,           blue: rgb & 0xFF       )   }}

Usage:

let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF)let color2 = UIColor(rgb: 0xFFFFFF)

How to get alpha?

Depending on your use case, you can simply use the native UIColor.withAlphaComponent method, e.g.

let semitransparentBlack = UIColor(rgb: 0x000000).withAlphaComponent(0.5)

Or you can add an additional (optional) parameter to the above methods:

convenience init(red: Int, green: Int, blue: Int, a: CGFloat = 1.0) {    self.init(        red: CGFloat(red) / 255.0,        green: CGFloat(green) / 255.0,        blue: CGFloat(blue) / 255.0,        alpha: a    )}convenience init(rgb: Int, a: CGFloat = 1.0) {    self.init(        red: (rgb >> 16) & 0xFF,        green: (rgb >> 8) & 0xFF,        blue: rgb & 0xFF,        a: a    )}

(we cannot name the parameter alpha because of a name collision with the existing initializer).

Called as:

let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF, a: 0.5)let color2 = UIColor(rgb: 0xFFFFFF, a: 0.5)

To get the alpha as an integer 0-255, we can

convenience init(red: Int, green: Int, blue: Int, a: Int = 0xFF) {    self.init(        red: CGFloat(red) / 255.0,        green: CGFloat(green) / 255.0,        blue: CGFloat(blue) / 255.0,        alpha: CGFloat(a) / 255.0    )}// let's suppose alpha is the first component (ARGB)convenience init(argb: Int) {    self.init(        red: (argb >> 16) & 0xFF,        green: (argb >> 8) & 0xFF,        blue: argb & 0xFF,        a: (argb >> 24) & 0xFF    )}

Called as

let color = UIColor(red: 0xFF, green: 0xFF, blue: 0xFF, a: 0xFF)let color2 = UIColor(argb: 0xFFFFFFFF)

Or a combination of the previous methods. There is absolutely no need to use strings.


This is a function that takes a hex string and returns a UIColor.
(You can enter hex strings with either format: #ffffff or ffffff)

Usage:

var color1 = hexStringToUIColor("#d3d3d3")

Swift 5: (Swift 4+)

func hexStringToUIColor (hex:String) -> UIColor {    var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()    if (cString.hasPrefix("#")) {        cString.remove(at: cString.startIndex)    }    if ((cString.count) != 6) {        return UIColor.gray    }    var rgbValue:UInt64 = 0    Scanner(string: cString).scanHexInt64(&rgbValue)    return UIColor(        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,        alpha: CGFloat(1.0)    )}

Swift 3:

func hexStringToUIColor (hex:String) -> UIColor {    var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()    if (cString.hasPrefix("#")) {        cString.remove(at: cString.startIndex)    }    if ((cString.characters.count) != 6) {        return UIColor.gray    }    var rgbValue:UInt32 = 0    Scanner(string: cString).scanHexInt32(&rgbValue)    return UIColor(        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,        alpha: CGFloat(1.0)    )}

Swift 2:

func hexStringToUIColor (hex:String) -> UIColor {    var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet() as NSCharacterSet).uppercaseString    if (cString.hasPrefix("#")) {      cString = cString.substringFromIndex(cString.startIndex.advancedBy(1))    }    if ((cString.characters.count) != 6) {      return UIColor.grayColor()    }    var rgbValue:UInt32 = 0    NSScanner(string: cString).scanHexInt(&rgbValue)    return UIColor(        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,        alpha: CGFloat(1.0)    )}



Source: arshad/gist:de147c42d7b3063ef7bc

Edit: Updated the code. Thanks, Hlung, jaytrixz, Ahmad F, Kegham K, and Adam Waite!


Swift 5 (Swift 4, Swift 3) UIColor extension:

extension UIColor {    convenience init(hexString: String) {        let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)        var int = UInt64()        Scanner(string: hex).scanHexInt64(&int)        let a, r, g, b: UInt64        switch hex.count {        case 3: // RGB (12-bit)            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)        case 6: // RGB (24-bit)            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)        case 8: // ARGB (32-bit)            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)        default:            (a, r, g, b) = (255, 0, 0, 0)        }        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)    }}

Usage:

let darkGrey = UIColor(hexString: "#757575")

Swift 2.x version:

extension UIColor {    convenience init(hexString: String) {        let hex = hexString.stringByTrimmingCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet)        var int = UInt32()        NSScanner(string: hex).scanHexInt(&int)        let a, r, g, b: UInt32        switch hex.characters.count {        case 3: // RGB (12-bit)            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)        case 6: // RGB (24-bit)            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)        case 8: // ARGB (32-bit)            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)        default:            (a, r, g, b) = (255, 0, 0, 0)        }        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)    }}