@@ -34,6 +34,8 @@ namespace ColorSetKit
3434 public partial class ColorSet
3535 {
3636 private static readonly ulong Magic = 0x434F4C4F52534554 ;
37+ private static readonly uint Major = 1 ;
38+ private static readonly uint Minor = 2 ;
3739 private static ColorSet SharedInstance = null ;
3840 private static readonly object SharedLock = new object ( ) ;
3941
@@ -76,7 +78,7 @@ public static ColorSet Shared
7678 }
7779 }
7880
79- private Dictionary < string , ColorPair > Colors
81+ private Dictionary < string , ColorPair > ColorPairs
8082 {
8183 get ;
8284 set ;
@@ -96,13 +98,23 @@ private object Lock
9698 }
9799 = new object ( ) ;
98100
99- public int Count
101+ public Dictionary < string , ColorPair > Colors
100102 {
101103 get
102104 {
103105 lock ( this . Lock )
104106 {
105- return this . Colors . Count ;
107+ Dictionary < string , ColorPair > colors = new Dictionary < string , ColorPair > ( ) ;
108+
109+ foreach ( KeyValuePair < string , ColorPair > p in this . ColorPairs )
110+ {
111+ if ( this . ColorNamed ( p . Key ) is ColorPair color )
112+ {
113+ colors [ p . Key ] = color ;
114+ }
115+ }
116+
117+ return colors ;
106118 }
107119 }
108120 }
@@ -127,13 +139,20 @@ public ColorSet( Data data )
127139 return ;
128140 }
129141
130- if ( stream . ReadUInt32 ( ) == 0 )
142+ uint major = stream . ReadUInt32 ( ) ;
143+
144+ if ( major == 0 )
131145 {
132146 return ;
133147 }
134148
135- stream . ReadUInt32 ( ) ;
149+ uint minor = stream . ReadUInt32 ( ) ;
136150
151+ if ( major > Major || minor > Minor )
152+ {
153+ return ;
154+ }
155+
137156 ulong n = stream . ReadUInt64 ( ) ;
138157
139158 for ( ulong i = 0 ; i < n ; i ++ )
@@ -155,7 +174,26 @@ public ColorSet( Data data )
155174 return ;
156175 }
157176
158- this . Add ( color , ( hasVariant ) ? variant : null , name ) ;
177+ List < LightnessPair > lightnesses = new List < LightnessPair > ( ) ;
178+
179+ if ( major > 1 || minor > 1 )
180+ {
181+ ulong nn = stream . ReadUInt64 ( ) ;
182+
183+ for ( ulong j = 0 ; j < nn ; j ++ )
184+ {
185+ LightnessPair p = new LightnessPair ( ) ;
186+
187+ p . Lightness1 . Lightness = stream . ReadDouble ( ) ;
188+ p . Lightness1 . Name = stream . ReadString ( ) ?? "" ;
189+ p . Lightness2 . Lightness = stream . ReadDouble ( ) ;
190+ p . Lightness2 . Name = stream . ReadString ( ) ?? "" ;
191+
192+ lightnesses . Add ( p ) ;
193+ }
194+ }
195+
196+ this . Add ( color , ( hasVariant ) ? variant : null , lightnesses , name ) ;
159197 }
160198 }
161199
@@ -168,9 +206,9 @@ public ColorPair ColorNamed( string name )
168206 {
169207 lock ( this . Lock )
170208 {
171- if ( this . Colors . ContainsKey ( name ) )
209+ if ( this . ColorPairs . ContainsKey ( name ) )
172210 {
173- return this . Colors [ name ] ;
211+ return this . ColorPairs [ name ] ;
174212 }
175213
176214 foreach ( ColorSet child in this . Children )
@@ -210,17 +248,27 @@ public void Set( SolidColorBrush color, string name )
210248 }
211249
212250 public void Add ( SolidColorBrush color , SolidColorBrush variant , string name )
251+ {
252+ this . Add ( color , variant , null , name ) ;
253+ }
254+
255+ public void Set ( SolidColorBrush color , SolidColorBrush variant , string name )
256+ {
257+ this . Set ( color , variant , null , name ) ;
258+ }
259+
260+ public void Add ( SolidColorBrush color , SolidColorBrush variant , List < LightnessPair > lightnesses , string name )
213261 {
214262 lock ( this . Lock )
215263 {
216- if ( this . Colors . ContainsKey ( name ) == false )
264+ if ( this . ColorPairs . ContainsKey ( name ) == false )
217265 {
218- this . Set ( color , variant , name ) ;
266+ this . Set ( color , variant , lightnesses , name ) ;
219267 }
220268 }
221269 }
222270
223- public void Set ( SolidColorBrush color , SolidColorBrush variant , string name )
271+ public void Set ( SolidColorBrush color , SolidColorBrush variant , List < LightnessPair > lightnesses , string name )
224272 {
225273 if ( name == null )
226274 {
@@ -229,15 +277,19 @@ public void Set( SolidColorBrush color, SolidColorBrush variant, string name )
229277
230278 lock ( this . Lock )
231279 {
232- this . Colors [ name ] = new ColorPair ( color , variant ) ;
280+ ColorPair p = new ColorPair ( color , variant )
281+ {
282+ Lightnesses = lightnesses ?? new List < LightnessPair > ( )
283+ } ;
284+ this . ColorPairs [ name ] = p ;
233285 }
234286 }
235287
236288 public Data Data
237289 {
238290 get
239291 {
240- Dictionary < string , ColorPair > colors = this . Colors ;
292+ Dictionary < string , ColorPair > colors = this . ColorPairs ;
241293 ColorSetStream stream = new ColorSetStream ( ) ;
242294 System . Windows . Media . Color clear = new System . Windows . Media . Color
243295 {
@@ -247,17 +299,29 @@ public Data Data
247299 A = 0
248300 } ;
249301
250- stream += Magic ; /* COLORSET */
251- stream += ( uint ) 1 ; /* Major */
252- stream += ( uint ) 0 ; /* Minor */
253- stream += ( ulong ) ( colors . Count ) ; /* Count */
302+ stream += Magic ;
303+ stream += Major ;
304+ stream += Minor ;
305+ stream += ( ulong ) ( colors . Count ) ;
254306
255307 foreach ( KeyValuePair < string , ColorPair > p in colors )
256308 {
257309 stream += p . Key ;
258310 stream += p . Value . Variant != null ;
259311 stream += p . Value . Color ?? new SolidColorBrush ( clear ) ;
260312 stream += p . Value . Variant ?? new SolidColorBrush ( clear ) ;
313+ stream += ( ulong ) ( p . Value . Lightnesses ? . Count ?? 0 ) ;
314+
315+ foreach ( LightnessPair lp in p . Value . Lightnesses )
316+ {
317+ LightnessVariant l1 = lp . Lightness1 ?? new LightnessVariant ( ) ;
318+ LightnessVariant l2 = lp . Lightness2 ?? new LightnessVariant ( ) ;
319+
320+ stream += l1 . Lightness ;
321+ stream += l1 . Name ?? "" ;
322+ stream += l2 . Lightness ;
323+ stream += l2 . Name ?? "" ;
324+ }
261325 }
262326
263327 return stream . Data ;
0 commit comments