Last active
July 30, 2019 22:06
-
-
Save edudnyk/d8256e75481a4643b2a0c50da8170186 to your computer and use it in GitHub Desktop.
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
extension UIColor : LKNSDictionaryCoding { | |
static func lk_dictEncode(object: AnyObject?)->NSMutableDictionary { | |
let color = object as? UIColor | |
var red: CGFloat = 0, | |
green: CGFloat = 0, | |
blue: CGFloat = 0, | |
alpha: CGFloat = 0 | |
if color?.getRed(&red, | |
green: &green, | |
blue: &blue, | |
alpha: &alpha) == false { | |
_ = color?.getWhite(&red, alpha:&alpha) | |
green = red | |
blue = red | |
} | |
let result = NSMutableDictionary(sharedKeySet: | |
NSMutableDictionary.sharedKeySet(forKeys: [ | |
NSString("r"), | |
NSString("g"), | |
NSString("b"), | |
NSString("a") | |
])) | |
result["r"] = red | |
result["g"] = green | |
result["b"] = blue | |
result["a"] = alpha | |
return result | |
} | |
static func lk_dictDecode(dictionaryRepresentation dictionary: NSDictionary?)->Self { | |
var red : CGFloat = 0, green : CGFloat = 0, blue : CGFloat = 0, alpha: CGFloat = 0 | |
if let redValue = dictionary?["r"] as? CGFloat { | |
red = redValue | |
} | |
if let greenValue = dictionary?["g"] as? CGFloat { | |
green = greenValue | |
} | |
if let blueValue = dictionary?["b"] as? CGFloat { | |
blue = blueValue | |
} | |
if let alphaValue = dictionary?["a"] as? CGFloat { | |
alpha = alphaValue | |
} | |
return Self(red: red, green: green, blue: blue, alpha: alpha) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment