Skip to content

Commit 7989b61

Browse files
committed
Support for the new XML format.
1 parent e052187 commit 7989b61

File tree

5 files changed

+100
-61
lines changed

5 files changed

+100
-61
lines changed

ColorSetKit-Test/ColorSetKit-Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
</Target>
7979
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />
8080
<PropertyGroup>
81-
<PostBuildEvent>copy "$(ProjectDir)Colors.colorset" "$(TargetDir)Colors.colorset"</PostBuildEvent>
81+
<PostBuildEvent>copy "$(ProjectDir)Colors.colorset" "$(TargetDir)Colors.colorset"
82+
copy "$(ProjectDir)Colors-XML.colorset" "$(TargetDir)Colors-XML.colorset"</PostBuildEvent>
8283
</PropertyGroup>
8384
</Project>

ColorSetKit-Test/Test.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace ColorSetKit_Test
3434
public class Test
3535
{
3636
[TestMethod]
37-
public void TestInitWithPath()
37+
public void TestInitWithPathBinary()
3838
{
3939
string path = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ), "Colors.colorset" );
4040
ColorSet set = new ColorSet( path );
@@ -43,7 +43,16 @@ public void TestInitWithPath()
4343
}
4444

4545
[TestMethod]
46-
public void TestInitWithData()
46+
public void TestInitWithPathXML()
47+
{
48+
string path = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ), "Colors-XML.colorset" );
49+
ColorSet set = new ColorSet( path );
50+
51+
Assert.IsTrue( set.Colors.Count > 0 );
52+
}
53+
54+
[TestMethod]
55+
public void TestInitWithDataBinary()
4756
{
4857
string path = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ), "Colors.colorset" );
4958
Data data = new Data( path );
@@ -52,6 +61,16 @@ public void TestInitWithData()
5261
Assert.IsTrue( set.Colors.Count > 0 );
5362
}
5463

64+
[TestMethod]
65+
public void TestInitWithDataXML()
66+
{
67+
string path = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ), "Colors-XML.colorset" );
68+
Data data = new Data( path );
69+
ColorSet set = new ColorSet( data );
70+
71+
Assert.IsTrue( set.Colors.Count > 0 );
72+
}
73+
5574
[TestMethod]
5675
public void TestShared()
5776
{

ColorSetKit/ColorPair.cs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,37 +71,36 @@ public ColorPair( Dictionary< string, object > dictionary ): this()
7171
}
7272

7373
{
74-
if( dictionary.TryGetValue( "color", out object o ) == false || !( o is Dictionary< string, object > dict ) )
74+
if( dictionary.TryGetValue( "color", out object o ) && o is Dictionary< string, object > dict )
7575
{
76-
throw new ArgumentException();
76+
this.Color = this.ColorFromDictionary( dict );
7777
}
78-
79-
this.Color = this.ColorFromDictionary( dict );
8078
}
8179

8280
{
83-
if( dictionary.TryGetValue( "variant", out object o ) == false || !( o is Dictionary< string, object > dict ) )
81+
if( dictionary.TryGetValue( "variant", out object o ) && o is Dictionary< string, object > dict )
8482
{
85-
throw new ArgumentException();
83+
this.Variant = this.ColorFromDictionary( dict );
8684
}
87-
88-
this.Variant = this.ColorFromDictionary( dict );
8985
}
9086

9187
{
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 )
88+
if( dictionary.TryGetValue( "lightnesses", out object o ) && o is List< object > list )
9889
{
99-
try
90+
foreach( object value in list )
10091
{
101-
this.Lightnesses.Add( new LightnessPair( l ) );
92+
if( !( value is Dictionary<string, object> l ) )
93+
{
94+
continue;
95+
}
96+
97+
try
98+
{
99+
this.Lightnesses.Add( new LightnessPair( l ) );
100+
}
101+
catch
102+
{}
102103
}
103-
catch
104-
{}
105104
}
106105
}
107106
}
@@ -133,14 +132,14 @@ private Dictionary< string, object > ColorToDictionary( SolidColorBrush color )
133132
return null;
134133
}
135134

136-
ColorExtensions.HSLComponents hsl = color.Color.GetHSL();
135+
ColorExtensions.RGBComponents rgb = color.Color.GetRGB();
137136

138137
return new Dictionary< string, object >
139138
{
140-
{ "h", hsl.Hue },
141-
{ "s", hsl.Saturation },
142-
{ "l", hsl.Lightness },
143-
{ "a", hsl.Alpha }
139+
{ "r", rgb.Red },
140+
{ "g", rgb.Green },
141+
{ "b", rgb.Blue },
142+
{ "a", rgb.Alpha }
144143
};
145144
}
146145

@@ -151,36 +150,36 @@ private SolidColorBrush ColorFromDictionary( Dictionary< string, object > dictio
151150
return null;
152151
}
153152

154-
double h;
155-
double s;
156-
double l;
153+
double r;
154+
double g;
155+
double b;
157156
double a;
158157

159158
{
160-
if( dictionary.TryGetValue( "h", out object o ) == false || !( o is double d ) )
159+
if( dictionary.TryGetValue( "r", out object o ) == false || !( o is double d ) )
161160
{
162161
throw new ArgumentException();
163162
}
164163

165-
h = d;
164+
r = d;
166165
}
167166

168167
{
169-
if( dictionary.TryGetValue( "s", out object o ) == false || !( o is double d ) )
168+
if( dictionary.TryGetValue( "g", out object o ) == false || !( o is double d ) )
170169
{
171170
throw new ArgumentException();
172171
}
173172

174-
s = d;
173+
g = d;
175174
}
176175

