bennyguitar / Colours

A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.
MIT License
3.09k stars 300 forks source link

Convert from hex string! #30

Closed dcflow closed 9 years ago

dcflow commented 9 years ago

One of these has an issue:

because I get different colors.

ex. Color(hex: Color.successColor().hexString()) -> returns blue

bennyguitar commented 9 years ago

It looks like the init method is borked somehow. Good find and I'll try and fix it this weekend.

dcflow commented 9 years ago

I wonder why they call them bugs? :P

convenience init(hex: String) {
    var rgbInt: UInt64 = 0
    let newHex: NSString = NSString(string: hex).stringByReplacingOccurrencesOfString("#", withString: "")
    var scanner = NSScanner(string: newHex)
    scanner.scanHexLongLong(&rgbInt)
    let r: CGFloat = CGFloat((rgbInt & 0xFF0000) >> 16)/255.0
    let g: CGFloat = CGFloat((rgbInt & 0x00FF00) >> 8)/255.0
    let b: CGFloat = CGFloat(rgbInt & 0x0000FF)/255.0
    self.init(red: r, green: g, blue: b, alpha: 1.0)
}