-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor4D.cs
More file actions
377 lines (349 loc) · 12.9 KB
/
Copy pathColor4D.cs
File metadata and controls
377 lines (349 loc) · 12.9 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
/*
* Copyright (c) 2012 Nicholas Woodfield
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Globalization;
using System.Runtime.InteropServices;
namespace Assimp {
/// <summary>
/// Represents a Red-Green-Blue-Alpha (RGBA) color.
/// Color values range from 0 to 1.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Color4D : IEquatable<Color4D> {
/// <summary>
/// Red component.
/// </summary>
public float R;
/// <summary>
/// Green component.
/// </summary>
public float G;
/// <summary>
/// Blue component.
/// </summary>
public float B;
/// <summary>
/// Alpha component.
/// </summary>
public float A;
/// <summary>
/// Gets or sets the component value at the specified zero-based index
/// in the order of RGBA (index 0 access R, 1 access G, etc). If
/// the index is not in range, a value of zero is returned.
/// </summary>
/// <param name="index">Zero-based index.</param>
/// <returns>The component value</returns>
public float this[int index] {
get {
switch(index) {
case 0:
return R;
case 1:
return G;
case 2:
return B;
case 3:
return A;
default:
return 0;
}
}
set {
switch(index) {
case 0:
R = value;
break;
case 1:
G = value;
break;
case 2:
B = value;
break;
case 3:
A = value;
break;
}
}
}
/// <summary>
/// Constructs a Color4D.
/// </summary>
/// <param name="r">Red component</param>
/// <param name="g">Green component</param>
/// <param name="b">Blue component</param>
/// <param name="a">Alpha component</param>
public Color4D(float r, float g, float b, float a) {
R = r;
G = g;
B = b;
A = a;
}
/// <summary>
/// Constructs a Color4D. Alpha is set to 1.0.
/// </summary>
/// <param name="r">Red component</param>
/// <param name="g">Green component</param>
/// <param name="b">Blue component</param>
public Color4D(float r, float g, float b) {
R = r;
G = g;
B = b;
A = 1.0f;
}
/// <summary>
/// Constructs a Color4D where each component is
/// set to the same value.
/// </summary>
/// <param name="value">Value to set R, G, B, A components</param>
public Color4D(float value) {
R = value;
G = value;
B = value;
A = value;
}
/// <summary>
/// Determines if the color is black, or close to being black.
/// </summary>
/// <returns>True if the color is black/nearly block, false otherwise.</returns>
public bool IsBlack() {
//Don't care about alpha
float epsilon = 10e-3f;
return (float)Math.Abs(R) < epsilon
&& (float)Math.Abs(G) < epsilon
&& (float)Math.Abs(B) < epsilon;
}
/// <summary>
/// Adds the two colors together.
/// </summary>
/// <param name="a">First color</param>
/// <param name="b">Second color</param>
/// <returns>Added color</returns>
public static Color4D operator+(Color4D a, Color4D b) {
Color4D c;
c.R = a.R + b.R;
c.G = a.G + b.G;
c.B = a.B + b.B;
c.A = a.A + b.A;
return c;
}
/// <summary>
/// Adds the value to each of the components of the color.
/// </summary>
/// <param name="color">Source color</param>
/// <param name="value">Value to add to each component</param>
/// <returns>Added color</returns>
public static Color4D operator+(Color4D color, float value) {
Color4D c;
c.R = color.R + value;
c.G = color.G + value;
c.B = color.B + value;
c.A = color.A + value;
return c;
}
/// <summary>
/// Adds the value to each of the components of the color.
/// </summary>
/// <param name="value">Value to add to each component</param>
/// <param name="color">Source color</param>
/// <returns>Added color</returns>
public static Color4D operator+(float value, Color4D color) {
Color4D c;
c.R = color.R + value;
c.G = color.G + value;
c.B = color.B + value;
c.A = color.A + value;
return c;
}
/// <summary>
/// Subtracts the second color from the first color.
/// </summary>
/// <param name="a">First color</param>
/// <param name="b">Second color</param>
/// <returns>Resulting color</returns>
public static Color4D operator-(Color4D a, Color4D b) {
Color4D c;
c.R = a.R - b.R;
c.G = a.G - b.G;
c.B = a.B - b.B;
c.A = a.A - b.A;
return c;
}
/// <summary>
/// Subtracts the value from each of the color's components.
/// </summary>
/// <param name="color">Source color</param>
/// <param name="value">Value to subtract from each component</param>
/// <returns>Resulting color</returns>
public static Color4D operator-(Color4D color, float value) {
Color4D c;
c.R = color.R - value;
c.G = color.G - value;
c.B = color.B - value;
c.A = color.A - value;
return c;
}
/// <summary>
/// Subtracts the color's components from the value, returning
/// the result as a new color. Same as <c>new Color4D(value) - color</c>
/// </summary>
/// <param name="value">Value for each component of the first color</param>
/// <param name="color">Second color</param>
/// <returns>Resulting color</returns>
public static Color4D operator-(float value, Color4D color) {
Color4D c;
c.R = value - color.R;
c.G = value - color.G;
c.B = value - color.B;
c.A = value - color.A;
return c;
}
/// <summary>
/// Multiplies the two colors.
/// </summary>
/// <param name="a">First color</param>
/// <param name="b">Second color</param>
/// <returns>Multiplied color.</returns>
public static Color4D operator*(Color4D a, Color4D b) {
Color4D c;
c.R = a.R * b.R;
c.G = a.G * b.G;
c.B = a.B * b.B;
c.A = a.A * b.A;
return c;
}
/// <summary>
/// Multiplies the color by a scalar value, component wise.
/// </summary>
/// <param name="value">Source color</param>
/// <param name="scale">Scalar value</param>
/// <returns>Resulting color</returns>
public static Color4D operator*(Color4D value, float scale) {
Color4D c;
c.R = value.R * scale;
c.G = value.G * scale;
c.B = value.B * scale;
c.A = value.A * scale;
return c;
}
/// <summary>
/// Multiplies the color by a scalar value, component wise.
/// </summary>
/// <param name="scale">Scalar value</param>
/// <param name="value">Source color</param>
/// <returns>Resulting color</returns>
public static Color4D operator*(float scale, Color4D value) {
Color4D c;
c.R = value.R * scale;
c.G = value.G * scale;
c.B = value.B * scale;
c.A = value.A * scale;
return c;
}
/// <summary>
/// Divides the first color by the second color, component wise.
/// </summary>
/// <param name="a">First color</param>
/// <param name="b">Second color</param>
/// <returns>Resulting color</returns>
public static Color4D operator/(Color4D a, Color4D b) {
Color4D c;
c.R = a.R / b.R;
c.G = a.G / b.G;
c.B = a.B / b.B;
c.A = a.A / b.A;
return c;
}
/// <summary>
/// Divides the color by a divisor value.
/// </summary>
/// <param name="color">Source color</param>
/// <param name="divisor">Divisor</param>
/// <returns>Resulting color</returns>
public static Color4D operator/(Color4D color, float divisor) {
float invDivisor = 1.0f / divisor;
Color4D c;
c.R = color.R * invDivisor;
c.G = color.G * invDivisor;
c.B = color.B * invDivisor;
c.A = color.A * invDivisor;
return c;
}
/// <summary>
/// Tets equality between two colors.
/// </summary>
/// <param name="a">First color</param>
/// <param name="b">Second color</param>
/// <returns>True if the colors are equal, false otherwise</returns>
public static bool operator==(Color4D a, Color4D b) {
return (a.R == b.R) && (a.G == b.G) && (a.B == b.B) && (a.A == b.A);
}
/// <summary>
/// Tets inequality between two colors.
/// </summary>
/// <param name="a">First color</param>
/// <param name="b">Second color</param>
/// <returns>True if the colors are not equal, false otherwise</returns>
public static bool operator!=(Color4D a, Color4D b) {
return (a.R != b.R) || (a.G != b.G) || (a.B != b.B) || (a.A != b.A);
}
/// <summary>
/// Tests equality between this color and another color
/// </summary>
/// <param name="other">Color to test against</param>
/// <returns>True if components are equal</returns>
public bool Equals(Color4D other) {
return (R == other.R) && (G == other.G) && (B == other.B) && (A == other.A);
}
/// <summary>
/// Tests equality between this color and another object.
/// </summary>
/// <param name="obj">Object to test against</param>
/// <returns>True if the object is a color and the components are equal</returns>
public override bool Equals(object obj) {
if(obj is Color4D) {
return Equals((Color4D) obj);
}
return false;
}
/// <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() {
return R.GetHashCode() + G.GetHashCode() + B.GetHashCode() + A.GetHashCode();
}
/// <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() {
CultureInfo info = CultureInfo.CurrentCulture;
return String.Format(info, "{{R:{0} G:{1} B:{2} A:{3}}}",
new Object[] { R.ToString(info), G.ToString(info), B.ToString(info), A.ToString(info) });
}
}
}