177176
{
178-
if( dictionary.TryGetValue( "l", out object o ) == false || !( o is double d ) )
177+
if( dictionary.TryGetValue( "b", out object o ) == false || !( o is double d ) )
179178
{
180179
throw new ArgumentException();
181180
}
182181

183-
l = d;
182+
b = d;
184183
}
185184

186185
{
@@ -192,7 +191,16 @@ private SolidColorBrush ColorFromDictionary( Dictionary< string, object > dictio
192191
a = d;
193192
}
194193

195-
return new SolidColorBrush( ColorExtensions.FromHSL( h, s, l, a ) );
194+
return new SolidColorBrush
195+
(
196+
System.Windows.Media.Color.FromArgb
197+
(
198+
( byte )Math.Round( a * 255 ),
199+
( byte )Math.Round( r * 255 ),
200+
( byte )Math.Round( g * 255 ),
201+
( byte )Math.Round( b * 255 )
202+
)
203+
);
196204
}
197205
}
198206
}

ColorSetKit/ColorSet.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ public ColorSet( Data data )
145145
return;
146146
}
147147

148+
if( PropertyListSerialization.PropertyListFromData( data ) is Dictionary< string, object > dict )
149+
{
150+
this.InitFromDictionary( dict );
151+
152+
return;
153+
}
154+
148155
ColorSetStream stream = new ColorSetStream( data );
149156

150157
if( stream.ReadUInt64() != Magic )
@@ -341,37 +348,37 @@ public Data Data
341348
}
342349
}
343350

344-
public ColorSet( Dictionary< string, object > dictionary ): this()
351+
private void InitFromDictionary( Dictionary< string, object > dictionary )
345352
{
346353
ulong magic;
347354
ulong major;
348355
ulong minor;
349356

350357
{
351-
if( dictionary.TryGetValue( "magic", out object o ) == false || !( o is ulong u ) )
358+
if( dictionary.TryGetValue( "magic", out object o ) == false || !( o is long u ) )
352359
{
353360
throw new ArgumentException();
354361
}
355362

356-
magic = u;
363+
magic = ( ulong )u;
357364
}
358365

359366
{
360-
if( dictionary.TryGetValue( "major", out object o ) == false || !( o is ulong u ) )
367+
if( dictionary.TryGetValue( "major", out object o ) == false || !( o is long u ) )
361368
{
362369
throw new ArgumentException();
363370
}
364371

365-
major = u;
372+
major = ( ulong )u;
366373
}
367374

368375
{
369-
if( dictionary.TryGetValue( "minor", out object o ) == false || !( o is ulong u ) )
376+
if( dictionary.TryGetValue( "minor", out object o ) == false || !( o is long u ) )
370377
{
371378
throw new ArgumentException();
372379
}
373380

374-
minor = u;
381+
minor = ( ulong )u;
375382
}
376383

377384
if( magic != Magic )
@@ -385,13 +392,18 @@ public ColorSet( Dictionary< string, object > dictionary ): this()
385392
}
386393

387394
{
388-
if( dictionary.TryGetValue( "colors", out object o ) && o is Dictionary< string, Dictionary< string, object > > colors )
395+
if( dictionary.TryGetValue( "colors", out object o ) && o is Dictionary< string, object > colors )
389396
{
390-
foreach( KeyValuePair< string, Dictionary< string, object > > p in colors )
397+
foreach( KeyValuePair< string, object > p in colors )
391398
{
399+
if( !( p.Value is Dictionary< string, object > dict ) )
400+
{
401+
continue;
402+
}
403+
392404
try
393405
{
394-
this.ColorPairs[ p.Key ] = new ColorPair( p.Value );
406+
this.ColorPairs[ p.Key ] = new ColorPair( dict );
395407
}
396408
catch
397409
{}

ColorSetKit/PropertyListSerialization.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
using System;
2626
using System.Collections.Generic;
27+
using System.Globalization;
2728
using System.IO;
2829
using System.Linq;
2930
using System.Text;
@@ -61,22 +62,20 @@ public static object PropertyListFromData( Data data )
6162
{
6263
xml.Load( reader );
6364

64-
if( xml.ChildNodes.Count != 1 )
65+
foreach( XmlNode child in xml.ChildNodes )
6566
{
66-
return null;
67-
}
67+
if( child.Name != "plist" )
68+
{
69+
continue;
70+
}
6871

69-
if( xml.FirstChild.Name != "plist" )
70-
{
71-
return null;
72-
}
72+
if( child.ChildNodes.Count != 1 )
73+
{
74+
return null;
75+
}
7376

74-
if( xml.FirstChild.ChildNodes.Count != 1 )
75-
{
76-
return null;
77+
return PropertyListFromXmlNode( child.FirstChild );
7778
}
78-
79-
return PropertyListFromXmlNode( xml.FirstChild.FirstChild );
8079
}
8180
}
8281
catch
@@ -110,7 +109,7 @@ private static long LongFromXmlNode( XmlNode node )
110109
{
111110
try
112111
{
113-
return Convert.ToInt64( node.Value );
112+
return long.Parse( node.InnerText, CultureInfo.InvariantCulture );
114113
}
115114
catch
116115
{}
@@ -122,7 +121,7 @@ private static double DoubleFromXmlNode( XmlNode node )
122121
{
123122
try
124123
{
125-
return Convert.ToDouble( node.Value );
124+
return double.Parse( node.InnerText, CultureInfo.InvariantCulture );
126125
}
127126
catch
128127
{}
@@ -166,7 +165,7 @@ private static Dictionary< string, object > DictionaryFromXmlNode( XmlNode node
166165
{
167166
if( child.Name == "key" )
168167
{
169-
key = child.Value;
168+
key = child.InnerText;
170169

171170
continue;
172171
}

0 commit comments

Comments
 (0)