Skip to content

Commit 99a894d

Browse files
committed
Windows - Updated the project so it can now read colorset files in versions 1.2.
1 parent 94241ea commit 99a894d

File tree

6 files changed

+202
-23
lines changed

6 files changed

+202
-23
lines changed

ColorSetKit-Test/Test.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void TestInitWithPath()
3939
string path = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ), "Colors.colorset" );
4040
ColorSet set = new ColorSet( path );
4141

42-
Assert.IsTrue( set.Count > 0 );
42+
Assert.IsTrue( set.Colors.Count > 0 );
4343
}
4444

4545
[TestMethod]
@@ -49,13 +49,13 @@ public void TestInitWithData()
4949
Data data = new Data( path );
5050
ColorSet set = new ColorSet( data );
5151

52-
Assert.IsTrue( set.Count > 0 );
52+
Assert.IsTrue( set.Colors.Count > 0 );
5353
}
5454

5555
[TestMethod]
5656
public void TestShared()
5757
{
58-
Assert.AreEqual( ColorSet.Shared.Count, 2 );
58+
Assert.AreEqual( ColorSet.Shared.Colors.Count, 2 );
5959

6060
Assert.IsTrue( ColorSet.Shared[ "NoVariant" ] != null );
6161
Assert.IsTrue( ColorSet.Shared[ "Variant" ] != null );
@@ -169,7 +169,7 @@ public void TestCreate()
169169

170170
set = new ColorSet( set.Data );
171171

172-
Assert.AreEqual( set.Count, 2 );
172+
Assert.AreEqual( set.Colors.Count, 2 );
173173

174174
Assert.IsTrue( set[ "NoVariant" ] != null );
175175
Assert.IsTrue( set[ "Variant" ] != null );

ColorSetKit/ColorPair.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public SolidColorBrush Variant
4444
set;
4545
}
4646

47+
public List< LightnessPair > Lightnesses
48+
{
49+
get;
50+
set;
51+
}
52+
4753
public ColorPair(): this( null )
4854
{}
4955

@@ -52,8 +58,9 @@ public ColorPair( SolidColorBrush color ): this( color, null )
5258

5359
public ColorPair( SolidColorBrush color, SolidColorBrush variant )
5460
{
55-
this.Color = color;
56-
this.Variant = variant;
61+
this.Color = color;
62+
this.Variant = variant;
63+
this.Lightnesses = new List< LightnessPair >();
5764
}
5865
}
5966
}

ColorSetKit/ColorSet.cs

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

ColorSetKit/ColorSetKit.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
<Compile Include="ColorSet.cs" />
5050
<Compile Include="ColorSetStream.cs" />
5151
<Compile Include="Data.cs" />
52+
<Compile Include="LightnessPair.cs" />
53+
<Compile Include="LightnessVariant.cs" />
5254
<Compile Include="Properties\AssemblyInfo.cs" />
5355
</ItemGroup>
5456
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

ColorSetKit/LightnessPair.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
using System.Windows.Media;
30+
31+
namespace ColorSetKit
32+
{
33+
public partial class LightnessPair
34+
{
35+
public LightnessVariant Lightness1
36+
{
37+
get;
38+
set;
39+
}
40+
41+
public LightnessVariant Lightness2
42+
{
43+
get;
44+
set;
45+
}
46+
47+
public LightnessPair()
48+
{
49+
this.Lightness1 = new LightnessVariant();
50+
this.Lightness2 = new LightnessVariant();
51+
}
52+
}
53+
}

ColorSetKit/LightnessVariant.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
using System.Windows.Media;
30+
31+
namespace ColorSetKit
32+
{
33+
public partial class LightnessVariant
34+
{
35+
public double Lightness
36+
{
37+
get;
38+
set;
39+
}
40+
41+
public string Name
42+
{
43+
get;
44+
set;
45+
}
46+
47+
public LightnessVariant()
48+
{
49+
this.Lightness = 0;
50+
this.Name = "";
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)