Created
December 14, 2019 11:19
-
-
Save florianbuerger/fa81174ec5c95217c08b5a0506c5be16 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public struct CodableColor: Codable { | |
public var red: CGFloat = 0 | |
public var green: CGFloat = 0 | |
public var blue: CGFloat = 0 | |
public var alpha: CGFloat = 1.0 | |
public var colorSpace: String | |
init(cgColor color: CGColor) { | |
let comps = color.components! | |
red = comps[0] | |
green = comps[1] | |
blue = comps[2] | |
if let name = color.colorSpace?.name { | |
colorSpace = name as String | |
} else { | |
colorSpace = CGColorSpace.extendedSRGB as String | |
} | |
if color.numberOfComponents == 4 { | |
alpha = comps[3] | |
} | |
} | |
var components: [CGFloat] { | |
return [red, green, blue, alpha] | |
} | |
} |
This file contains hidden or 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
let decoded = ... | |
let color = CGColor(colorSpace: decoded.colorSpace, components: decoded.components) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment