forked from BAndysc/WoWDatabaseEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColor3.cs
866 lines (787 loc) · 35.7 KB
/
Color3.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// 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.
// -----------------------------------------------------------------------------
// Original code from SlimMath project. http://code.google.com/p/slimmath/
// Greetings to SlimDX Group. Original code published with the following license:
// -----------------------------------------------------------------------------
/*
* Copyright (c) 2007-2011 SlimDX Group
*
* 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.CompilerServices;
using System.Runtime.InteropServices;
namespace TheMaths
{
/// <summary>
/// Represents a color in the form of rgb.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Color3 : IEquatable<Color3>, IFormattable
{
private const string toStringFormat = "Red:{0} Green:{1} Blue:{2}";
/// <summary>
/// The Black color (0, 0, 0).
/// </summary>
public static readonly Color3 Black = new Color3(0.0f, 0.0f, 0.0f);
/// <summary>
/// The White color (1, 1, 1, 1).
/// </summary>
public static readonly Color3 White = new Color3(1.0f, 1.0f, 1.0f);
/// <summary>
/// The red component of the color.
/// </summary>
public float Red;
/// <summary>
/// The green component of the color.
/// </summary>
public float Green;
/// <summary>
/// The blue component of the color.
/// </summary>
public float Blue;
/// <summary>
/// Initializes a new instance of the <see cref="Color3"/> struct.
/// </summary>
/// <param name="value">The value that will be assigned to all components.</param>
public Color3(float value)
{
Red = Green = Blue = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="Color3"/> struct.
/// </summary>
/// <param name="red">The red component of the color.</param>
/// <param name="green">The green component of the color.</param>
/// <param name="blue">The blue component of the color.</param>
public Color3(float red, float green, float blue)
{
Red = red;
Green = green;
Blue = blue;
}
/// <summary>
/// Initializes a new instance of the <see cref="Color3"/> struct.
/// </summary>
/// <param name="value">The red, green, and blue components of the color.</param>
public Color3(Vector3 value)
{
Red = value.X;
Green = value.Y;
Blue = value.Z;
}
/// <summary>
/// Initializes a new instance of the <see cref="Color3"/> struct.
/// </summary>
/// <param name="rgb">A packed integer containing all three color components in RGB order.
/// The alpha component is ignored.</param>
public Color3(int rgb)
{
Blue = ((rgb >> 16) & 255) / 255.0f;
Green = ((rgb >> 8) & 255) / 255.0f;
Red = (rgb & 255) / 255.0f;
}
/// <summary>
/// Initializes a new instance of the <see cref="Color3"/> struct.
/// </summary>
/// <param name="values">The values to assign to the red, green, and blue components of the color. This must be an array with three elements.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
public Color3(float[] values)
{
if (values == null)
throw new ArgumentNullException("values");
if (values.Length != 3)
throw new ArgumentOutOfRangeException("values", "There must be three and only three input values for Color3.");
Red = values[0];
Green = values[1];
Blue = values[2];
}
/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the red, green, or blue component, depending on the index.</value>
/// <param name="index">The index of the component to access. Use 0 for the red component, 1 for the green component, and 2 for the blue component.</param>
/// <returns>The value of the component at the specified index.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 2].</exception>
public float this[int index]
{
get
{
switch (index)
{
case 0: return Red;
case 1: return Green;
case 2: return Blue;
}
throw new ArgumentOutOfRangeException("index", "Indices for Color3 run from 0 to 2, inclusive.");
}
set
{
switch (index)
{
case 0: Red = value; break;
case 1: Green = value; break;
case 2: Blue = value; break;
default: throw new ArgumentOutOfRangeException("index", "Indices for Color3 run from 0 to 2, inclusive.");
}
}
}
/// <summary>
/// Converts the color into a packed integer.
/// </summary>
/// <returns>A packed integer containing all three color components.
/// The alpha channel is set to 255.</returns>
public int ToRgba()
{
uint a = 255;
uint r = (uint) (Red * 255.0f) & 255;
uint g = (uint) (Green * 255.0f) & 255;
uint b = (uint) (Blue * 255.0f) & 255;
uint value = r;
value |= g << 8;
value |= b << 16;
value |= a << 24;
return (int)value;
}
/// <summary>
/// Converts the color into a packed integer.
/// </summary>
/// <returns>A packed integer containing all three color components.
/// The alpha channel is set to 255.</returns>
public int ToBgra()
{
uint a = 255;
uint r = (uint)(Red * 255.0f) & 255;
uint g = (uint)(Green * 255.0f) & 255;
uint b = (uint)(Blue * 255.0f) & 255;
uint value = b;
value |= g << 8;
value |= r << 16;
value |= a << 24;
return (int)value;
}
/// <summary>
/// Converts the color into a three component vector.
/// </summary>
/// <returns>A three component vector containing the red, green, and blue components of the color.</returns>
public Vector3 ToVector3()
{
return new Vector3(Red, Green, Blue);
}
/// <summary>
/// Creates an array containing the elements of the color.
/// </summary>
/// <returns>A three-element array containing the components of the color.</returns>
public float[] ToArray()
{
return new float[] { Red, Green, Blue };
}
/// <summary>
/// Adds two colors.
/// </summary>
/// <param name="left">The first color to add.</param>
/// <param name="right">The second color to add.</param>
/// <param name="result">When the method completes, completes the sum of the two colors.</param>
public static void Add(ref Color3 left, ref Color3 right, out Color3 result)
{
result.Red = left.Red + right.Red;
result.Green = left.Green + right.Green;
result.Blue = left.Blue + right.Blue;
}
/// <summary>
/// Adds two colors.
/// </summary>
/// <param name="left">The first color to add.</param>
/// <param name="right">The second color to add.</param>
/// <returns>The sum of the two colors.</returns>
public static Color3 Add(Color3 left, Color3 right)
{
return new Color3(left.Red + right.Red, left.Green + right.Green, left.Blue + right.Blue);
}
/// <summary>
/// Subtracts two colors.
/// </summary>
/// <param name="left">The first color to subtract.</param>
/// <param name="right">The second color to subtract.</param>
/// <param name="result">WHen the method completes, contains the difference of the two colors.</param>
public static void Subtract(ref Color3 left, ref Color3 right, out Color3 result)
{
result.Red = left.Red - right.Red;
result.Green = left.Green - right.Green;
result.Blue = left.Blue - right.Blue;
}
/// <summary>
/// Subtracts two colors.
/// </summary>
/// <param name="left">The first color to subtract.</param>
/// <param name="right">The second color to subtract</param>
/// <returns>The difference of the two colors.</returns>
public static Color3 Subtract(Color3 left, Color3 right)
{
return new Color3(left.Red - right.Red, left.Green - right.Green, left.Blue - right.Blue);
}
/// <summary>
/// Modulates two colors.
/// </summary>
/// <param name="left">The first color to modulate.</param>
/// <param name="right">The second color to modulate.</param>
/// <param name="result">When the method completes, contains the modulated color.</param>
public static void Modulate(ref Color3 left, ref Color3 right, out Color3 result)
{
result.Red = left.Red * right.Red;
result.Green = left.Green * right.Green;
result.Blue = left.Blue * right.Blue;
}
/// <summary>
/// Modulates two colors.
/// </summary>
/// <param name="left">The first color to modulate.</param>
/// <param name="right">The second color to modulate.</param>
/// <returns>The modulated color.</returns>
public static Color3 Modulate(Color3 left, Color3 right)
{
return new Color3(left.Red * right.Red, left.Green * right.Green, left.Blue * right.Blue);
}
/// <summary>
/// Scales a color.
/// </summary>
/// <param name="value">The color to scale.</param>
/// <param name="scale">The amount by which to scale.</param>
/// <param name="result">When the method completes, contains the scaled color.</param>
public static void Scale(ref Color3 value, float scale, out Color3 result)
{
result.Red = value.Red * scale;
result.Green = value.Green * scale;
result.Blue = value.Blue * scale;
}
/// <summary>
/// Scales a color.
/// </summary>
/// <param name="value">The color to scale.</param>
/// <param name="scale">The amount by which to scale.</param>
/// <returns>The scaled color.</returns>
public static Color3 Scale(Color3 value, float scale)
{
return new Color3(value.Red * scale, value.Green * scale, value.Blue * scale);
}
/// <summary>
/// Negates a color.
/// </summary>
/// <param name="value">The color to negate.</param>
/// <param name="result">When the method completes, contains the negated color.</param>
public static void Negate(ref Color3 value, out Color3 result)
{
result.Red = 1.0f - value.Red;
result.Green = 1.0f - value.Green;
result.Blue = 1.0f - value.Blue;
}
/// <summary>
/// Negates a color.
/// </summary>
/// <param name="value">The color to negate.</param>
/// <returns>The negated color.</returns>
public static Color3 Negate(Color3 value)
{
return new Color3(1.0f - value.Red, 1.0f - value.Green, 1.0f - value.Blue);
}
/// <summary>
/// Restricts a value to be within a specified range.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="result">When the method completes, contains the clamped value.</param>
public static void Clamp(ref Color3 value, ref Color3 min, ref Color3 max, out Color3 result)
{
float red = value.Red;
red = (red > max.Red) ? max.Red : red;
red = (red < min.Red) ? min.Red : red;
float green = value.Green;
green = (green > max.Green) ? max.Green : green;
green = (green < min.Green) ? min.Green : green;
float blue = value.Blue;
blue = (blue > max.Blue) ? max.Blue : blue;
blue = (blue < min.Blue) ? min.Blue : blue;
result = new Color3(red, green, blue);
}
/// <summary>
/// Restricts a value to be within a specified range.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The clamped value.</returns>
public static Color3 Clamp(Color3 value, Color3 min, Color3 max)
{
Color3 result;
Clamp(ref value, ref min, ref max, out result);
return result;
}
/// <summary>
/// Performs a linear interpolation between two colors.
/// </summary>
/// <param name="start">Start color.</param>
/// <param name="end">End color.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <param name="result">When the method completes, contains the linear interpolation of the two colors.</param>
/// <remarks>
/// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
/// </remarks>
public static void Lerp(ref Color3 start, ref Color3 end, float amount, out Color3 result)
{
result.Red = MathUtil.Lerp(start.Red, end.Red, amount);
result.Green = MathUtil.Lerp(start.Green, end.Green, amount);
result.Blue = MathUtil.Lerp(start.Blue, end.Blue, amount);
}
/// <summary>
/// Performs a linear interpolation between two colors.
/// </summary>
/// <param name="start">Start color.</param>
/// <param name="end">End color.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <returns>The linear interpolation of the two colors.</returns>
/// <remarks>
/// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
/// </remarks>
public static Color3 Lerp(Color3 start, Color3 end, float amount)
{
Color3 result;
Lerp(ref start, ref end, amount, out result);
return result;
}
/// <summary>
/// Performs a cubic interpolation between two colors.
/// </summary>
/// <param name="start">Start color.</param>
/// <param name="end">End color.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <param name="result">When the method completes, contains the cubic interpolation of the two colors.</param>
public static void SmoothStep(ref Color3 start, ref Color3 end, float amount, out Color3 result)
{
amount = MathUtil.SmoothStep(amount);
Lerp(ref start, ref end, amount, out result);
}
/// <summary>
/// Performs a cubic interpolation between two colors.
/// </summary>
/// <param name="start">Start color.</param>
/// <param name="end">End color.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <returns>The cubic interpolation of the two colors.</returns>
public static Color3 SmoothStep(Color3 start, Color3 end, float amount)
{
Color3 result;
SmoothStep(ref start, ref end, amount, out result);
return result;
}
/// <summary>
/// Returns a color containing the smallest components of the specified colors.
/// </summary>
/// <param name="left">The first source color.</param>
/// <param name="right">The second source color.</param>
/// <param name="result">When the method completes, contains an new color composed of the largest components of the source colors.</param>
public static void Max(ref Color3 left, ref Color3 right, out Color3 result)
{
result.Red = (left.Red > right.Red) ? left.Red : right.Red;
result.Green = (left.Green > right.Green) ? left.Green : right.Green;
result.Blue = (left.Blue > right.Blue) ? left.Blue : right.Blue;
}
/// <summary>
/// Returns a color containing the largest components of the specified colors.
/// </summary>
/// <param name="left">The first source color.</param>
/// <param name="right">The second source color.</param>
/// <returns>A color containing the largest components of the source colors.</returns>
public static Color3 Max(Color3 left, Color3 right)
{
Color3 result;
Max(ref left, ref right, out result);
return result;
}
/// <summary>
/// Returns a color containing the smallest components of the specified colors.
/// </summary>
/// <param name="left">The first source color.</param>
/// <param name="right">The second source color.</param>
/// <param name="result">When the method completes, contains an new color composed of the smallest components of the source colors.</param>
public static void Min(ref Color3 left, ref Color3 right, out Color3 result)
{
result.Red = (left.Red < right.Red) ? left.Red : right.Red;
result.Green = (left.Green < right.Green) ? left.Green : right.Green;
result.Blue = (left.Blue < right.Blue) ? left.Blue : right.Blue;
}
/// <summary>
/// Returns a color containing the smallest components of the specified colors.
/// </summary>
/// <param name="left">The first source color.</param>
/// <param name="right">The second source color.</param>
/// <returns>A color containing the smallest components of the source colors.</returns>
public static Color3 Min(Color3 left, Color3 right)
{
Color3 result;
Min(ref left, ref right, out result);
return result;
}
/// <summary>
/// Adjusts the contrast of a color.
/// </summary>
/// <param name="value">The color whose contrast is to be adjusted.</param>
/// <param name="contrast">The amount by which to adjust the contrast.</param>
/// <param name="result">When the method completes, contains the adjusted color.</param>
public static void AdjustContrast(ref Color3 value, float contrast, out Color3 result)
{
result.Red = 0.5f + contrast * (value.Red - 0.5f);
result.Green = 0.5f + contrast * (value.Green - 0.5f);
result.Blue = 0.5f + contrast * (value.Blue - 0.5f);
}
/// <summary>
/// Adjusts the contrast of a color.
/// </summary>
/// <param name="value">The color whose contrast is to be adjusted.</param>
/// <param name="contrast">The amount by which to adjust the contrast.</param>
/// <returns>The adjusted color.</returns>
public static Color3 AdjustContrast(Color3 value, float contrast)
{
return new Color3(
0.5f + contrast * (value.Red - 0.5f),
0.5f + contrast * (value.Green - 0.5f),
0.5f + contrast * (value.Blue - 0.5f));
}
/// <summary>
/// Adjusts the saturation of a color.
/// </summary>
/// <param name="value">The color whose saturation is to be adjusted.</param>
/// <param name="saturation">The amount by which to adjust the saturation.</param>
/// <param name="result">When the method completes, contains the adjusted color.</param>
public static void AdjustSaturation(ref Color3 value, float saturation, out Color3 result)
{
float grey = value.Red * 0.2125f + value.Green * 0.7154f + value.Blue * 0.0721f;
result.Red = grey + saturation * (value.Red - grey);
result.Green = grey + saturation * (value.Green - grey);
result.Blue = grey + saturation * (value.Blue - grey);
}
/// <summary>
/// Adjusts the saturation of a color.
/// </summary>
/// <param name="value">The color whose saturation is to be adjusted.</param>
/// <param name="saturation">The amount by which to adjust the saturation.</param>
/// <returns>The adjusted color.</returns>
public static Color3 AdjustSaturation(Color3 value, float saturation)
{
float grey = value.Red * 0.2125f + value.Green * 0.7154f + value.Blue * 0.0721f;
return new Color3(
grey + saturation * (value.Red - grey),
grey + saturation * (value.Green - grey),
grey + saturation * (value.Blue - grey));
}
/// <summary>
/// Computes the premultiplied value of the provided color.
/// </summary>
/// <param name="value">The non-premultiplied value.</param>
/// <param name="alpha">The color alpha.</param>
/// <param name="result">The premultiplied result.</param>
public static void Premultiply(ref Color3 value, float alpha, out Color3 result)
{
result.Red = value.Red * alpha;
result.Green = value.Green * alpha;
result.Blue = value.Blue * alpha;
}
/// <summary>
/// Computes the premultiplied value of the provided color.
/// </summary>
/// <param name="value">The non-premultiplied value.</param>
/// <param name="alpha">The color alpha.</param>
/// <returns>The premultiplied color.</returns>
public static Color3 Premultiply(Color3 value, float alpha)
{
Color3 result;
Premultiply(ref value, alpha, out result);
return result;
}
/// <summary>
/// Adds two colors.
/// </summary>
/// <param name="left">The first color to add.</param>
/// <param name="right">The second color to add.</param>
/// <returns>The sum of the two colors.</returns>
public static Color3 operator +(Color3 left, Color3 right)
{
return new Color3(left.Red + right.Red, left.Green + right.Green, left.Blue + right.Blue);
}
/// <summary>
/// Assert a color (return it unchanged).
/// </summary>
/// <param name="value">The color to assert (unchanged).</param>
/// <returns>The asserted (unchanged) color.</returns>
public static Color3 operator +(Color3 value)
{
return value;
}
/// <summary>
/// Subtracts two colors.
/// </summary>
/// <param name="left">The first color to subtract.</param>
/// <param name="right">The second color to subtract.</param>
/// <returns>The difference of the two colors.</returns>
public static Color3 operator -(Color3 left, Color3 right)
{
return new Color3(left.Red - right.Red, left.Green - right.Green, left.Blue - right.Blue);
}
/// <summary>
/// Negates a color.
/// </summary>
/// <param name="value">The color to negate.</param>
/// <returns>A negated color.</returns>
public static Color3 operator -(Color3 value)
{
return new Color3(-value.Red, -value.Green, -value.Blue);
}
/// <summary>
/// Scales a color.
/// </summary>
/// <param name="scale">The factor by which to scale the color.</param>
/// <param name="value">The color to scale.</param>
/// <returns>The scaled color.</returns>
public static Color3 operator *(float scale, Color3 value)
{
return new Color3(value.Red * scale, value.Green * scale, value.Blue * scale);
}
/// <summary>
/// Scales a color.
/// </summary>
/// <param name="value">The factor by which to scale the color.</param>
/// <param name="scale">The color to scale.</param>
/// <returns>The scaled color.</returns>
public static Color3 operator *(Color3 value, float scale)
{
return new Color3(value.Red * scale, value.Green * scale, value.Blue * scale);
}
/// <summary>
/// Modulates two colors.
/// </summary>
/// <param name="left">The first color to modulate.</param>
/// <param name="right">The second color to modulate.</param>
/// <returns>The modulated color.</returns>
public static Color3 operator *(Color3 left, Color3 right)
{
return new Color3(left.Red * right.Red, left.Green * right.Green, left.Blue * right.Blue);
}
/// <summary>
/// Tests for equality between two objects.
/// </summary>
/// <param name="left">The first value to compare.</param>
/// <param name="right">The second value to compare.</param>
/// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public static bool operator ==(Color3 left, Color3 right)
{
return left.Equals(ref right);
}
/// <summary>
/// Tests for inequality between two objects.
/// </summary>
/// <param name="left">The first value to compare.</param>
/// <param name="right">The second value to compare.</param>
/// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public static bool operator !=(Color3 left, Color3 right)
{
return !left.Equals(ref right);
}
/// <summary>
/// Performs an explicit conversion from <see cref="Color3"/> to <see cref="Color4"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator Color4(Color3 value)
{
return new Color4(value.Red, value.Green, value.Blue, 1.0f);
}
/// <summary>
/// Performs an implicit conversion from <see cref="Color3"/> to <see cref="Vector3"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Vector3(Color3 value)
{
return new Vector3(value.Red, value.Green, value.Blue);
}
/// <summary>
/// Performs an implicit conversion from <see cref="Vector3"/> to <see cref="Color3"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Color3(Vector3 value)
{
return new Color3(value.X, value.Y, value.Z);
}
/// <summary>
/// Performs an explicit conversion from <see cref="System.Int32"/> to <see cref="Color3"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator Color3(int value)
{
return new Color3(value);
}
/// <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 ToString(CultureInfo.CurrentCulture);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="format">The format to apply to each channel element (float)</param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="formatProvider">The format provider.</param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public string ToString(IFormatProvider formatProvider)
{
return string.Format(formatProvider, toStringFormat, Red, Green, Blue);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="format">The format to apply to each channel element (float).</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public string ToString(string format, IFormatProvider formatProvider)
{
if (format == null)
return ToString(formatProvider);
return string.Format(formatProvider,
toStringFormat,
Red.ToString(format, formatProvider),
Green.ToString(format, formatProvider),
Blue.ToString(format, formatProvider));
}
/// <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
{
var hashCode = Red.GetHashCode();
hashCode = (hashCode * 397) ^ Green.GetHashCode();
hashCode = (hashCode * 397) ^ Blue.GetHashCode();
return hashCode;
}
}
/// <summary>
/// Determines whether the specified <see cref="Color3"/> is equal to this instance.
/// </summary>
/// <param name="other">The <see cref="Color3"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="Color3"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public bool Equals(ref Color3 other)
{
return Red == other.Red && Green == other.Green && Blue == other.Blue;
}
/// <summary>
/// Determines whether the specified <see cref="Color3"/> is equal to this instance.
/// </summary>
/// <param name="other">The <see cref="Color3"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="Color3"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public bool Equals(Color3 other)
{
return Equals(ref other);
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="value">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 value)
{
if (!(value is Color3))
return false;
var strongValue = (Color3)value;
return Equals(ref strongValue);
}
// /// <summary>
// /// Performs an implicit conversion from <see cref="Color3"/> to <see cref="RawColor3"/>.
// /// </summary>
// /// <param name="value">The value.</param>
// /// <returns>The result of the conversion.</returns>
// public unsafe static implicit operator RawColor3(Color3 value)
// {
// return *(RawColor3*)&value;
// }
//
// /// <summary>
// /// Performs an implicit conversion from <see cref="RawColor3"/> to <see cref="Color3"/>.
// /// </summary>
// /// <param name="value">The value.</param>
// /// <returns>The result of the conversion.</returns>
// public unsafe static implicit operator Color3(RawColor3 value)
// {
// return *(Color3*)&value;
// }
}
}