Skip to content

Commit 3f6d15c

Browse files
committed
C# - Colors array is now private. Returning ColorPair objects. Added support for subscripting.
1 parent 6fa9707 commit 3f6d15c

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

ColorSetKit-Test/Test.cs

Lines changed: 12 additions & 12 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.Colors.Count > 0 );
42+
Assert.IsTrue( set.Count > 0 );
4343
}
4444

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

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

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

60-
Assert.IsTrue( ColorSet.Shared.Colors.ContainsKey( "NoVariant" ) );
61-
Assert.IsTrue( ColorSet.Shared.Colors.ContainsKey( "Variant" ) );
60+
Assert.IsTrue( ColorSet.Shared[ "NoVariant" ] != null );
61+
Assert.IsTrue( ColorSet.Shared[ "Variant" ] != null );
6262

63-
ColorPair p1 = ColorSet.Shared.Colors[ "NoVariant" ];
64-
ColorPair p2 = ColorSet.Shared.Colors[ "Variant" ];
63+
ColorPair p1 = ColorSet.Shared[ "NoVariant" ];
64+
ColorPair p2 = ColorSet.Shared[ "Variant" ];
6565

6666
{
6767
if( p1.Color is SolidColorBrush c )
@@ -144,13 +144,13 @@ public void TestCreate()
144144

145145
set = new ColorSet( set.Data );
146146

147-
Assert.AreEqual( set.Colors.Count, 2 );
147+
Assert.AreEqual( set.Count, 2 );
148148

149-
Assert.IsTrue( set.Colors.ContainsKey( "NoVariant" ) );
150-
Assert.IsTrue( set.Colors.ContainsKey( "Variant" ) );
149+
Assert.IsTrue( set[ "NoVariant" ] != null );
150+
Assert.IsTrue( set[ "Variant" ] != null );
151151

152-
ColorPair p1 = set.Colors[ "NoVariant" ];
153-
ColorPair p2 = set.Colors[ "Variant" ];
152+
ColorPair p1 = set[ "NoVariant" ];
153+
ColorPair p2 = set[ "Variant" ];
154154

155155
{
156156
if( p1.Color is SolidColorBrush c )

ColorSetKit/Color.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Color( string name )
4141

4242
public override object ProvideValue( IServiceProvider provider )
4343
{
44-
return ColorSet.Shared.ColorNamed( this.Name );
44+
return ColorSet.Shared.ColorNamed( this.Name )?.Color;
4545
}
4646

4747
[ConstructorArgument( "name" )]

ColorSetKit/ColorSet.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,30 @@ public static ColorSet Shared
5555
}
5656
}
5757

58-
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
58+
private Dictionary< string, ColorPair > Colors
7059
{
7160
get;
7261
set;
7362
}
7463
= new Dictionary< string, ColorPair >();
7564

76-
private object _Lock
65+
private object Lock
7766
{
7867
get;
7968
}
8069
= new object();
8170

71+
public int Count
72+
{
73+
get
74+
{
75+
lock( this.Lock )
76+
{
77+
return this.Colors.Count;
78+
}
79+
}
80+
}
81+
8282
public ColorSet()
8383
{}
8484

@@ -131,13 +131,21 @@ public ColorSet( Data data )
131131
}
132132
}
133133

134-
public SolidColorBrush ColorNamed( string name )
134+
public ColorPair this[ string key ]
135+
{
136+
get
137+
{
138+
return this.ColorNamed( key );
139+
}
140+
}
141+
142+
public ColorPair ColorNamed( string name )
135143
{
136-
lock( this._Lock )
144+
lock( this.Lock )
137145
{
138-
if( this._Colors.ContainsKey( name ) )
146+
if( this.Colors.ContainsKey( name ) )
139147
{
140-
return this._Colors[ name ].Color;
148+
return this.Colors[ name ];
141149
}
142150

143151
return null;
@@ -156,9 +164,9 @@ public void Set( SolidColorBrush color, string name )
156164

157165
public void Add( SolidColorBrush color, SolidColorBrush variant, string name )
158166
{
159-
lock( this._Lock )
167+
lock( this.Lock )
160168
{
161-
if( this._Colors.ContainsKey( name ) == false )
169+
if( this.Colors.ContainsKey( name ) == false )
162170
{
163171
this.Set( color, variant, name );
164172
}
@@ -172,9 +180,9 @@ public void Set( SolidColorBrush color, SolidColorBrush variant, string name )
172180
return;
173181
}
174182

175-
lock( this._Lock )
183+
lock( this.Lock )
176184
{
177-
this._Colors[ name ] = new ColorPair( color, variant );
185+
this.Colors[ name ] = new ColorPair( color, variant );
178186
}
179187
}
180188

0 commit comments

Comments
 (0)