-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorConverter.cs
298 lines (251 loc) · 7.78 KB
/
ColorConverter.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#nullable enable
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SColor = System.Drawing.Color;
namespace Leclair.Stardew.ThemeManager
{
/// <summary>
/// ColorConverter is a JsonConverter for Newtonsoft.Json that has better
/// support for deserializing colors from strings. It uses CSS's hex and
/// rgb() syntaxes, while falling back to System.Drawing.ColorTranslator
/// to parse color names.
/// </summary>
public class ColorConverter : JsonConverter
{
#region Color Names
/// <summary>
/// A dictionary mapping color names to XNA Colors. Names are handled
/// in a case-insensitive way, and lazy loaded.
/// </summary>
private static readonly Lazy<Dictionary<string, Color>> NamedColors = new Lazy<Dictionary<string, Color>>(LoadNamedColors);
private static Dictionary<string, Color> LoadNamedColors()
{
Dictionary<string, Color> result = new Dictionary<string, Color>(StringComparer.OrdinalIgnoreCase);
// Load every available color name from XNA Color.
foreach(PropertyInfo prop in typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.Public))
{
string name = prop.Name;
if (result.ContainsKey(name) || prop.PropertyType != typeof(Color))
continue;
Color? color;
try
{
color = prop.GetValue(null) as Color?;
} catch
{
continue;
}
if (color.HasValue)
result[name] = color.Value;
}
// Also load every available color name from System.Drawing.Color.
foreach(PropertyInfo prop in typeof(SColor).GetProperties(BindingFlags.Static | BindingFlags.Public))
{
string name = prop.Name;
if (result.ContainsKey(name) || prop.PropertyType != typeof(SColor))
continue;
SColor? color;
try
{
color = prop.GetValue(null) as SColor?;
}
catch
{
continue;
}
if (color.HasValue)
result[name] = new Color(
color.Value.R,
color.Value.G,
color.Value.B,
color.Value.A
);
}
return result;
}
#endregion
#region Color Parsing
public static readonly Regex HEX_REGEX = new(@"^\s*#([0-9a-f]{3,4}|(?:[0-9a-f]{2}){3,4})\s*$", RegexOptions.IgnoreCase);
public static readonly Regex RGB_REGEX = new(@"^\s*rgba?\s*\(\s*(\d+%?)\s*,\s*(\d+%?)\s*,\s*(\d+%?)(?:\s*,\s*(\d+%|[\d.]+))?\s*\)\s*$", RegexOptions.IgnoreCase);
public static readonly Regex PLAIN_REGEX = new(@"^\s*(\d+)(?:\s*,\s*(\d+))(?:\s*,\s*(\d+))(?:\s*,\s*(\d+))?\s*$");
/// <summary>
/// If a value ends with a percentage sign, treat it as a percent of
/// 255. Otherwise, just parse it as a normal int.
/// </summary>
/// <param name="value">The value to parse</param>
private static int HydrateRGBValue(string value)
{
if (value.EndsWith('%'))
return (int)Math.Floor(Convert.ToInt32(value[0..^1]) / 100f * 255);
return Convert.ToInt32(value);
}
/// <summary>
/// Parse a CSS color string and return a Color. This supports hex
/// and rgb() syntaxes for input, and falls back to
/// System.Drawing.ColorTranslator.FromHtml() for handling color
/// names. HSL/HSV/etc. are not supported, only RGB.
/// </summary>
/// <param name="input">A CSS color string in hex, rgb, or name format</param>
/// <returns>A Color object, or null if no valid color was present</returns>
public static Color? ParseColor(string? input)
{
if (string.IsNullOrEmpty(input))
return null;
int r = -1;
int g = -1;
int b = -1;
int a = 255;
// CSS hex format is:
// #RGB
// #RGBA
// #RRGGBB
// #RRGGBBAA
// ColorTranslator.FromHtml does this incorrectly according to the
// CSS spec, so let's do it ourselves.
var match = HEX_REGEX.Match(input);
if (match.Success)
{
string value = match.Groups[1].Value;
if (value.Length == 3 || value.Length == 4)
{
r = Convert.ToInt32(value[0..1], 16) * 17;
g = Convert.ToInt32(value[1..2], 16) * 17;
b = Convert.ToInt32(value[2..3], 16) * 17;
if (value.Length == 4)
a = Convert.ToInt32(value[3..4], 16) * 17;
}
if (value.Length == 6 || value.Length == 8)
{
r = Convert.ToInt32(value[0..2], 16);
g = Convert.ToInt32(value[2..4], 16);
b = Convert.ToInt32(value[4..6], 16);
if (value.Length == 8)
a = Convert.ToInt32(value[6..8], 16);
}
}
// CSS rgb format. FromHtml doesn't handle this at all.
match = RGB_REGEX.Match(input);
if (match.Success)
{
r = HydrateRGBValue(match.Groups[1].Value);
g = HydrateRGBValue(match.Groups[2].Value);
b = HydrateRGBValue(match.Groups[3].Value);
if (match.Groups[4].Success)
{
string value = match.Groups[4].Value;
if (value.EndsWith('%'))
a = HydrateRGBValue(value);
else
{
float fval = Convert.ToSingle(value);
if (fval >= 0)
a = (int)Math.Floor(255 * fval);
}
}
}
// Plain Format. Just R, G, B, A on a line. This is how SMAPI's
// built-in Color serializer reads strings, so we should support
// it even if it's a bit silly.
match = PLAIN_REGEX.Match(input);
if (match.Success)
{
r = Convert.ToInt32(match.Groups[1].Value);
g = Convert.ToInt32(match.Groups[2].Value);
b = Convert.ToInt32(match.Groups[3].Value);
if (match.Groups[4].Success)
a = Convert.ToInt32(match.Groups[4].Value);
}
// Did we assign r/g/b values?
if (r != -1 && g != -1 && b != -1)
{
// ... and are they in range?
if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255 && a >= 0 && a <= 255)
return new Color(r, g, b, a);
// If not in range, return null, it's invalid.
return null;
}
// Finally, use the name lookup table in case someone provided
// a color by name rather than value.
if (NamedColors.Value.TryGetValue(input.Trim(), out Color color))
return color;
return null;
}
#endregion
#region JsonConverter
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Color) || Nullable.GetUnderlyingType(objectType) == typeof(Color);
}
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
string path = reader.Path;
switch (reader.TokenType)
{
case JsonToken.Null:
return null;
case JsonToken.String:
return ReadString(JToken.Load(reader).Value<string>());
case JsonToken.StartObject:
return ReadObject(JObject.Load(reader), path);
default:
throw new JsonReaderException($"Can't parse Color? from {reader.TokenType} node (path: {reader.Path}).");
}
}
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
if (value is not Color color)
{
writer.WriteNull();
return;
}
JObject jo = new JObject
{
{"R", color.R},
{"G", color.G},
{"B", color.B},
{"A", color.A}
};
jo.WriteTo(writer);
}
private static Color? ReadString(string? value)
{
return ParseColor(value);
}
private static bool TryReadInt(JObject obj, string path, out int result)
{
if (!obj.TryGetValue(path, StringComparison.OrdinalIgnoreCase, out var token))
{
result = default;
return false;
}
result = token.Value<int>();
return true;
}
private static Color? ReadObject(JObject? obj, string path)
{
if (obj is null)
return null;
try
{
if (!TryReadInt(obj, "R", out int R) ||
!TryReadInt(obj, "G", out int G) ||
!TryReadInt(obj, "B", out int B)
)
return null;
if (TryReadInt(obj, "A", out int A))
return new Color(R, G, B, A);
return new Color(R, G, B);
}
catch (Exception ex)
{
throw new JsonReaderException($"Can't parse Color? from JSON object node (path: {path}).", ex);
}
}
#endregion
}
}