@@ -22,14 +22,17 @@ import AppKit
2222#endif
2323
2424/// This extension makes `Color` implement `Codable`.
25- extension Color : Codable {
26-
25+ extension Color : @retroactive Decodable { }
26+ extension Color : @retroactive Encodable { }
27+
28+ public extension Color {
29+
2730 enum CodingKeys : String , CodingKey {
2831 case red, green, blue, alpha
2932 }
3033
3134 /// Initialize a color value from a decoder.
32- public init ( from decoder: Decoder ) throws {
35+ init ( from decoder: Decoder ) throws {
3336 let container = try decoder. container ( keyedBy: CodingKeys . self)
3437 let r = try container. decode ( Double . self, forKey: . red)
3538 let g = try container. decode ( Double . self, forKey: . green)
@@ -40,10 +43,9 @@ extension Color: Codable {
4043
4144 /// Encode the color, using an encoder.
4245 ///
43- /// Note that encoding colors that support features like
44- /// dark mode, high contrast etc. will cause the encoded
45- /// colors to only contain the current color information.
46- public func encode( to encoder: Encoder ) throws {
46+ /// > Important: Encoding colors that support system features like dark mode,
47+ /// high contrast etc. will cause the encoded colors to be non-dynamic.
48+ func encode( to encoder: Encoder ) throws {
4749 guard let colorComponents = self . colorComponents else { return }
4850 var container = encoder. container ( keyedBy: CodingKeys . self)
4951 try container. encode ( colorComponents. red, forKey: . red)
@@ -69,13 +71,8 @@ private extension Color {
6971
7072 #if os(macOS)
7173 SystemColor ( self ) . getRed ( & r, green: & g, blue: & b, alpha: & a)
72- // Note that non RGB color will raise an exception, that I don't now how to catch because it is an Objc exception.
7374 #else
74- guard SystemColor ( self ) . getRed ( & r, green: & g, blue: & b, alpha: & a) else {
75- // Pay attention that the color should be convertible into RGB format
76- // Colors using hue, saturation and brightness won't work
77- return nil
78- }
75+ guard SystemColor ( self ) . getRed ( & r, green: & g, blue: & b, alpha: & a) else { return nil }
7976 #endif
8077
8178 return ( r, g, b, a)
0 commit comments