Forked from zane-carter/Color+RawRepresentable.swift
Last active
May 14, 2023 15:27
-
-
Save gewill/38e4d41570c1268d8a95dca35aa8cf0e to your computer and use it in GitHub Desktop.
Support iOS and macOS
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
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