/
JascPaletteSerializer.cs
175 lines (146 loc) · 5.04 KB
/
JascPaletteSerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Drawing;
using System.IO;
using System.Text;
namespace Cyotek.Windows.Forms
{
// Cyotek Color Picker controls library
// Copyright © 2013-2017 Cyotek Ltd.
// http://cyotek.com/blog/tag/colorpicker
// Licensed under the MIT License. See license.txt for the full text.
// If you use this code in your applications, donations or attribution are welcome
/// <summary>
/// Serializes and deserializes color palettes into and from the Jasc palette format.
/// </summary>
[Obsolete("This class will be removed in a future update to the library.")]
public class JascPaletteSerializer : PaletteSerializer
{
#region Properties
/// <summary>
/// Gets the default extension for files generated with this palette format.
/// </summary>
/// <value>The default extension for files generated with this palette format.</value>
public override string DefaultExtension
{
get { return "pal"; }
}
/// <summary>
/// Gets a descriptive name of the palette format
/// </summary>
/// <value>The descriptive name of the palette format.</value>
public override string Name
{
get { return "JASC Palette"; }
}
#endregion
#region Methods
/// <summary>
/// Determines whether this instance can read palette from data the specified stream.
/// </summary>
/// <param name="stream">The stream.</param>
/// <returns><c>true</c> if this instance can read palette data from the specified stream; otherwise, <c>false</c>.</returns>
public override bool CanReadFrom(Stream stream)
{
bool result;
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
try
{
using (StreamReader reader = new StreamReader(stream))
{
string header;
string version;
// check signature
header = reader.ReadLine();
version = reader.ReadLine();
result = header == "JASC-PAL" && version == "0100";
}
}
catch
{
result = false;
}
return result;
}
/// <summary>
/// Deserializes the <see cref="ColorCollection" /> contained by the specified <see cref="Stream" />.
/// </summary>
/// <param name="stream">The <see cref="Stream" /> that contains the palette to deserialize.</param>
/// <returns>The <see cref="ColorCollection" /> being deserialized.</returns>
public override ColorCollection Deserialize(Stream stream)
{
ColorCollection results;
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
results = new ColorCollection();
using (StreamReader reader = new StreamReader(stream))
{
string header;
string version;
int colorCount;
// check signature
header = reader.ReadLine();
version = reader.ReadLine();
if (header != "JASC-PAL" || version != "0100")
{
throw new InvalidDataException("Invalid palette file");
}
colorCount = Convert.ToInt32(reader.ReadLine());
for (int i = 0; i < colorCount; i++)
{
int r;
int g;
int b;
string data;
string[] parts;
data = reader.ReadLine();
parts = !string.IsNullOrEmpty(data) ? data.Split(new[]
{
' ',
'\t'
}, StringSplitOptions.RemoveEmptyEntries) : new string[0];
if (!int.TryParse(parts[0], out r) || !int.TryParse(parts[1], out g) || !int.TryParse(parts[2], out b))
{
throw new InvalidDataException(string.Format("Invalid palette contents found with data '{0}'", data));
}
results.Add(Color.FromArgb(r, g, b));
}
}
return results;
}
/// <summary>
/// Serializes the specified <see cref="ColorCollection" /> and writes the palette to a file using the specified <see cref="Stream" />.
/// </summary>
/// <param name="stream">The <see cref="Stream" /> used to write the palette.</param>
/// <param name="palette">The <see cref="ColorCollection" /> to serialize.</param>
public override void Serialize(Stream stream, ColorCollection palette)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
if (palette == null)
{
throw new ArgumentNullException(nameof(palette));
}
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
writer.WriteLine("JASC-PAL");
writer.WriteLine("0100");
writer.WriteLine(palette.Count);
foreach (Color color in palette)
{
writer.Write("{0} ", color.R);
writer.Write("{0} ", color.G);
writer.Write("{0} ", color.B);
writer.WriteLine();
}
}
}
#endregion
}
}