/
OxyColor.cs
475 lines (429 loc) · 15.8 KB
/
OxyColor.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OxyColor.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Describes a color in terms of alpha, red, green, and blue channels.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot
{
using System;
using System.Globalization;
/// <summary>
/// Describes a color in terms of alpha, red, green, and blue channels.
/// </summary>
public struct OxyColor : ICodeGenerating, IEquatable<OxyColor>
{
/// <summary>
/// The red component.
/// </summary>
private readonly byte r;
/// <summary>
/// The green component.
/// </summary>
private readonly byte g;
/// <summary>
/// The blue component.
/// </summary>
private readonly byte b;
/// <summary>
/// The alpha component.
/// </summary>
private readonly byte a;
/// <summary>
/// Initializes a new instance of the <see cref="OxyColor"/> struct.
/// </summary>
/// <param name="a">The alpha value.</param>
/// <param name="r">The red value.</param>
/// <param name="g">The green value.</param>
/// <param name="b">The blue value.</param>
private OxyColor(byte a, byte r, byte g, byte b)
{
this.a = a;
this.r = r;
this.g = g;
this.b = b;
}
/// <summary>
/// Gets the alpha value.
/// </summary>
/// <value>The alpha value.</value>
public byte A
{
get
{
return this.a;
}
}
/// <summary>
/// Gets the blue value.
/// </summary>
/// <value>The blue value.</value>
public byte B
{
get
{
return this.b;
}
}
/// <summary>
/// Gets the green value.
/// </summary>
/// <value>The green value.</value>
public byte G
{
get
{
return this.g;
}
}
/// <summary>
/// Gets the red value.
/// </summary>
/// <value>The red value.</value>
public byte R
{
get
{
return this.r;
}
}
/// <summary>
/// Parse a string.
/// </summary>
/// <param name="value">The string in the format <c>"#FFFFFF00"</c> or <c>"255,200,180,50"</c>.</param>
/// <returns>The parsed color.</returns>
/// <exception cref="System.FormatException">Invalid format.</exception>
public static OxyColor Parse(string value)
{
value = value.Trim();
if (value.StartsWith("#"))
{
value = value.Trim('#');
var u = uint.Parse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
if (value.Length < 8)
{
// alpha value was not specified
u += 0xFF000000;
}
return FromUInt32(u);
}
var values = value.Split(',');
if (values.Length < 3 || values.Length > 4)
{
throw new FormatException("Invalid format.");
}
var i = 0;
byte alpha = 255;
if (values.Length > 3)
{
alpha = byte.Parse(values[i++], CultureInfo.InvariantCulture);
}
var red = byte.Parse(values[i++], CultureInfo.InvariantCulture);
var green = byte.Parse(values[i++], CultureInfo.InvariantCulture);
var blue = byte.Parse(values[i], CultureInfo.InvariantCulture);
return FromArgb(alpha, red, green, blue);
}
/// <summary>
/// Calculates the difference between two <see cref="OxyColor" />s
/// </summary>
/// <param name="c1">The first color.</param>
/// <param name="c2">The second color.</param>
/// <returns>L2-norm in ARGB space</returns>
public static double ColorDifference(OxyColor c1, OxyColor c2)
{
// http://en.wikipedia.org/wiki/OxyColor_difference
// http://mathworld.wolfram.com/L2-Norm.html
double dr = (c1.R - c2.R) / 255.0;
double dg = (c1.G - c2.G) / 255.0;
double db = (c1.B - c2.B) / 255.0;
double da = (c1.A - c2.A) / 255.0;
double e = (dr * dr) + (dg * dg) + (db * db) + (da * da);
return Math.Sqrt(e);
}
/// <summary>
/// Convert an <see cref="uint" /> to a <see cref="OxyColor" />.
/// </summary>
/// <param name="color">The unsigned integer color value.</param>
/// <returns>The <see cref="OxyColor" />.</returns>
[CLSCompliant(false)]
public static OxyColor FromUInt32(uint color)
{
var a = (byte)(color >> 24);
var r = (byte)(color >> 16);
var g = (byte)(color >> 8);
var b = (byte)(color >> 0);
return FromArgb(a, r, g, b);
}
/// <summary>
/// Creates a OxyColor from the specified HSV array.
/// </summary>
/// <param name="hsv">The HSV value array.</param>
/// <returns>A OxyColor.</returns>
public static OxyColor FromHsv(double[] hsv)
{
if (hsv.Length != 3)
{
throw new InvalidOperationException("Wrong length of hsv array.");
}
return FromHsv(hsv[0], hsv[1], hsv[2]);
}
/// <summary>
/// Converts from HSV to <see cref="OxyColor" />
/// </summary>
/// <param name="hue">The hue value [0,1]</param>
/// <param name="sat">The saturation value [0,1]</param>
/// <param name="val">The intensity value [0,1]</param>
/// <returns>The <see cref="OxyColor" />.</returns>
/// <remarks>See <a href="http://en.wikipedia.org/wiki/HSL_Color_space">Wikipedia</a>.</remarks>
public static OxyColor FromHsv(double hue, double sat, double val)
{
double g, b;
double r = g = b = 0;
if (sat.Equals(0))
{
// Gray scale
r = g = b = val;
}
else
{
if (hue.Equals(1))
{
hue = 0;
}
hue *= 6.0;
var i = (int)Math.Floor(hue);
double f = hue - i;
double aa = val * (1 - sat);
double bb = val * (1 - (sat * f));
double cc = val * (1 - (sat * (1 - f)));
switch (i)
{
case 0:
r = val;
g = cc;
b = aa;
break;
case 1:
r = bb;
g = val;
b = aa;
break;
case 2:
r = aa;
g = val;
b = cc;
break;
case 3:
r = aa;
g = bb;
b = val;
break;
case 4:
r = cc;
g = aa;
b = val;
break;
case 5:
r = val;
g = aa;
b = bb;
break;
}
}
return FromRgb((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
/// <summary>
/// Calculate the difference in hue between two <see cref="OxyColor" />s.
/// </summary>
/// <param name="c1">The first color.</param>
/// <param name="c2">The second color.</param>
/// <returns>The hue difference.</returns>
public static double HueDifference(OxyColor c1, OxyColor c2)
{
var hsv1 = c1.ToHsv();
var hsv2 = c2.ToHsv();
double dh = hsv1[0] - hsv2[0];
// clamp to [-0.5,0.5]
if (dh > 0.5)
{
dh -= 1.0;
}
if (dh < -0.5)
{
dh += 1.0;
}
double e = dh * dh;
return Math.Sqrt(e);
}
/// <summary>
/// Creates a color defined by an alpha value and another color.
/// </summary>
/// <param name="a">Alpha value.</param>
/// <param name="color">The original color.</param>
/// <returns>A color.</returns>
public static OxyColor FromAColor(byte a, OxyColor color)
{
return FromArgb(a, color.R, color.G, color.B);
}
/// <summary>
/// Creates a color from the specified ARGB values.
/// </summary>
/// <param name="a">The alpha value.</param>
/// <param name="r">The red value.</param>
/// <param name="g">The green value.</param>
/// <param name="b">The blue value.</param>
/// <returns>A color.</returns>
public static OxyColor FromArgb(byte a, byte r, byte g, byte b)
{
return new OxyColor(a, r, g, b);
}
/// <summary>
/// Creates a new <see cref="OxyColor" /> structure from the specified RGB values.
/// </summary>
/// <param name="r">The red value.</param>
/// <param name="g">The green value.</param>
/// <param name="b">The blue value.</param>
/// <returns>A <see cref="OxyColor" /> structure with the specified values and an alpha channel value of 1.</returns>
public static OxyColor FromRgb(byte r, byte g, byte b)
{
// ReSharper restore InconsistentNaming
return new OxyColor(255, r, g, b);
}
/// <summary>
/// Interpolates the specified colors.
/// </summary>
/// <param name="color1">The color1.</param>
/// <param name="color2">The color2.</param>
/// <param name="t">The t.</param>
/// <returns>The interpolated color</returns>
public static OxyColor Interpolate(OxyColor color1, OxyColor color2, double t)
{
double a = (color1.A * (1 - t)) + (color2.A * t);
double r = (color1.R * (1 - t)) + (color2.R * t);
double g = (color1.G * (1 - t)) + (color2.G * t);
double b = (color1.B * (1 - t)) + (color2.B * t);
return FromArgb((byte)a, (byte)r, (byte)g, (byte)b);
}
/// <summary>
/// Determines whether the specified colors are equal to each other.
/// </summary>
/// <param name="first">The first color.</param>
/// <param name="second">The second color.</param>
/// <returns><c>true</c> if the two colors are equal; otherwise, <c>false</c> .</returns>
public static bool operator ==(OxyColor first, OxyColor second)
{
return first.Equals(second);
}
/// <summary>
/// Determines whether the specified colors are not equal to each other.
/// </summary>
/// <param name="first">The first color.</param>
/// <param name="second">The second color.</param>
/// <returns><c>true</c> if the two colors are not equal; otherwise, <c>false</c> .</returns>
public static bool operator !=(OxyColor first, OxyColor second)
{
return !first.Equals(second);
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c> .</returns>
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj.GetType() != typeof(OxyColor))
{
return false;
}
return this.Equals((OxyColor)obj);
}
/// <summary>
/// Determines whether the specified <see cref="OxyColor" /> is equal to this instance.
/// </summary>
/// <param name="other">The <see cref="OxyColor" /> to compare with this instance.</param>
/// <returns><c>true</c> if the specified <see cref="OxyColor" /> is equal to this instance; otherwise, <c>false</c> .</returns>
public bool Equals(OxyColor other)
{
return other.A == this.A && other.R == this.R && other.G == this.G && other.B == this.B;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode()
{
unchecked
{
int result = this.A.GetHashCode();
result = (result * 397) ^ this.R.GetHashCode();
result = (result * 397) ^ this.G.GetHashCode();
result = (result * 397) ^ this.B.GetHashCode();
return result;
}
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
return string.Format(
CultureInfo.InvariantCulture, "#{0:x2}{1:x2}{2:x2}{3:x2}", this.A, this.R, this.G, this.B);
}
/// <summary>
/// Determines whether this color is invisible.
/// </summary>
/// <returns><c>True</c> if the alpha value is 0.</returns>
public bool IsInvisible()
{
return this.A == 0;
}
/// <summary>
/// Determines whether this color is visible.
/// </summary>
/// <returns><c>True</c> if the alpha value is greater than 0.</returns>
public bool IsVisible()
{
return this.A > 0;
}
/// <summary>
/// Determines whether this color is undefined.
/// </summary>
/// <returns><c>True</c> if the color equals <see cref="OxyColors.Undefined" />.</returns>
public bool IsUndefined()
{
return this.Equals(OxyColors.Undefined);
}
/// <summary>
/// Determines whether this color is automatic.
/// </summary>
/// <returns><c>True</c> if the color equals <see cref="OxyColors.Automatic" />.</returns>
public bool IsAutomatic()
{
return this.Equals(OxyColors.Automatic);
}
/// <summary>
/// Gets the actual color.
/// </summary>
/// <param name="defaultColor">The default color.</param>
/// <returns>The default color if the current color equals OxyColors.Automatic, otherwise the color itself.</returns>
public OxyColor GetActualColor(OxyColor defaultColor)
{
return this.IsAutomatic() ? defaultColor : this;
}
/// <summary>
/// Returns C# code that generates this instance.
/// </summary>
/// <returns>The C# code.</returns>
string ICodeGenerating.ToCode()
{
return this.ToCode();
}
}
}