Last active
August 21, 2020 06:01
-
-
Save foxicode/28f1a32b3becf5b7ae249ba19cd0340e to your computer and use it in GitHub Desktop.
Swift extension to convert String/Int to UIColor/NSColor and back
This file contains 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
#if os(iOS) || os(tvOS) | |
import UIKit | |
typealias UniColor = UIColor | |
#else | |
import Cocoa | |
typealias UniColor = NSColor | |
#endif | |
private extension Int { | |
func duplicate4bits() -> Int { | |
return (self << 4) + self | |
} | |
} | |
extension UniColor { | |
convenience init?(hexString: String) { | |
self.init(hexString: hexString, alpha: 1.0) | |
} | |
fileprivate convenience init?(hex3: Int, alpha: Float) { | |
self.init(red: CGFloat( ((hex3 & 0xF00) >> 8).duplicate4bits() ) / 255.0, | |
green: CGFloat( ((hex3 & 0x0F0) >> 4).duplicate4bits() ) / 255.0, | |
blue: CGFloat( ((hex3 & 0x00F) >> 0).duplicate4bits() ) / 255.0, | |
alpha: CGFloat(alpha)) | |
} | |
fileprivate convenience init?(hex6: Int, alpha: Float) { | |
self.init(red: CGFloat( (hex6 & 0xFF0000) >> 16 ) / 255.0, | |
green: CGFloat( (hex6 & 0x00FF00) >> 8 ) / 255.0, | |
blue: CGFloat( (hex6 & 0x0000FF) >> 0 ) / 255.0, alpha: CGFloat(alpha)) | |
} | |
convenience init?(hexString: String, alpha: Float) { | |
var hex = hexString | |
if hex.hasPrefix("#") { | |
hex = String(hex[hex.index(after: hex.startIndex)...]) | |
} | |
guard let hexVal = Int(hex, radix: 16) else { | |
self.init() | |
return nil | |
} | |
switch hex.count { | |
case 3: self.init(hex3: hexVal, alpha: alpha) | |
case 6: self.init(hex6: hexVal, alpha: alpha) | |
default: self.init() | |
return nil | |
} | |
} | |
convenience init?(hex: Int) { | |
self.init(hex: hex, alpha: 1.0) | |
} | |
convenience init?(hex: Int, alpha: Float) { | |
if (0x000000 ... 0xFFFFFF) ~= hex { | |
self.init(hex6: hex, alpha: alpha) | |
} else { | |
self.init() | |
return nil | |
} | |
} | |
} | |
extension String { | |
func toColor() -> UniColor? { | |
UniColor(hexString: self) | |
} | |
} | |
extension Int { | |
func toColor(alpha: Float = 1.0) -> UniColor? { | |
UniColor(hex: self, alpha: alpha) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment