I have a macOS/Swift app that I have written a preferences tabview. The preferences are being stored in UserDefaults. I have implemented a JSON export routine to export the preferences. I was using sRGBA values to store them as a String, but that does not correctly set the NSColor for the current appearance setting/vibrancy upon import. When I import the JSON the colors are wrong.
I would like to know the best way to encode NSColor to data for export to JSON and then decode upon import. This export/import method needs to preserve the actual color based on the current appearance/vibrancy when the export occurs.
I use an NSColorWell for the user to choose their color for a preference. However, I am now thinking that in order to preserve the correct color for the current appearance, I would need to restrict them to a color pallet that only represents "system" colors for the specific preferences that would be affected by an appearance change. Is that correct? I can do that, but I don't know how to preserve that in a JSON export.
I am assuming that a proper "system" color import from JSON would allow the OS/App to correctly display the color based on the currently set appearance. For example, if the user exported the preferences to JSON and the appearance at export time is "Light", but when they do an import of those preferences, and the appearance has been changed to "Dark" the colors would update accordingly. Or would I need to reload the view to pick up the new colors for the current appearance?
I'm sure I can answer the above questions if I can figure out how to preserve the correct color setting for a JSON export/import. I've tried going down the road to make NSColor Codable, but I'm having trouble with that as well. I'll keep working in that direction until I find another way.