Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gewill/38e4d41570c1268d8a95dca35aa8cf0e to your computer and use it in GitHub Desktop.
Save gewill/38e4d41570c1268d8a95dca35aa8cf0e to your computer and use it in GitHub Desktop.
Support iOS and macOS
import Foundation
import SwiftUI
#if os(macOS)
import AppKit
public typealias CrossPlatformColor = NSColor
#else
import UIKit
public typealias CrossPlatformColor = UIColor
#endif
extension Color: RawRepresentable {
public init?(rawValue: String) {
guard let data = Data(base64Encoded: rawValue) else {
self = .black
return
}
do {
let color = try NSKeyedUnarchiver.unarchivedObject(ofClass: CrossPlatformColor.self, from: data) ?? .black
self = Color(color)
} catch {
self = .black
}
}
public var rawValue: String {
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: CrossPlatformColor(self), requiringSecureCoding: false) as Data
return data.base64EncodedString()
} catch {
return ""
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment