Created
September 10, 2024 13:35
-
-
Save weAreJack/5ad2c82db659d50d2515384b202bd315 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
import SwiftUI | |
extension CGColor { | |
static let black = CGColor(red: 0, green: 0, blue: 0, alpha: 1) | |
static let white = CGColor(red: 1, green: 1, blue: 1, alpha: 1) | |
} | |
struct CodableColor: Codable { | |
static let black = CodableColor(cgColor: CGColor.black) | |
static let white = CodableColor(cgColor: CGColor.white) | |
let colorSpace: String | |
let components: [CGFloat] | |
var cgColor: CGColor { | |
guard let colorSpace = CGColorSpace(name: colorSpace as CFString), | |
let cgColor = CGColor(colorSpace: colorSpace, components: components) else { | |
return CGColor.white | |
} | |
return cgColor | |
} | |
var color: Color { | |
Color(cgColor: cgColor) | |
} | |
enum CodingKeys: String, CodingKey { | |
case colorSpace | |
case components | |
} | |
init(cgColor: CGColor) { | |
self.colorSpace = cgColor.colorSpace?.name as? String ?? CGColorSpace.sRGB as String | |
self.components = cgColor.components ?? [] | |
} | |
init(_ color: Color.Resolved) { | |
self.init(cgColor: color.cgColor) | |
} | |
init( | |
red: CGFloat, | |
green: CGFloat, | |
blue: CGFloat, | |
opacity: CGFloat | |
) { | |
self.colorSpace = CGColorSpace.sRGB as String | |
self.components = [red, green, blue, opacity] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment