forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.cs
More file actions
387 lines (356 loc) · 10.1 KB
/
Color.cs
File metadata and controls
387 lines (356 loc) · 10.1 KB
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
#if NETCOREAPP1_0
using System;
using System.Text;
namespace JavaScriptEngineSwitcher.Tests.Interop.Drawing
{
/// <summary>
/// Represents an ARGB (alpha, red, green, blue) color
/// </summary>
public struct Color
{
private readonly string _name;
private readonly long _value;
private readonly short _knownColor;
private readonly short _state;
private static short StateKnownColorValid = 1;
private static short StateARGBValueValid = 2;
private static short StateValueMask = StateARGBValueValid;
private static short StateNameValid = 8;
private static long NotDefinedValue = 0L;
public static readonly Color Empty = new Color();
/// <summary>
/// Gets a value indicating whether this <see cref="Color"/> structure is a predefined color.
/// Predefined colors are represented by the elements of the <see cref="KnownColor"/> enumeration.
/// </summary>
/// <returns>true if this <see cref="Color"/> was created from a predefined color by using either
/// the <code>FromName</code> method or the <code>FromKnownColor</code> method; otherwise, false.</returns>
public bool IsKnownColor
{
get
{
return ((uint)_state & (uint)StateKnownColorValid) > 0U;
}
}
/// <summary>
/// Gets a name of this <see cref="Color"/>
/// </summary>
/// <returns>The name of this <see cref="Color"/></returns>
public string Name
{
get
{
if ((_state & StateNameValid) != 0)
{
return _name;
}
if (IsKnownColor)
{
var knownColor = (KnownColor)_knownColor;
return KnownColorTable.KnownColorToName(knownColor) ?? knownColor.ToString();
}
return Convert.ToString(_value, 16);
}
}
private long Value
{
get
{
if ((_state & StateValueMask) != 0)
{
return _value;
}
if (IsKnownColor)
{
var knownColor = (KnownColor)_knownColor;
return KnownColorTable.KnownColorToArgb(knownColor);
}
return NotDefinedValue;
}
}
/// <summary>
/// Gets a system-defined color that has an ARGB value of #FFFF4500
/// </summary>
/// <returns>A <see cref="Color"/> representing a system-defined color</returns>
public static Color OrangeRed
{
get
{
return new Color(KnownColor.OrangeRed);
}
}
/// <summary>
/// Gets a red component value of this <see cref="Color"/> structure
/// </summary>
/// <returns>The red component value of this <see cref="Color"/></returns>
public byte R
{
get
{
return (byte)((ulong)(Value >> 16) & byte.MaxValue);
}
}
/// <summary>
/// Gets a green component value of this <see cref="Color"/> structure
/// </summary>
/// <returns>The green component value of this <see cref="Color"/></returns>
public byte G
{
get
{
return (byte)((ulong)(Value >> 8) & byte.MaxValue);
}
}
/// <summary>
/// Gets a blue component value of this <see cref="Color"/> structure
/// </summary>
/// <returns>The blue component value of this <see cref="Color"/></returns>
public byte B
{
get
{
return (byte)((ulong)Value & byte.MaxValue);
}
}
/// <summary>
/// Gets a alpha component value of this <see cref="Color"/> structure
/// </summary>
/// <returns>The alpha component value of this <see cref="Color"/></returns>
public byte A
{
get
{
return (byte)((ulong)(Value >> 24) & byte.MaxValue);
}
}
internal Color(KnownColor knownColor)
{
_value = 0L;
_state = StateKnownColorValid;
_name = null;
_knownColor = (short)knownColor;
}
private Color(long value, short state, string name, KnownColor knownColor)
{
_value = value;
_state = state;
_name = name;
_knownColor = (short)knownColor;
}
private static void CheckByte(int value, string name)
{
if (value < 0 || value > byte.MaxValue)
{
throw new ArgumentException(string.Format(
"Value of '{1}' is not valid for '{0}'. '{0}' should be greater than or equal to {2} and less than or equal to {3}.",
name, value, 0, (int)byte.MaxValue
));
}
}
private static long MakeArgb(byte alpha, byte red, byte green, byte blue)
{
return (uint)(red << 16 | green << 8 | blue | alpha << 24) & (long)uint.MaxValue;
}
/// <summary>
/// Creates a <see cref="Color"/> structure from the specified 8-bit color values (red, green, and blue).
/// The alpha value is implicitly 255 (fully opaque).
/// Although this method allows a 32-bit value to be passed for each color component, the value
/// of each component is limited to 8 bits.
/// </summary>
/// <param name="red">The red component value for the new <see cref="Color"/>.
/// Valid values are 0 through 255.</param>
/// <param name="green">The green component value for the new <see cref="Color"/>.
/// Valid values are 0 through 255.</param>
/// <param name="blue">The blue component value for the new <see cref="Color"/>.
/// Valid values are 0 through 255.</param>
/// <returns>The <see cref="Color"/> that this method creates</returns>
/// <exception cref="ArgumentException"><paramref name="red"/>, <paramref name="green"/>, or
/// <paramref name="blue"/> is less than 0 or greater than 255.</exception>
public static Color FromArgb(int red, int green, int blue)
{
return FromArgb(byte.MaxValue, red, green, blue);
}
/// <summary>
/// Creates a <see cref="Color"/> structure from the four ARGB component (alpha, red, green, and blue) values.
/// Although this method allows a 32-bit value to be passed for each component, the value of
/// each component is limited to 8 bits.
/// </summary>
/// <param name="alpha">The alpha component. Valid values are 0 through 255.</param>
/// <param name="red">The red component. Valid values are 0 through 255.</param>
/// <param name="green">The green component. Valid values are 0 through 255.</param>
/// <param name="blue">The blue component. Valid values are 0 through 255.</param>
/// <returns>The <see cref="Color"/> that this method creates</returns>
/// <exception cref="ArgumentException"><paramref name="alpha"/>, <paramref name="red"/>,
/// <paramref name="green"/>, or <paramref name="blue"/> is less than 0 or greater than 255.</exception>
public static Color FromArgb(int alpha, int red, int green, int blue)
{
CheckByte(alpha, "alpha");
CheckByte(red, "red");
CheckByte(green, "green");
CheckByte(blue, "blue");
return new Color(MakeArgb((byte)alpha, (byte)red, (byte)green, (byte)blue),
StateARGBValueValid, null, 0);
}
/// <summary>
/// Gets a hue-saturation-brightness (HSB) brightness value for this <see cref="Color"/> structure
/// </summary>
/// <returns>The brightness of this <see cref="Color"/>.
/// The brightness ranges from 0.0 through 1.0, where 0.0 represents black and 1.0 represents white.</returns>
public float GetBrightness()
{
double num1 = R / (double)byte.MaxValue;
float num2 = G / (float)byte.MaxValue;
float num3 = B / (float)byte.MaxValue;
float num4 = (float)num1;
float num5 = (float)num1;
if (num2 > num4)
{
num4 = num2;
}
if (num3 > num4)
{
num4 = num3;
}
if (num2 < num5)
{
num5 = num2;
}
if (num3 < num5)
{
num5 = num3;
}
return (float)((num4 + num5) / 2.0);
}
/// <summary>
/// Gets a hue-saturation-brightness (HSB) hue value, in degrees, for this <see cref="Color"/> structure
/// </summary>
/// <returns>The hue, in degrees, of this <see cref="Color"/>.
/// The hue is measured in degrees, ranging from 0.0 through 360.0, in HSB color space.</returns>
public float GetHue()
{
if (R == G && G == B)
{
return 0.0f;
}
float num1 = R / (float)byte.MaxValue;
float num2 = G / (float)byte.MaxValue;
float num3 = B / (float)byte.MaxValue;
float num4 = 0.0f;
float num5 = num1;
float num6 = num1;
if (num2 > num5)
{
num5 = num2;
}
if (num3 > num5)
{
num5 = num3;
}
if (num2 < num6)
{
num6 = num2;
}
if (num3 < num6)
{
num6 = num3;
}
float num7 = num5 - num6;
if (num1 == num5)
{
num4 = (num2 - num3)/num7;
}
else if (num2 == num5)
{
num4 = (float)(2.0 + (num3 - num1) / num7);
}
else if (num3 == num5)
{
num4 = (float)(4.0 + (num1 - num2) / (double)num7);
}
float num8 = num4 * 60f;
if (num8 < 0.0)
{
num8 += 360f;
}
return num8;
}
/// <summary>
/// Gets a hue-saturation-brightness (HSB) saturation value for this <see cref="Color"/> structure
/// </summary>
/// <returns>The saturation of this <see cref="Color"/>.
/// The saturation ranges from 0.0 through 1.0, where 0.0 is grayscale and 1.0 is the most saturated.</returns>
public float GetSaturation()
{
double num1 = R / (double)byte.MaxValue;
float num2 = G / (float)byte.MaxValue;
float num3 = B / (float)byte.MaxValue;
float num4 = 0.0f;
float num5 = (float)num1;
float num6 = (float)num1;
if (num2 > num5)
{
num5 = num2;
}
if (num3 > num5)
{
num5 = num3;
}
if (num2 < num6)
{
num6 = num2;
}
if (num3 < num6)
{
num6 = num3;
}
if (num5 != num6)
{
num4 = (num5 + num6) / 2.0 > 0.5 ?
(float)((num5 -num6) / (2.0 - num5 - num6))
:
(num5 - num6) / (num5 + num6)
;
}
return num4;
}
/// <summary>
/// Converts this <see cref="Color"/> structure to a human-readable string
/// </summary>
/// <returns>A string that is the name of this <see cref="Color"/>, if the <see cref="Color"/>
/// is created from a predefined color by using either the <code>FromName</code> method
/// or the <code>FromKnownColor</code> method;
/// otherwise, a string that consists of the ARGB component names and their values.
/// </returns>
public override string ToString()
{
var sb = new StringBuilder(32);
sb.Append(GetType().Name);
sb.Append(" [");
if ((_state & StateNameValid) != 0)
{
sb.Append(Name);
}
else if ((_state & StateKnownColorValid) != 0)
{
sb.Append(Name);
}
else if ((_state & StateValueMask) != 0)
{
sb.Append("A=");
sb.Append(A);
sb.Append(", R=");
sb.Append(R);
sb.Append(", G=");
sb.Append(G);
sb.Append(", B=");
sb.Append(B);
}
else
{
sb.Append("Empty");
}
sb.Append("]");
return sb.ToString();
}
}
}
#endif