Skip to content

Commit f8b12a2

Browse files
committed
Added a shared instance.
1 parent 329dda9 commit f8b12a2

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

ColorSetKit/ColorSet.cs

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System;
2626
using System.Collections.Generic;
2727
using System.Linq;
28+
using System.Reflection;
2829
using System.Text;
2930
using System.Windows.Media;
3031

@@ -33,15 +34,46 @@ namespace ColorSetKit
3334
public partial class ColorSet
3435
{
3536
private static readonly ulong Magic = 0x434F4C4F52534554;
37+
private static ColorSet _Shared = null;
38+
private static object _SharedLock = new object();
39+
40+
public static ColorSet Shared
41+
{
42+
get
43+
{
44+
lock( _SharedLock )
45+
{
46+
if( _Shared == null )
47+
{
48+
string path = Assembly.GetExecutingAssembly().Location;
49+
50+
_Shared = new ColorSet( System.IO.Path.Combine( System.IO.Path.GetDirectoryName( path ), "Colors.colorset" ) );
51+
}
52+
53+
return _Shared;
54+
}
55+
}
56+
}
3657

3758
public Dictionary< string, ColorPair > Colors
59+
{
60+
get
61+
{
62+
lock( this._Lock )
63+
{
64+
return new Dictionary< string, ColorPair >( this._Colors );
65+
}
66+
}
67+
}
68+
69+
private Dictionary< string, ColorPair > _Colors
3870
{
3971
get;
40-
private set;
72+
set;
4173
}
4274
= new Dictionary< string, ColorPair >();
4375

44-
private object Lock
76+
private object _Lock
4577
{
4678
get;
4779
}
@@ -96,6 +128,19 @@ public ColorSet( Data data )
96128
}
97129
}
98130

131+
public SolidColorBrush ColorNamed( string name )
132+
{
133+
lock( this._Lock )
134+
{
135+
if( this._Colors.ContainsKey( name ) )
136+
{
137+
return this._Colors[ name ].Color;
138+
}
139+
140+
return null;
141+
}
142+
}
143+
99144
public void Add( SolidColorBrush color, string name )
100145
{
101146
this.Add( color, null, name );
@@ -108,9 +153,9 @@ public void Set( SolidColorBrush color, string name )
108153

109154
public void Add( SolidColorBrush color, SolidColorBrush variant, string name )
110155
{
111-
lock( this.Lock )
156+
lock( this._Lock )
112157
{
113-
if( this.Colors.ContainsKey( name ) == false )
158+
if( this._Colors.ContainsKey( name ) == false )
114159
{
115160
this.Set( color, variant, name );
116161
}
@@ -124,9 +169,9 @@ public void Set( SolidColorBrush color, SolidColorBrush variant, string name )
124169
return;
125170
}
126171

127-
lock( this.Lock )
172+
lock( this._Lock )
128173
{
129-
this.Colors[ name ] = new ColorPair( color, variant );
174+
this._Colors[ name ] = new ColorPair( color, variant );
130175
}
131176
}
132177
}

0 commit comments

Comments
 (0)