Skip to content

Commit 54d2ca1

Browse files
committed
Added support for writig colorset files.
1 parent 3764f01 commit 54d2ca1

File tree

4 files changed

+230
-10
lines changed

4 files changed

+230
-10
lines changed

ColorSetKit/ColorSet.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ private object _Lock
7979
}
8080
= new object();
8181

82+
public ColorSet()
83+
{}
84+
8285
public ColorSet( string path ): this( new Data( path ) )
8386
{}
8487

@@ -174,5 +177,35 @@ public void Set( SolidColorBrush color, SolidColorBrush variant, string name )
174177
this._Colors[ name ] = new ColorPair( color, variant );
175178
}
176179
}
180+
181+
public Data Data
182+
{
183+
get
184+
{
185+
Dictionary< string, ColorPair > colors = this.Colors;
186+
ColorSetStream stream = new ColorSetStream();
187+
System.Windows.Media.Color clear = new System.Windows.Media.Color();
188+
189+
clear.R = 0;
190+
clear.G = 0;
191+
clear.B = 0;
192+
clear.A = 0;
193+
194+
stream += Magic; /* COLORSET */
195+
stream += ( uint )1; /* Major */
196+
stream += ( uint )0; /* Minor */
197+
stream += ( ulong )( colors.Count ); /* Count */
198+
199+
foreach( KeyValuePair< string, ColorPair > p in colors )
200+
{
201+
stream += p.Key;
202+
stream += p.Value.Variant != null;
203+
stream += p.Value.Color ?? new SolidColorBrush( clear );
204+
stream += p.Value.Variant ?? new SolidColorBrush( clear );
205+
}
206+
207+
return stream.Data;
208+
}
209+
}
177210
}
178211
}

ColorSetKit/ColorSetKit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<Reference Include="Microsoft.CSharp" />
4242
<Reference Include="System.Data" />
4343
<Reference Include="System.Xml" />
44+
<Reference Include="WindowsBase" />
4445
</ItemGroup>
4546
<ItemGroup>
4647
<Compile Include="Color.cs" />

ColorSetKit/ColorSetStream.cs

Lines changed: 175 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@
2525
using System;
2626
using System.Collections.Generic;
2727
using System.Linq;
28+
using System.Net;
2829
using System.Text;
2930
using System.Windows.Media;
3031

