Saving Color from UIColorPickerViewController with UserDefaults

I've been trying to save a selected color with UserDefaults from UIColorPickerViewController. But I run into a color space fiasco. Anyway, here come my lines of code.

class ViewController: UIViewController, UIColorPickerViewControllerDelegate {
	@IBOutlet weak var imageView: UIImageView!	
	
	@IBAction func selectTapped(_ sender: UIButton) {
		let picker = UIColorPickerViewController()
		picker.delegate = self
		picker.selectedColor = .yellow
		picker.supportsAlpha = false
		present(picker, animated: true, completion: nil)
	}

	override func viewDidLoad() {
		super.viewDidLoad()
	}
	
	override func viewDidAppear(_ animated: Bool) {
		super.viewDidAppear(animated)
		
		if let color = UserDefaultsUIColor.shared.readColor(key: "MyColor") {
			print("Color being read: \(color)")
		}
	}
	
	func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
		let color = viewController.selectedColor
		print("Selected color: \(color)")
		UserDefaultsUIColor.shared.saveColor(color: viewController.selectedColor, key: "MyColor")
	}
	
	func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
		imageView.backgroundColor = viewController.selectedColor
	}
}

class UserDefaultsUIColor {
	static let shared = UserDefaultsUIColor()
	
	func saveColor(color: UIColor, key: String) {
		let userDefaults = UserDefaults.standard
		do {
			let data = try NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: false) as NSData?
			userDefaults.set(data, forKey: key)
		} catch {
			print("Error UserDefaults: \(error.localizedDescription)")
		}
	}
	
	func readColor(key: String) -> UIColor? {
		let userDefaults = UserDefaults.standard
		if let data = userDefaults.data(forKey: key) {
			do {
				if let color = try NSKeyedUnarchiver.unarchivedObject(ofClass: UIColor.self, from: data) {
					return color
				}
			} catch {
				print("Error UserDefaults")
			}
		}
		return nil
	}
}

I first start out with a yellow color (UIColor.yellow). And I select a color whose RGB values are 76, 212, 158, respectively. And the color picker guy returns the following.

kCGColorSpaceModelRGB 0.298039 0.831373 0.619608 1

And I get the following in reading the saved color data object.

UIExtendedSRGBColorSpace -0.270778 0.84506 0.603229 1

How can I save and read color data objects consistently? I could specify a color space when I save a color. But it doesn't go well.

Muchos thankos

Señor Tomato de Source

Saving Color from UIColorPickerViewController with UserDefaults
 
 
Q