Skip to content

Instantly share code, notes, and snippets.

@alexj70
Last active April 27, 2017 21:28
Show Gist options
  • Save alexj70/dc1d74dac0f0bc68f894f476d799f16c to your computer and use it in GitHub Desktop.
Save alexj70/dc1d74dac0f0bc68f894f476d799f16c to your computer and use it in GitHub Desktop.
UIColor from hex

UIColor

enum ColorType: UInt32 {
    case tint = 0x005054ff
    case background = 0xF5F5F2ff
   
   var color: UIColor { return UIColor(type: self) }
}

extension UIColor {
    convenience init(type: ColorType) {
        self.init(rgbaValue: type.rawValue)
    }

    convenience init(rgbaValue: UInt32) {
        let red   = CGFloat((rgbaValue >> 24) & 0xff) / 255.0
        let green = CGFloat((rgbaValue >> 16) & 0xff) / 255.0
        let blue  = CGFloat((rgbaValue >>  8) & 0xff) / 255.0
        let alpha = CGFloat((rgbaValue      ) & 0xff) / 255.0
        
        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
}

Using

let color1 = UIColor(type: .background)
let color2 = ColorType.background.color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment