@@ -50,7 +50,7 @@ public List< LightnessPair > Lightnesses
5050 set ;
5151 }
5252
53- public ColorPair ( ) : this ( null )
53+ public ColorPair ( ) : this ( null , null )
5454 { }
5555
5656 public ColorPair ( SolidColorBrush color ) : this ( color , null )
@@ -62,5 +62,137 @@ public ColorPair( SolidColorBrush color, SolidColorBrush variant )
6262 this . Variant = variant ;
6363 this . Lightnesses = new List < LightnessPair > ( ) ;
6464 }
65+
66+ public ColorPair ( Dictionary < string , object > dictionary ) : this ( )
67+ {
68+ if ( dictionary == null )
69+ {
70+ throw new ArgumentException ( ) ;
71+ }
72+
73+ {
74+ if ( dictionary . TryGetValue ( "color" , out object o ) == false || ! ( o is Dictionary < string , object > dict ) )
75+ {
76+ throw new ArgumentException ( ) ;
77+ }
78+
79+ this . Color = this . ColorFromDictionary ( dict ) ;
80+ }
81+
82+ {
83+ if ( dictionary . TryGetValue ( "variant" , out object o ) == false || ! ( o is Dictionary < string , object > dict ) )
84+ {
85+ throw new ArgumentException ( ) ;
86+ }
87+
88+ this . Variant = this . ColorFromDictionary ( dict ) ;
89+ }
90+
91+ {
92+ if ( dictionary . TryGetValue ( "lightnesses" , out object o ) == false || ! ( o is List < Dictionary < string , object > > list ) )
93+ {
94+ throw new ArgumentException ( ) ;
95+ }
96+
97+ foreach ( Dictionary < string , object > l in list )
98+ {
99+ try
100+ {
101+ this . Lightnesses . Add ( new LightnessPair ( l ) ) ;
102+ }
103+ catch
104+ { }
105+ }
106+ }
107+ }
108+
109+ public Dictionary < string , object > ToDictionary ( )
110+ {
111+ Dictionary < string , object > dict = new Dictionary < string , object >
112+ {
113+ { "color" , this . ColorToDictionary ( this . Color ) } ,
114+ { "variant" , this . ColorToDictionary ( this . Variant ) }
115+ } ;
116+
117+ List < Dictionary < string , object > > lightnesses = new List < Dictionary < string , object > > ( ) ;
118+
119+ foreach ( LightnessPair l in this . Lightnesses )
120+ {
121+ lightnesses . Add ( l . ToDictionary ( ) ) ;
122+ }
123+
124+ dict [ "lightnesses" ] = lightnesses ;
125+
126+ return dict ;
127+ }
128+
129+ private Dictionary < string , object > ColorToDictionary ( SolidColorBrush color )
130+ {
131+ if ( color == null )
132+ {
133+ return null ;
134+ }
135+
136+ ColorExtensions . HSLComponents hsl = color . Color . GetHSL ( ) ;
137+
138+ return new Dictionary < string , object >
139+ {
140+ { "h" , hsl . Hue } ,
141+ { "s" , hsl . Saturation } ,
142+ { "l" , hsl . Lightness } ,
143+ { "a" , hsl . Alpha }
144+ } ;
145+ }
146+
147+ private SolidColorBrush ColorFromDictionary ( Dictionary < string , object > dictionary )
148+ {
149+ if ( dictionary == null )
150+ {
151+ return null ;
152+ }
153+
154+ double h ;
155+ double s ;
156+ double l ;
157+ double a ;
158+
159+ {
160+ if ( dictionary . TryGetValue ( "h" , out object o ) == false || ! ( o is double d ) )
161+ {
162+ throw new ArgumentException ( ) ;
163+ }
164+
165+ h = d ;
166+ }
167+
168+ {
169+ if ( dictionary . TryGetValue ( "s" , out object o ) == false || ! ( o is double d ) )
170+ {
171+ throw new ArgumentException ( ) ;
172+ }
173+
174+ s = d ;
175+ }
176+
177+ {
178+ if ( dictionary . TryGetValue ( "l" , out object o ) == false || ! ( o is double d ) )
179+ {
180+ throw new ArgumentException ( ) ;
181+ }
182+
183+ l = d ;
184+ }
185+
186+ {
187+ if ( dictionary . TryGetValue ( "a" , out object o ) == false || ! ( o is double d ) )
188+ {
189+ throw new ArgumentException ( ) ;
190+ }
191+
192+ a = d ;
193+ }
194+
195+ return new SolidColorBrush ( ColorExtensions . FromHSL ( h , s , l , a ) ) ;
196+ }
65197 }
66198}
0 commit comments