-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PalEntry.cs
406 lines (369 loc) · 11.5 KB
/
PalEntry.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System;
using System.Text;
/// <summary>
/// Associates a color with a name in a palette.
/// </summary>
[Serializable]
public class PalEntry : IComparable<PalEntry>, IEquatable<PalEntry>
{
/// <summary>
/// Character limit for entry names.
/// </summary>
public const int NameCharLimit = 64;
/// <summary>
/// The entry's color.
/// </summary>
protected Lab color;
/// <summary>
/// The entry's name.
/// </summary>
protected string name;
/// <summary>
/// The entry's color.
/// If the color's alpha is less than
/// zero, clear black is used instead.
/// </summary>
/// <value>color</value>
public Lab Color
{
get
{
return this.color;
}
set
{
this.color = Lab.None(value) ? Lab.ClearBlack : value;
}
}
/// <summary>
/// The entry's name.
/// If the name is invalid, the entry's color
/// in web-friendly hexadcimal is used instead.
/// </summary>
/// <value>name</value>
public string Name
{
get
{
return this.name;
}
set
{
string trval = value.Trim();
if (trval.Length > 0)
{
this.name = trval[..Utils.Min(trval.Length, PalEntry.NameCharLimit)];
}
else
{
this.name = Rgb.ToHexWeb(Rgb.SrLab2ToStandard(this.color));
}
}
}
/// <summary>
/// Constructs an empty entry.
/// </summary>
public PalEntry()
{
this.color = Lab.ClearBlack;
this.name = "Empty";
}
/// <summary>
/// Constructs an entry from a color and a name.
/// </summary>
/// <param name="color">color</param>
/// <param name="name">name</param>
public PalEntry(in Lab color, in string name = "")
{
this.Color = color;
this.Name = name;
}
/// <summary>
/// Tests this entry for equivalence with an object.
/// </summary>
/// <param name="value">the object</param>
/// <returns>equivalence</returns>
public override bool Equals(object value)
{
if (Object.ReferenceEquals(this, value)) { return true; }
if (value is null) { return false; }
if (value is PalEntry entry) { return this.Equals(entry); }
return false;
}
/// <summary>
/// Returns a hash code for this entry based on its color.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
if (Lab.None(this.color)) { return 0; }
return this.color.GetHashCode();
}
/// <summary>
/// Returns a string representation of this entry.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return PalEntry.ToString(this);
}
/// <summary>
/// Compares entries according to their colors.
/// </summary>
/// <param name="pe">entry</param>
/// <returns>comparison</returns>
public int CompareTo(PalEntry pe)
{
if (pe is null) { return 1; }
bool leftZeroAlpha = Lab.None(this.color);
bool rightZeroAlpha = Lab.None(pe.color);
if (leftZeroAlpha && rightZeroAlpha) { return 0; }
if (leftZeroAlpha) { return -1; }
if (rightZeroAlpha) { return 1; }
int hashLeft = this.GetHashCode();
int hashRight = pe.GetHashCode();
return (hashLeft < hashRight) ? -1 :
(hashLeft > hashRight) ? 1 :
0;
}
/// <summary>
/// Tests this entry for equivalence with another in compliance with the
/// IEquatable interface.
/// </summary>
/// <param name="pe">palette entry</param>
/// <returns>evaluation</returns>
public bool Equals(PalEntry pe)
{
if (pe is null) { return false; }
return this.GetHashCode() == pe.GetHashCode();
}
/// <summary>
/// Sets an entry from a color and a name.
/// </summary>
/// <param name="color">color</param>
/// <param name="name">name</param>
/// <returns>this entry</returns>
public PalEntry Set(in Lab color, in string name)
{
this.Color = color;
this.Name = name;
return this;
}
/// <summary>
/// Converts a color to a palette entry.
/// </summary>
/// <param name="c">color</param>
public static implicit operator PalEntry(in Lab c)
{
return new PalEntry(c, "");
}
/// <summary>
/// Appends an entry to an array of entries.
/// </summary>
/// <param name="a">array</param>
/// <param name="b">entry</param>
/// <returns>array</returns>
public static PalEntry[] Append(in PalEntry[] a, in PalEntry b)
{
bool aNull = a == null;
bool bNull = b is null;
if (aNull && bNull) { return new PalEntry[] { }; }
if (bNull) { return a; }
if (aNull) { return new PalEntry[] { b }; }
int aLen = a.Length;
PalEntry[] result = new PalEntry[aLen + 1];
System.Array.Copy(a, 0, result, 0, aLen);
result[aLen] = b;
return result;
}
/// <summary>
/// Inserts an entry into an array of entries at an index.
/// </summary>
/// <param name="a">array</param>
/// <param name="index">index</param>
/// <param name="b">entry</param>
/// <returns>array</returns>
public static PalEntry[] Insert(in PalEntry[] a, in int index, in PalEntry b)
{
bool aNull = a is null;
bool bNull = b is null;
if (aNull && bNull) { return new PalEntry[] { }; }
if (bNull) { return a; }
if (aNull) { return new PalEntry[] { b }; }
int aLen = a.Length;
int valIdx = Utils.RemFloor(index, aLen + 1);
PalEntry[] result = new PalEntry[aLen + 1];
System.Array.Copy(a, 0, result, 0, valIdx);
result[valIdx] = b;
System.Array.Copy(a, valIdx, result, valIdx + 1, aLen - valIdx);
return result;
}
/// <summary>
/// Prepends an entry to an array of entries.
/// </summary>
/// <param name="a">array</param>
/// <param name="b">entry</param>
/// <returns>array</returns>
public static PalEntry[] Prepend(in PalEntry[] a, in PalEntry b)
{
bool aNull = a is null;
bool bNull = b is null;
if (aNull && bNull) { return new PalEntry[] { }; }
if (bNull) { return a; }
if (aNull) { return new PalEntry[] { b }; }
int aLen = a.Length;
PalEntry[] result = new PalEntry[aLen + 1];
result[0] = b;
System.Array.Copy(a, 0, result, 1, aLen);
return result;
}
/// <summary>
/// Removes an entry from from an array at an index.
/// </summary>
/// <param name="a">array</param>
/// <param name="index">index</param>
/// <returns>array</returns>
public static (PalEntry[], PalEntry) RemoveAt(in PalEntry[] a, in int index)
{
bool aNull = a == null;
if (aNull) { return (new PalEntry[] { }, null); }
int aLen = a.Length;
if (aLen < 1) { return (new PalEntry[] { }, null); }
if (aLen < 2) { return (new PalEntry[] { }, a[0]); }
int valIdx = Utils.RemFloor(index, aLen);
PalEntry[] result = new PalEntry[aLen - 1];
System.Array.Copy(a, 0, result, 0, valIdx);
System.Array.Copy(a, valIdx + 1, result, valIdx, aLen - 1 - valIdx);
return (result, a[valIdx]);
}
/// <summary>
/// Resizes an array of entries.
/// </summary>
/// <param name="arr">array</param>
/// <param name="sz">new size</param>
/// <returns>resized array</returns>
public static PalEntry[] Resize(in PalEntry[] arr, in int sz)
{
if (sz < 1) { return new PalEntry[] { }; }
PalEntry[] result = new PalEntry[sz];
if (arr == null)
{
for (int i = 0; i < sz; ++i) { result[i] = new PalEntry(); }
return result;
}
int last = arr.Length - 1;
for (int i = 0; i < sz; ++i)
{
if (i > last || arr[i] is null)
{
result[i] = new PalEntry();
}
else
{
result[i] = arr[i];
}
}
return result;
}
/// <summary>
/// Returns a string representation of an entry
/// suitable for a GPL file extension.
/// </summary>
/// <param name="pe">entry</param>
/// <returns>string</returns>
public static string ToGplString(in PalEntry pe)
{
return PalEntry.ToGplString(
new StringBuilder(16 + PalEntry.NameCharLimit), pe,
(x) => Rgb.Clamp(x, 0.0f, 1.0f)).ToString();
}
/// <summary>
/// Appends a representation of an entry to a string builder,
/// formatted for the GPL file extension.
/// </summary>
/// <param name="sb">string builder</param>
/// <param name="pe">entry</param>
/// <param name="tm">tone mapper</param>
/// <returns>string builder</returns>
public static StringBuilder ToGplString(
in StringBuilder sb,
in PalEntry pe,
in Func<Rgb, Rgb> tm)
{
PalEntry.ToPalString(sb, pe, tm);
sb.Append(' ');
sb.Append(pe.Name);
return sb;
}
/// <summary>
/// Returns a string representation of an entry
/// suitable for a PAL file extension.
/// </summary>
/// <param name="pe">entry</param>
/// <returns>string</returns>
public static string ToPalString(in PalEntry pe)
{
return PalEntry.ToPalString(
new StringBuilder(16), pe,
(x) => Rgb.Clamp(x, 0.0f, 1.0f)).ToString();
}
/// <summary>
/// Appends a representation of an entry to a string builder,
/// formatted for the PAL file extension.
/// </summary>
/// <param name="sb">string builder</param>
/// <param name="pe">entry</param>
/// <param name="tm">tone mapper</param>
/// <returns>string builder</returns>
public static StringBuilder ToPalString(
in StringBuilder sb,
in PalEntry pe,
in Func<Rgb, Rgb> tm)
{
Lab lab = pe.color;
Rgb c = Rgb.SrLab2ToStandard(lab);
Rgb ctm = tm(c);
int r = (int)(ctm.R * 255.0f + 0.5f);
int g = (int)(ctm.G * 255.0f + 0.5f);
int b = (int)(ctm.B * 255.0f + 0.5f);
sb.Append(r.ToString().PadLeft(3, ' '));
sb.Append(' ');
sb.Append(g.ToString().PadLeft(3, ' '));
sb.Append(' ');
sb.Append(b.ToString().PadLeft(3, ' '));
return sb;
}
/// <summary>
/// Returns a string representation of an entry.
/// </summary>
/// <param name="pe">entry</param>
/// <param name="places">number of decimal places</param>
/// <returns>string</returns>
public static string ToString(
in PalEntry pe,
in int places = 4)
{
return PalEntry.ToString(
new StringBuilder(128), pe, places).ToString();
}
/// <summary>
/// Appends a representation of an entry to a string builder.
/// </summary>
/// <param name="sb">string builder</param>
/// <param name="pe">entry</param>
/// <param name="places">number of decimal places</param>
/// <returns>string builder</returns>
public static StringBuilder ToString(
in StringBuilder sb,
in PalEntry pe,
in int places = 4)
{
sb.Append("{\"color\":");
Lab.ToString(sb, pe.color, places);
sb.Append(",\"name\":\"");
sb.Append(pe.name);
sb.Append('\"');
sb.Append('}');
return sb;
}
}