3132
namespace ColorSetKit
3233
{
3334
public partial class ColorSetStream
3435
{
35-
private Data Data
36+
public Data Data
3637
{
3738
get;
38-
set;
39+
private set;
3940
}
4041

4142
private ulong Pos
@@ -51,6 +52,11 @@ private object Lock
5152
}
5253
= new object();
5354

55+
public ColorSetStream()
56+
{
57+
this.Data = new Data();
58+
}
59+
5460
public ColorSetStream( Data data )
5561
{
5662
this.Data = data;
@@ -232,5 +238,172 @@ public bool Read( ulong size, out byte[] buffer )
232238
}
233239
}
234240
}
241+
242+
public void Append( string value )
243+
{
244+
lock( this.Lock )
245+
{
246+
byte[] data = Encoding.ASCII.GetBytes( value );
247+
248+
this.Append( ( ulong )( value.Length + 1 ) );
249+
this.Data.Append( data );
250+
this.Append( 0 );
251+
}
252+
}
253+
254+
public void Append( SolidColorBrush value )
255+
{
256+
lock( this.Lock )
257+
{
258+
double r = value.Color.R;
259+
double g = value.Color.G;
260+
double b = value.Color.B;
261+
double a = value.Color.A;
262+
263+
r /= 255;
264+
g /= 255;
265+
b /= 255;
266+
a /= 255;
267+
268+
this.Append( r );
269+
this.Append( g );
270+
this.Append( b );
271+
this.Append( a );
272+
}
273+
}
274+
275+
public void Append( byte value )
276+
{
277+
lock( this.Lock )
278+
{
279+
this.Data.Append( new byte[] { value } );
280+
}
281+
}
282+
283+
public void Append( ushort value )
284+
{
285+
lock( this.Lock )
286+
{
287+
if( BitConverter.IsLittleEndian == false )
288+
{
289+
value = ( ushort )IPAddress.HostToNetworkOrder( ( short )value );
290+
}
291+
292+
this.Data.Append( BitConverter.GetBytes( value ) );
293+
}
294+
}
295+
296+
public void Append( uint value )
297+
{
298+
lock( this.Lock )
299+
{
300+
if( BitConverter.IsLittleEndian == false )
301+
{
302+
value = ( uint )IPAddress.HostToNetworkOrder( ( int )value );
303+
}
304+
305+
this.Data.Append( BitConverter.GetBytes( value ) );
306+
}
307+
}
308+
309+
public void Append( ulong value )
310+
{
311+
lock( this.Lock )
312+
{
313+
if( BitConverter.IsLittleEndian == false )
314+
{
315+
value = ( ulong )IPAddress.HostToNetworkOrder( ( long )value );
316+
}
317+
318+
this.Data.Append( BitConverter.GetBytes( value ) );
319+
}
320+
}
321+
322+
public void Append( float value )
323+
{
324+
lock( this.Lock )
325+
{
326+
this.Data.Append( BitConverter.GetBytes( value ) );
327+
}
328+
}
329+
330+
public void Append( double value )
331+
{
332+
lock( this.Lock )
333+
{
334+
this.Data.Append( BitConverter.GetBytes( value ) );
335+
}
336+
}
337+
338+
public void Append( bool value )
339+
{
340+
lock( this.Lock )
341+
{
342+
this.Append( ( value ) ? ( byte )1 : ( byte )0 );
343+
}
344+
}
345+
346+
public static ColorSetStream operator +( ColorSetStream s, string value )
347+
{
348+
s.Append( value );
349+
350+
return s;
351+
}
352+
353+
public static ColorSetStream operator +( ColorSetStream s, SolidColorBrush value )
354+
{
355+
s.Append( value );
356+
357+
return s;
358+
}
359+
360+
public static ColorSetStream operator +( ColorSetStream s, byte value )
361+
{
362+
s.Append( value );
363+
364+
return s;
365+
}
366+
367+
public static ColorSetStream operator +( ColorSetStream s, ushort value )
368+
{
369+
s.Append( value );
370+
371+
return s;
372+
}
373+
374+
public static ColorSetStream operator +( ColorSetStream s, uint value )
375+
{
376+
s.Append( value );
377+
378+
return s;
379+
}
380+
381+
public static ColorSetStream operator +( ColorSetStream s, ulong value )
382+
{
383+
s.Append( value );
384+
385+
return s;
386+
}
387+
388+
public static ColorSetStream operator +( ColorSetStream s, float value )
389+
{
390+
s.Append( value );
391+
392+
return s;
393+
}
394+
395+
public static ColorSetStream operator +( ColorSetStream s, double value )
396+
{
397+
s.Append( value );
398+
399+
return s;
400+
}
401+
402+
public static ColorSetStream operator +( ColorSetStream s, bool value )
403+
{
404+
s.Append( value );
405+
406+
return s;
407+
}
235408
}
236409
}

ColorSetKit/Data.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,49 @@ public partial class Data
3434
{
3535
public ulong Count
3636
{
37-
get;
38-
private set;
37+
get
38+
{
39+
return ( ulong )( this.Bytes.Count );
40+
}
3941
}
40-
= 0;
4142

42-
private byte[] Bytes
43+
private List< byte > Bytes
4344
{
4445
get;
4546
set;
4647
}
4748

49+
public Data()
50+
{
51+
this.Bytes = new List< byte >();
52+
}
53+
4854
public Data( string path )
4955
{
5056
try
5157
{
52-
this.Bytes = File.ReadAllBytes( path );
53-
this.Count = ( ulong )this.Bytes.Length;
58+
this.Bytes = File.ReadAllBytes( path ).ToList();
5459
}
5560
catch
5661
{
5762

58-
this.Count = 0;
63+
this.Bytes = new List< byte >();
5964
}
6065
}
6166

6267
public void CopyBytes( byte[] buffer, ulong offset, ulong size )
6368
{
6469
for( ulong i = offset; i < offset + size; i++ )
6570
{
66-
buffer[ i - offset ] = this.Bytes[ i ];
71+
buffer[ i - offset ] = this.Bytes[ ( int )i ];
72+
}
73+
}
74+
75+
public void Append( byte[] data )
76+
{
77+
foreach( byte b in data )
78+
{
79+
this.Bytes.Add( b );
6780
}
6881
}
6982
}

0 commit comments

Comments
 (0)