Skip to content

Commit 9661240

Browse files
committed
Preparing for the XML format.
1 parent 4fa8723 commit 9661240

File tree

6 files changed

+338
-3
lines changed

6 files changed

+338
-3
lines changed

ColorSetKit/ColorPair.cs

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

ColorSetKit/ColorSet.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131

3232
namespace ColorSetKit
3333
{
34+
public enum ColorSetFormat
35+
{
36+
Binary,
37+
XML
38+
}
39+
3440
public partial class ColorSet
3541
{
3642
private static readonly ulong Magic = 0x434F4C4F52534554;
@@ -92,6 +98,13 @@ private List< ColorSet > Children
9298
}
9399
= new List< ColorSet >();
94100

101+
private ColorSetFormat Format
102+
{
103+
get;
104+
set;
105+
}
106+
= ColorSetFormat.Binary;
107+
95108
private object Lock
96109
{
97110
get;
@@ -327,5 +340,88 @@ public Data Data
327340
return stream.Data;
328341
}
329342
}
343+
344+
public ColorSet( Dictionary< string, object > dictionary ): this()
345+
{
346+
ulong magic;
347+
ulong major;
348+
ulong minor;
349+
350+
{
351+
if( dictionary.TryGetValue( "magic", out object o ) == false || !( o is UInt64 u ) )
352+
{
353+
throw new ArgumentException();
354+
}
355+
356+
magic = u;
357+
}
358+
359+
{
360+
if( dictionary.TryGetValue( "major", out object o ) == false || !( o is UInt64 u ) )
361+
{
362+
throw new ArgumentException();
363+
}
364+
365+
major = u;
366+
}
367+
368+
{
369+
if( dictionary.TryGetValue( "minor", out object o ) == false || !( o is UInt64 u ) )
370+
{
371+
throw new ArgumentException();
372+
}
373+
374+
minor = u;
375+
}
376+
377+
if( magic != Magic )
378+
{
379+
throw new ArgumentException();
380+
}
381+
382+
if( major < 1 || minor < 2 )
383+
{
384+
throw new ArgumentException();
385+
}
386+
387+
{
388+
if( dictionary.TryGetValue( "colors", out object o ) && o is Dictionary< string, Dictionary< string, object > > colors )
389+
{
390+
foreach( KeyValuePair< string, Dictionary< string, object > > p in colors )
391+
{
392+
try
393+
{
394+
this.ColorPairs[ p.Key ] = new ColorPair( p.Value );
395+
}
396+
catch
397+
{}
398+
}
399+
}
400+
}
401+
402+
this.Format = ColorSetFormat.XML;
403+
}
404+
405+
public Dictionary< string, object > ToDictionary()
406+
{
407+
Dictionary< string, ColorPair > colors = this.ColorPairs;
408+
Dictionary< string, object > dict = new Dictionary< string, object >
409+
{
410+
{ "magic", Magic },
411+
{ "major", Major },
412+
{ "minor", Minor }
413+
};
414+
415+
Dictionary< string, Dictionary< string, object > > pairs = new Dictionary< string, Dictionary< string, object > >();
416+
417+
foreach( KeyValuePair< string, ColorPair > p in colors )
418+
{
419+
pairs[ p.Key ] = p.Value.ToDictionary();
420+
}
421+
422+
dict[ "colors" ] = pairs;
423+
424+
return dict;
425+
}
330426
}
331427
}

ColorSetKit/ColorSetKit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="ColorSet.cs" />
5151
<Compile Include="ColorSetStream.cs" />
5252
<Compile Include="Data.cs" />
53+
<Compile Include="IDictionaryRepresentable.cs" />
5354
<Compile Include="LightnessPair.cs" />
5455
<Compile Include="LightnessVariant.cs" />
5556
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*******************************************************************************
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2018 Jean-David Gadina - www.imazing.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
******************************************************************************/
24+
25+
using System;
26+
using System.Collections.Generic;
27+
using System.Linq;
28+
using System.Text;
29+
30+
namespace ColorSetKit
31+
{
32+
public interface IDictionaryRepresentable
33+
{
34+
Dictionary< string, object > ToDictionary();
35+
}
36+
}

ColorSetKit/LightnessPair.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
namespace ColorSetKit
3232
{
33-
public partial class LightnessPair
33+
public partial class LightnessPair: IDictionaryRepresentable
3434
{
3535
public LightnessVariant Lightness1
3636
{
@@ -49,5 +49,42 @@ public LightnessPair()
4949
this.Lightness1 = new LightnessVariant();
5050
this.Lightness2 = new LightnessVariant();
5151
}
52+
53+
public LightnessPair( Dictionary< string, object > dictionary ): this()
54+
{
55+
if( dictionary == null )
56+
{
57+
throw new ArgumentException();
58+
}
59+
60+
{
61+
if( dictionary.TryGetValue( "lightness1", out object o ) == false || !( o is Dictionary< string, object > dict ) )
62+
{
63+
throw new ArgumentException();
64+
}
65+
66+
this.Lightness1 = new LightnessVariant( dict );
67+
}
68+
69+
{
70+
if( dictionary.TryGetValue( "lightness2", out object o ) == false || !( o is Dictionary< string, object > dict ) )
71+
{
72+
throw new ArgumentException();
73+
}
74+
75+
this.Lightness2 = new LightnessVariant( dict );
76+
}
77+
}
78+
79+
public Dictionary< string, object > ToDictionary()
80+
{
81+
Dictionary< string, object > dict = new Dictionary< string, object >
82+
{
83+
{ "lightness1", this.Lightness1.ToDictionary() },
84+
{ "lightness2", this.Lightness2.ToDictionary() }
85+
};
86+
87+
return dict;
88+
}
5289
}
5390
}

0 commit comments

Comments
 (0)