Persisting Color from ColorPicker

Has anyone successfully persisted Color, particularly in SwiftData? So far my attempts have failed:

  1. Making Color conform to Codable results in a run time error (from memory something about ColorBox).
  2. Color.Resolved already conforms Codable but this results in "SwiftData/ModelCoders.swift:124: Fatal error: Composite Coder only supports Keyed Container"

None of the other color types conform to Codable (CGColor, NSColor and UIColor) so does the swift language really not have a persistable color type?

Replies

You could create a class that has the component elements of the color as properties and have convenience propertie(s) that return a color using the stored properties.

Save it as a String and add the Hex color #

  • Yeah I considered saving it as a hex string the problem is ColorPicker can have either sRGB or D3 Display color spaces as input (D3 is essentially a superset of sRGB). Color.Resolved produces negative float values for colors outside of sRGB space and I assume would require a longer hex string when encoding them.

Add a Comment