Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Color4.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under MIT License. See LICENSE.md for details.
3 //
4 // -----------------------------------------------------------------------------
5 // Original code from SlimMath project. http://code.google.com/p/slimmath/
6 // Greetings to SlimDX Group. Original code published with the following license:
7 // -----------------------------------------------------------------------------
8 /*
9 * Copyright (c) 2007-2011 SlimDX Group
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29 using System;
30 using System.Globalization;
31 using System.Runtime.InteropServices;
32 using SiliconStudio.Core.Serialization;
33 
34 namespace SiliconStudio.Core.Mathematics
35 {
36  /// <summary>
37  /// Represents a color in the form of rgba.
38  /// </summary>
39  [DataContract("Color4")]
40  [DataStyle(DataStyle.Compact)]
41  [StructLayout(LayoutKind.Sequential, Pack = 4)]
42  public struct Color4 : IEquatable<Color4>, IFormattable
43  {
44  private const string toStringFormat = "A:{0} R:{1} G:{2} B:{3}";
45 
46  /// <summary>
47  /// The Black color (0, 0, 0, 1).
48  /// </summary>
49  public static readonly Color4 Black = new Color4(0.0f, 0.0f, 0.0f, 1.0f);
50 
51  /// <summary>
52  /// The White color (1, 1, 1, 1).
53  /// </summary>
54  public static readonly Color4 White = new Color4(1.0f, 1.0f, 1.0f, 1.0f);
55 
56  /// <summary>
57  /// The red component of the color.
58  /// </summary>
59  [DataMember(0)]
60  public float R;
61 
62  /// <summary>
63  /// The green component of the color.
64  /// </summary>
65  [DataMember(1)]
66  public float G;
67 
68  /// <summary>
69  /// The blue component of the color.
70  /// </summary>
71  [DataMember(2)]
72  public float B;
73 
74  /// <summary>
75  /// The alpha component of the color.
76  /// </summary>
77  [DataMember(3)]
78  public float A;
79 
80  /// <summary>
81  /// Initializes a new instance of the <see cref="SharpDX.Color4"/> struct.
82  /// </summary>
83  /// <param name="value">The value that will be assigned to all components.</param>
84  public Color4(float value)
85  {
86  A = R = G = B = value;
87  }
88 
89  /// <summary>
90  /// Initializes a new instance of the <see cref="SharpDX.Color4"/> struct.
91  /// </summary>
92  /// <param name="red">The red component of the color.</param>
93  /// <param name="green">The green component of the color.</param>
94  /// <param name="blue">The blue component of the color.</param>
95  /// <param name="alpha">The alpha component of the color.</param>
96  public Color4(float red, float green, float blue, float alpha)
97  {
98  R = red;
99  G = green;
100  B = blue;
101  A = alpha;
102  }
103 
104  /// <summary>
105  /// Initializes a new instance of the <see cref="SharpDX.Color4"/> struct.
106  /// </summary>
107  /// <param name="value">The red, green, blue, and alpha components of the color.</param>
108  public Color4(Vector4 value)
109  {
110  R = value.X;
111  G = value.Y;
112  B = value.Z;
113  A = value.W;
114  }
115 
116  /// <summary>
117  /// Initializes a new instance of the <see cref="SharpDX.Color4"/> struct.
118  /// </summary>
119  /// <param name="value">The red, green, and blue components of the color.</param>
120  /// <param name="alpha">The alpha component of the color.</param>
121  public Color4(Vector3 value, float alpha)
122  {
123  R = value.X;
124  G = value.Y;
125  B = value.Z;
126  A = alpha;
127  }
128 
129  /// <summary>
130  /// Initializes a new instance of the <see cref="SharpDX.Color4"/> struct.
131  /// </summary>
132  /// <param name="rgba">A packed integer containing all four color components in RGBA order.</param>
133  public Color4(uint rgba)
134  {
135  A = ((rgba >> 24) & 255) / 255.0f;
136  B = ((rgba >> 16) & 255) / 255.0f;
137  G = ((rgba >> 8) & 255) / 255.0f;
138  R = (rgba & 255) / 255.0f;
139  }
140 
141  /// <summary>
142  /// Initializes a new instance of the <see cref="SharpDX.Color4"/> struct.
143  /// </summary>
144  /// <param name="rgba">A packed integer containing all four color components in RGBA order.</param>
145  public Color4(int rgba)
146  {
147  A = ((rgba >> 24) & 255) / 255.0f;
148  B = ((rgba >> 16) & 255) / 255.0f;
149  G = ((rgba >> 8) & 255) / 255.0f;
150  R = (rgba & 255) / 255.0f;
151  }
152 
153  /// <summary>
154  /// Initializes a new instance of the <see cref="SharpDX.Color4"/> struct.
155  /// </summary>
156  /// <param name="values">The values to assign to the red, green, blue, and alpha components of the color. This must be an array with four elements.</param>
157  /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
158  /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
159  public Color4(float[] values)
160  {
161  if (values == null)
162  throw new ArgumentNullException("values");
163  if (values.Length != 4)
164  throw new ArgumentOutOfRangeException("values", "There must be four and only four input values for Color4.");
165 
166  R = values[0];
167  G = values[1];
168  B = values[2];
169  A = values[3];
170  }
171 
172  /// <summary>
173  /// Initializes a new instance of the <see cref="SharpDX.Color4"/> struct.
174  /// </summary>
175  /// <param name="color"><see cref="SharpDX.Color3"/> used to initialize the color.</param>
176  public Color4(Color3 color)
177  {
178  R = color.R;
179  G = color.G;
180  B = color.B;
181  A = 1.0f;
182  }
183 
184  /// <summary>
185  /// Initializes a new instance of the <see cref="SharpDX.Color4"/> struct.
186  /// </summary>
187  /// <param name="color"><see cref="SharpDX.Color3"/> used to initialize the color.</param>
188  /// <param name="alpha">The alpha component of the color.</param>
189  public Color4(Color3 color, float alpha)
190  {
191  R = color.R;
192  G = color.G;
193  B = color.B;
194  A = alpha;
195  }
196 
197  /// <summary>
198  /// Gets or sets the component at the specified index.
199  /// </summary>
200  /// <value>The value of the red, green, blue, and alpha components, depending on the index.</value>
201  /// <param name="index">The index of the component to access. Use 0 for the alpha component, 1 for the red component, 2 for the green component, and 3 for the blue component.</param>
202  /// <returns>The value of the component at the specified index.</returns>
203  /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 3].</exception>
204  public float this[int index]
205  {
206  get
207  {
208  switch (index)
209  {
210  case 0: return R;
211  case 1: return G;
212  case 2: return B;
213  case 3: return A;
214  }
215 
216  throw new ArgumentOutOfRangeException("index", "Indices for Color4 run from 0 to 3, inclusive.");
217  }
218 
219  set
220  {
221  switch (index)
222  {
223  case 0: R = value; break;
224  case 1: G = value; break;
225  case 2: B = value; break;
226  case 3: A = value; break;
227  default: throw new ArgumentOutOfRangeException("index", "Indices for Color4 run from 0 to 3, inclusive.");
228  }
229  }
230  }
231 
232  /// <summary>
233  /// Converts the color into a packed integer.
234  /// </summary>
235  /// <returns>A packed integer containing all four color components.</returns>
236  public int ToBgra()
237  {
238  uint a = (uint)(A * 255.0f) & 255;
239  uint r = (uint)(R * 255.0f) & 255;
240  uint g = (uint)(G * 255.0f) & 255;
241  uint b = (uint)(B * 255.0f) & 255;
242 
243  uint value = b;
244  value |= g << 8;
245  value |= r << 16;
246  value |= a << 24;
247 
248  return (int)value;
249  }
250 
251  /// <summary>
252  /// Converts the color into a packed integer.
253  /// </summary>
254  /// <returns>A packed integer containing all four color components.</returns>
255  public void ToBgra(out byte r, out byte g, out byte b, out byte a)
256  {
257  a = (byte)(A * 255.0f);
258  r = (byte)(R * 255.0f);
259  g = (byte)(G * 255.0f);
260  b = (byte)(B * 255.0f);
261  }
262 
263  /// <summary>
264  /// Converts the color into a packed integer.
265  /// </summary>
266  /// <returns>A packed integer containing all four color components.</returns>
267  public int ToRgba()
268  {
269  uint a = (uint)(A * 255.0f) & 255;
270  uint r = (uint)(R * 255.0f) & 255;
271  uint g = (uint)(G * 255.0f) & 255;
272  uint b = (uint)(B * 255.0f) & 255;
273 
274  uint value = r;
275  value |= g << 8;
276  value |= b << 16;
277  value |= a << 24;
278 
279  return (int)value;
280  }
281 
282  /// <summary>
283  /// Converts the color into a three component vector.
284  /// </summary>
285  /// <returns>A three component vector containing the red, green, and blue components of the color.</returns>
287  {
288  return new Vector3(R, G, B);
289  }
290 
291  /// <summary>
292  /// Converts the color into a four component vector.
293  /// </summary>
294  /// <returns>A four component vector containing all four color components.</returns>
296  {
297  return new Vector4(R, G, B, A);
298  }
299 
300  /// <summary>
301  /// Creates an array containing the elements of the color.
302  /// </summary>
303  /// <returns>A four-element array containing the components of the color.</returns>
304  public float[] ToArray()
305  {
306  return new float[] { R, G, B, A };
307  }
308 
309  /// <summary>
310  /// Adds two colors.
311  /// </summary>
312  /// <param name="left">The first color to add.</param>
313  /// <param name="right">The second color to add.</param>
314  /// <param name="result">When the method completes, completes the sum of the two colors.</param>
315  public static void Add(ref Color4 left, ref Color4 right, out Color4 result)
316  {
317  result.A = left.A + right.A;
318  result.R = left.R + right.R;
319  result.G = left.G + right.G;
320  result.B = left.B + right.B;
321  }
322 
323  /// <summary>
324  /// Adds two colors.
325  /// </summary>
326  /// <param name="left">The first color to add.</param>
327  /// <param name="right">The second color to add.</param>
328  /// <returns>The sum of the two colors.</returns>
329  public static Color4 Add(Color4 left, Color4 right)
330  {
331  return new Color4(left.R + right.R, left.G + right.G, left.B + right.B, left.A + right.A);
332  }
333 
334  /// <summary>
335  /// Subtracts two colors.
336  /// </summary>
337  /// <param name="left">The first color to subtract.</param>
338  /// <param name="right">The second color to subtract.</param>
339  /// <param name="result">WHen the method completes, contains the difference of the two colors.</param>
340  public static void Subtract(ref Color4 left, ref Color4 right, out Color4 result)
341  {
342  result.A = left.A - right.A;
343  result.R = left.R - right.R;
344  result.G = left.G - right.G;
345  result.B = left.B - right.B;
346  }
347 
348  /// <summary>
349  /// Subtracts two colors.
350  /// </summary>
351  /// <param name="left">The first color to subtract.</param>
352  /// <param name="right">The second color to subtract</param>
353  /// <returns>The difference of the two colors.</returns>
354  public static Color4 Subtract(Color4 left, Color4 right)
355  {
356  return new Color4(left.R - right.R, left.G - right.G, left.B - right.B, left.A - right.A);
357  }
358 
359  /// <summary>
360  /// Modulates two colors.
361  /// </summary>
362  /// <param name="left">The first color to modulate.</param>
363  /// <param name="right">The second color to modulate.</param>
364  /// <param name="result">When the method completes, contains the modulated color.</param>
365  public static void Modulate(ref Color4 left, ref Color4 right, out Color4 result)
366  {
367  result.A = left.A * right.A;
368  result.R = left.R * right.R;
369  result.G = left.G * right.G;
370  result.B = left.B * right.B;
371  }
372 
373  /// <summary>
374  /// Modulates two colors.
375  /// </summary>
376  /// <param name="left">The first color to modulate.</param>
377  /// <param name="right">The second color to modulate.</param>
378  /// <returns>The modulated color.</returns>
379  public static Color4 Modulate(Color4 left, Color4 right)
380  {
381  return new Color4(left.R * right.R, left.G * right.G, left.B * right.B, left.A * right.A);
382  }
383 
384  /// <summary>
385  /// Scales a color.
386  /// </summary>
387  /// <param name="value">The color to scale.</param>
388  /// <param name="scale">The amount by which to scale.</param>
389  /// <param name="result">When the method completes, contains the scaled color.</param>
390  public static void Scale(ref Color4 value, float scale, out Color4 result)
391  {
392  result.A = value.A * scale;
393  result.R = value.R * scale;
394  result.G = value.G * scale;
395  result.B = value.B * scale;
396  }
397 
398  /// <summary>
399  /// Scales a color.
400  /// </summary>
401  /// <param name="value">The color to scale.</param>
402  /// <param name="scale">The amount by which to scale.</param>
403  /// <returns>The scaled color.</returns>
404  public static Color4 Scale(Color4 value, float scale)
405  {
406  return new Color4(value.R * scale, value.G * scale, value.B * scale, value.A * scale);
407  }
408 
409  /// <summary>
410  /// Negates a color.
411  /// </summary>
412  /// <param name="value">The color to negate.</param>
413  /// <param name="result">When the method completes, contains the negated color.</param>
414  public static void Negate(ref Color4 value, out Color4 result)
415  {
416  result.A = 1.0f - value.A;
417  result.R = 1.0f - value.R;
418  result.G = 1.0f - value.G;
419  result.B = 1.0f - value.B;
420  }
421 
422  /// <summary>
423  /// Negates a color.
424  /// </summary>
425  /// <param name="value">The color to negate.</param>
426  /// <returns>The negated color.</returns>
427  public static Color4 Negate(Color4 value)
428  {
429  return new Color4(1.0f - value.R, 1.0f - value.G, 1.0f - value.B, 1.0f - value.A);
430  }
431 
432  /// <summary>
433  /// Restricts a value to be within a specified range.
434  /// </summary>
435  /// <param name="value">The value to clamp.</param>
436  /// <param name="min">The minimum value.</param>
437  /// <param name="max">The maximum value.</param>
438  /// <param name="result">When the method completes, contains the clamped value.</param>
439  public static void Clamp(ref Color4 value, ref Color4 min, ref Color4 max, out Color4 result)
440  {
441  float alpha = value.A;
442  alpha = (alpha > max.A) ? max.A : alpha;
443  alpha = (alpha < min.A) ? min.A : alpha;
444 
445  float red = value.R;
446  red = (red > max.R) ? max.R : red;
447  red = (red < min.R) ? min.R : red;
448 
449  float green = value.G;
450  green = (green > max.G) ? max.G : green;
451  green = (green < min.G) ? min.G : green;
452 
453  float blue = value.B;
454  blue = (blue > max.B) ? max.B : blue;
455  blue = (blue < min.B) ? min.B : blue;
456 
457  result = new Color4(red, green, blue, alpha);
458  }
459 
460  /// <summary>
461  /// Restricts a value to be within a specified range.
462  /// </summary>
463  /// <param name="value">The value to clamp.</param>
464  /// <param name="min">The minimum value.</param>
465  /// <param name="max">The maximum value.</param>
466  /// <returns>The clamped value.</returns>
467  public static Color4 Clamp(Color4 value, Color4 min, Color4 max)
468  {
469  Color4 result;
470  Clamp(ref value, ref min, ref max, out result);
471  return result;
472  }
473 
474  /// <summary>
475  /// Performs a linear interpolation between two colors.
476  /// </summary>
477  /// <param name="start">Start color.</param>
478  /// <param name="end">End color.</param>
479  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
480  /// <param name="result">When the method completes, contains the linear interpolation of the two colors.</param>
481  /// <remarks>
482  /// 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.
483  /// </remarks>
484  public static void Lerp(ref Color4 start, ref Color4 end, float amount, out Color4 result)
485  {
486  result.R = MathUtil.Lerp(start.R, end.R, amount);
487  result.G = MathUtil.Lerp(start.G, end.G, amount);
488  result.B = MathUtil.Lerp(start.B, end.B, amount);
489  result.A = MathUtil.Lerp(start.A, end.A, amount);
490  }
491 
492  /// <summary>
493  /// Performs a linear interpolation between two colors.
494  /// </summary>
495  /// <param name="start">Start color.</param>
496  /// <param name="end">End color.</param>
497  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
498  /// <returns>The linear interpolation of the two colors.</returns>
499  /// <remarks>
500  /// 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.
501  /// </remarks>
502  public static Color4 Lerp(Color4 start, Color4 end, float amount)
503  {
504  Color4 result;
505  Lerp(ref start, ref end, amount, out result);
506  return result;
507  }
508 
509  /// <summary>
510  /// Performs a cubic interpolation between two colors.
511  /// </summary>
512  /// <param name="start">Start color.</param>
513  /// <param name="end">End color.</param>
514  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
515  /// <param name="result">When the method completes, contains the cubic interpolation of the two colors.</param>
516  public static void SmoothStep(ref Color4 start, ref Color4 end, float amount, out Color4 result)
517  {
518  amount = MathUtil.SmoothStep(amount);
519  Lerp(ref start, ref end, amount, out result);
520  }
521 
522  /// <summary>
523  /// Performs a cubic interpolation between two colors.
524  /// </summary>
525  /// <param name="start">Start color.</param>
526  /// <param name="end">End color.</param>
527  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
528  /// <returns>The cubic interpolation of the two colors.</returns>
529  public static Color4 SmoothStep(Color4 start, Color4 end, float amount)
530  {
531  Color4 result;
532  SmoothStep(ref start, ref end, amount, out result);
533  return result;
534  }
535 
536  /// <summary>
537  /// Returns a color containing the smallest components of the specified colors.
538  /// </summary>
539  /// <param name="left">The first source color.</param>
540  /// <param name="right">The second source color.</param>
541  /// <param name="result">When the method completes, contains an new color composed of the largest components of the source colors.</param>
542  public static void Max(ref Color4 left, ref Color4 right, out Color4 result)
543  {
544  result.A = (left.A > right.A) ? left.A : right.A;
545  result.R = (left.R > right.R) ? left.R : right.R;
546  result.G = (left.G > right.G) ? left.G : right.G;
547  result.B = (left.B > right.B) ? left.B : right.B;
548  }
549 
550  /// <summary>
551  /// Returns a color containing the largest components of the specified colors.
552  /// </summary>
553  /// <param name="left">The first source color.</param>
554  /// <param name="right">The second source color.</param>
555  /// <returns>A color containing the largest components of the source colors.</returns>
556  public static Color4 Max(Color4 left, Color4 right)
557  {
558  Color4 result;
559  Max(ref left, ref right, out result);
560  return result;
561  }
562 
563  /// <summary>
564  /// Returns a color containing the smallest components of the specified colors.
565  /// </summary>
566  /// <param name="left">The first source color.</param>
567  /// <param name="right">The second source color.</param>
568  /// <param name="result">When the method completes, contains an new color composed of the smallest components of the source colors.</param>
569  public static void Min(ref Color4 left, ref Color4 right, out Color4 result)
570  {
571  result.A = (left.A < right.A) ? left.A : right.A;
572  result.R = (left.R < right.R) ? left.R : right.R;
573  result.G = (left.G < right.G) ? left.G : right.G;
574  result.B = (left.B < right.B) ? left.B : right.B;
575  }
576 
577  /// <summary>
578  /// Returns a color containing the smallest components of the specified colors.
579  /// </summary>
580  /// <param name="left">The first source color.</param>
581  /// <param name="right">The second source color.</param>
582  /// <returns>A color containing the smallest components of the source colors.</returns>
583  public static Color4 Min(Color4 left, Color4 right)
584  {
585  Color4 result;
586  Min(ref left, ref right, out result);
587  return result;
588  }
589 
590  /// <summary>
591  /// Adjusts the contrast of a color.
592  /// </summary>
593  /// <param name="value">The color whose contrast is to be adjusted.</param>
594  /// <param name="contrast">The amount by which to adjust the contrast.</param>
595  /// <param name="result">When the method completes, contains the adjusted color.</param>
596  public static void AdjustContrast(ref Color4 value, float contrast, out Color4 result)
597  {
598  result.A = value.A;
599  result.R = 0.5f + contrast * (value.R - 0.5f);
600  result.G = 0.5f + contrast * (value.G - 0.5f);
601  result.B = 0.5f + contrast * (value.B - 0.5f);
602  }
603 
604  /// <summary>
605  /// Adjusts the contrast of a color.
606  /// </summary>
607  /// <param name="value">The color whose contrast is to be adjusted.</param>
608  /// <param name="contrast">The amount by which to adjust the contrast.</param>
609  /// <returns>The adjusted color.</returns>
610  public static Color4 AdjustContrast(Color4 value, float contrast)
611  {
612  return new Color4(
613  0.5f + contrast * (value.R - 0.5f),
614  0.5f + contrast * (value.G - 0.5f),
615  0.5f + contrast * (value.B - 0.5f),
616  value.A);
617  }
618 
619  /// <summary>
620  /// Adjusts the saturation of a color.
621  /// </summary>
622  /// <param name="value">The color whose saturation is to be adjusted.</param>
623  /// <param name="saturation">The amount by which to adjust the saturation.</param>
624  /// <param name="result">When the method completes, contains the adjusted color.</param>
625  public static void AdjustSaturation(ref Color4 value, float saturation, out Color4 result)
626  {
627  float grey = value.R * 0.2125f + value.G * 0.7154f + value.B * 0.0721f;
628 
629  result.A = value.A;
630  result.R = grey + saturation * (value.R - grey);
631  result.G = grey + saturation * (value.G - grey);
632  result.B = grey + saturation * (value.B - grey);
633  }
634 
635  /// <summary>
636  /// Adjusts the saturation of a color.
637  /// </summary>
638  /// <param name="value">The color whose saturation is to be adjusted.</param>
639  /// <param name="saturation">The amount by which to adjust the saturation.</param>
640  /// <returns>The adjusted color.</returns>
641  public static Color4 AdjustSaturation(Color4 value, float saturation)
642  {
643  float grey = value.R * 0.2125f + value.G * 0.7154f + value.B * 0.0721f;
644 
645  return new Color4(
646  grey + saturation * (value.R - grey),
647  grey + saturation * (value.G - grey),
648  grey + saturation * (value.B - grey),
649  value.A);
650  }
651 
652  /// <summary>
653  /// Adds two colors.
654  /// </summary>
655  /// <param name="left">The first color to add.</param>
656  /// <param name="right">The second color to add.</param>
657  /// <returns>The sum of the two colors.</returns>
658  public static Color4 operator +(Color4 left, Color4 right)
659  {
660  return new Color4(left.R + right.R, left.G + right.G, left.B + right.B, left.A + right.A);
661  }
662 
663  /// <summary>
664  /// Assert a color (return it unchanged).
665  /// </summary>
666  /// <param name="value">The color to assert (unchanged).</param>
667  /// <returns>The asserted (unchanged) color.</returns>
668  public static Color4 operator +(Color4 value)
669  {
670  return value;
671  }
672 
673  /// <summary>
674  /// Subtracts two colors.
675  /// </summary>
676  /// <param name="left">The first color to subtract.</param>
677  /// <param name="right">The second color to subtract.</param>
678  /// <returns>The difference of the two colors.</returns>
679  public static Color4 operator -(Color4 left, Color4 right)
680  {
681  return new Color4(left.R - right.R, left.G - right.G, left.B - right.B, left.A - right.A);
682  }
683 
684  /// <summary>
685  /// Negates a color.
686  /// </summary>
687  /// <param name="value">The color to negate.</param>
688  /// <returns>A negated color.</returns>
689  public static Color4 operator -(Color4 value)
690  {
691  return new Color4(-value.R, -value.G, -value.B, -value.A);
692  }
693 
694  /// <summary>
695  /// Scales a color.
696  /// </summary>
697  /// <param name="scale">The factor by which to scale the color.</param>
698  /// <param name="value">The color to scale.</param>
699  /// <returns>The scaled color.</returns>
700  public static Color4 operator *(float scale, Color4 value)
701  {
702  return new Color4(value.R * scale, value.G * scale, value.B * scale, value.A * scale);
703  }
704 
705  /// <summary>
706  /// Scales a color.
707  /// </summary>
708  /// <param name="value">The factor by which to scale the color.</param>
709  /// <param name="scale">The color to scale.</param>
710  /// <returns>The scaled color.</returns>
711  public static Color4 operator *(Color4 value, float scale)
712  {
713  return new Color4(value.R * scale, value.G * scale, value.B * scale, value.A * scale);
714  }
715 
716  /// <summary>
717  /// Modulates two colors.
718  /// </summary>
719  /// <param name="left">The first color to modulate.</param>
720  /// <param name="right">The second color to modulate.</param>
721  /// <returns>The modulated color.</returns>
722  public static Color4 operator *(Color4 left, Color4 right)
723  {
724  return new Color4(left.R * right.R, left.G * right.G, left.B * right.B, left.A * right.A);
725  }
726 
727  /// <summary>
728  /// Tests for equality between two objects.
729  /// </summary>
730  /// <param name="left">The first value to compare.</param>
731  /// <param name="right">The second value to compare.</param>
732  /// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
733  public static bool operator ==(Color4 left, Color4 right)
734  {
735  return left.Equals(right);
736  }
737 
738  /// <summary>
739  /// Tests for inequality between two objects.
740  /// </summary>
741  /// <param name="left">The first value to compare.</param>
742  /// <param name="right">The second value to compare.</param>
743  /// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
744  public static bool operator !=(Color4 left, Color4 right)
745  {
746  return !left.Equals(right);
747  }
748 
749  /// <summary>
750  /// Performs an explicit conversion from <see cref="SharpDX.Color4"/> to <see cref="SharpDX.Color3"/>.
751  /// </summary>
752  /// <param name="value">The value.</param>
753  /// <returns>The result of the conversion.</returns>
754  public static explicit operator Color3(Color4 value)
755  {
756  return new Color3(value.R, value.G, value.B);
757  }
758 
759  /// <summary>
760  /// Performs an explicit conversion from <see cref="SharpDX.Color4"/> to <see cref="SharpDX.Vector3"/>.
761  /// </summary>
762  /// <param name="value">The value.</param>
763  /// <returns>The result of the conversion.</returns>
764  public static explicit operator Vector3(Color4 value)
765  {
766  return new Vector3(value.R, value.G, value.B);
767  }
768 
769  /// <summary>
770  /// Performs an implicit conversion from <see cref="SharpDX.Color4"/> to <see cref="SharpDX.Vector4"/>.
771  /// </summary>
772  /// <param name="value">The value.</param>
773  /// <returns>The result of the conversion.</returns>
774  public static implicit operator Vector4(Color4 value)
775  {
776  return new Vector4(value.R, value.G, value.B, value.A);
777  }
778 
779  /// <summary>
780  /// Performs an explicit conversion from <see cref="SharpDX.Vector3"/> to <see cref="SharpDX.Color4"/>.
781  /// </summary>
782  /// <param name="value">The value.</param>
783  /// <returns>The result of the conversion.</returns>
784  public static explicit operator Color4(Vector3 value)
785  {
786  return new Color4(value.X, value.Y, value.Z, 1.0f);
787  }
788 
789  /// <summary>
790  /// Performs an explicit conversion from <see cref="SharpDX.Vector4"/> to <see cref="SharpDX.Color4"/>.
791  /// </summary>
792  /// <param name="value">The value.</param>
793  /// <returns>The result of the conversion.</returns>
794  public static explicit operator Color4(Vector4 value)
795  {
796  return new Color4(value.X, value.Y, value.Z, value.W);
797  }
798 
799  /// <summary>
800  /// Performs an explicit conversion from <see cref="SharpDX.Vector3"/> to <see cref="SharpDX.Color4"/>.
801  /// </summary>
802  /// <param name="value">The value.</param>
803  /// <returns>The result of the conversion.</returns>
804  public static explicit operator Color4(ColorBGRA value)
805  {
806  return new Color4(value.R, value.G, value.B, value.A);
807  }
808 
809  /// <summary>
810  /// Performs an explicit conversion from <see cref="SharpDX.Vector4"/> to <see cref="SharpDX.Color4"/>.
811  /// </summary>
812  /// <param name="value">The value.</param>
813  /// <returns>The result of the conversion.</returns>
814  public static explicit operator ColorBGRA(Color4 value)
815  {
816  return new ColorBGRA(value.R, value.G, value.B, value.A);
817  }
818 
819  /// <summary>
820  /// Performs an explicit conversion from <see cref="SharpDX.Color4"/> to <see cref="System.Int32"/>.
821  /// </summary>
822  /// <param name="value">The value.</param>
823  /// <returns>
824  /// The result of the conversion.
825  /// </returns>
826  public static explicit operator int(Color4 value)
827  {
828  return value.ToRgba();
829  }
830 
831  /// <summary>
832  /// Performs an explicit conversion from <see cref="System.Int32"/> to <see cref="SharpDX.Color4"/>.
833  /// </summary>
834  /// <param name="value">The value.</param>
835  /// <returns>
836  /// The result of the conversion.
837  /// </returns>
838  public static explicit operator Color4(int value)
839  {
840  return new Color4(value);
841  }
842 
843  /// <summary>
844  /// Returns a <see cref="System.String"/> that represents this instance.
845  /// </summary>
846  /// <returns>
847  /// A <see cref="System.String"/> that represents this instance.
848  /// </returns>
849  public override string ToString()
850  {
851  return ToString(CultureInfo.CurrentCulture);
852  }
853 
854  /// <summary>
855  /// Returns a <see cref="System.String"/> that represents this instance.
856  /// </summary>
857  /// <param name="format">The format to apply to each channel (float).</param>
858  /// <returns>
859  /// A <see cref="System.String"/> that represents this instance.
860  /// </returns>
861  public string ToString(string format)
862  {
863  return ToString(format, CultureInfo.CurrentCulture);
864  }
865 
866  /// <summary>
867  /// Returns a <see cref="System.String"/> that represents this instance.
868  /// </summary>
869  /// <param name="formatProvider">The format provider.</param>
870  /// <returns>
871  /// A <see cref="System.String"/> that represents this instance.
872  /// </returns>
873  public string ToString(IFormatProvider formatProvider)
874  {
875  return string.Format(formatProvider, toStringFormat, A, R, G, B);
876  }
877 
878  /// <summary>
879  /// Returns a <see cref="System.String"/> that represents this instance.
880  /// </summary>
881  /// <param name="format">The format to apply to each channel (float).</param>
882  /// <param name="formatProvider">The format provider.</param>
883  /// <returns>
884  /// A <see cref="System.String"/> that represents this instance.
885  /// </returns>
886  public string ToString(string format, IFormatProvider formatProvider)
887  {
888  if (format == null)
889  return ToString(formatProvider);
890 
891  return string.Format(formatProvider,
892  toStringFormat,
893  A.ToString(format, formatProvider),
894  R.ToString(format, formatProvider),
895  G.ToString(format, formatProvider),
896  B.ToString(format, formatProvider));
897  }
898 
899  /// <summary>
900  /// Returns a hash code for this instance.
901  /// </summary>
902  /// <returns>
903  /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
904  /// </returns>
905  public override int GetHashCode()
906  {
907  return A.GetHashCode() + R.GetHashCode() + G.GetHashCode() + B.GetHashCode();
908  }
909 
910  /// <summary>
911  /// Determines whether the specified <see cref="SharpDX.Color4"/> is equal to this instance.
912  /// </summary>
913  /// <param name="other">The <see cref="SharpDX.Color4"/> to compare with this instance.</param>
914  /// <returns>
915  /// <c>true</c> if the specified <see cref="SharpDX.Color4"/> is equal to this instance; otherwise, <c>false</c>.
916  /// </returns>
917  public bool Equals(Color4 other)
918  {
919  return A == other.A && R == other.R && G == other.G && B == other.B;
920  }
921 
922  /// <summary>
923  /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
924  /// </summary>
925  /// <param name="value">The <see cref="System.Object"/> to compare with this instance.</param>
926  /// <returns>
927  /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
928  /// </returns>
929  public override bool Equals(object value)
930  {
931  if (value == null)
932  return false;
933 
934  if (!ReferenceEquals(value.GetType(), typeof(Color4)))
935  return false;
936 
937  return Equals((Color4)value);
938  }
939  }
940 }
static Color4 Scale(Color4 value, float scale)
Scales a color.
Definition: Color4.cs:404
bool Equals(Color4 other)
Determines whether the specified SharpDX.Color4 is equal to this instance.
Definition: Color4.cs:917
static void Negate(ref Color4 value, out Color4 result)
Negates a color.
Definition: Color4.cs:414
static Color4 Negate(Color4 value)
Negates a color.
Definition: Color4.cs:427
FbxDouble3 operator*(double factor, FbxDouble3 vector)
float Y
The Y component of the vector.
Definition: Vector3.cs:84
float A
The alpha component of the color.
Definition: Color4.cs:78
float W
The W component of the vector.
Definition: Vector4.cs:101
byte G
The green component of the color.
Definition: ColorBGRA.cs:30
function b
function a
byte B
The blue component of the color.
Definition: ColorBGRA.cs:24
static void Subtract(ref Color4 left, ref Color4 right, out Color4 result)
Subtracts two colors.
Definition: Color4.cs:340
Color4(Color3 color)
Initializes a new instance of the SharpDX.Color4 struct.
Definition: Color4.cs:176
byte A
The alpha component of the color.
Definition: ColorBGRA.cs:42
Represents a color in the form of rgb.
Definition: Color3.cs:41
float X
The X component of the vector.
Definition: Vector4.cs:83
static void SmoothStep(ref Color4 start, ref Color4 end, float amount, out Color4 result)
Performs a cubic interpolation between two colors.
Definition: Color4.cs:516
static void Lerp(ref Color4 start, ref Color4 end, float amount, out Color4 result)
Performs a linear interpolation between two colors.
Definition: Color4.cs:484
static void Min(ref Color4 left, ref Color4 right, out Color4 result)
Returns a color containing the smallest components of the specified colors.
Definition: Color4.cs:569
float X
The X component of the vector.
Definition: Vector3.cs:78
static Color4 Lerp(Color4 start, Color4 end, float amount)
Performs a linear interpolation between two colors.
Definition: Color4.cs:502
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
float B
The blue component of the color.
Definition: Color4.cs:72
Represents a 32-bit color (4 bytes) in the form of BGRA (in byte order: B, G, B, A).
Definition: ColorBGRA.cs:16
void ToBgra(out byte r, out byte g, out byte b, out byte a)
Converts the color into a packed integer.
Definition: Color4.cs:255
float[] ToArray()
Creates an array containing the elements of the color.
Definition: Color4.cs:304
float G
The green component of the color.
Definition: Color4.cs:66
static Color4 SmoothStep(Color4 start, Color4 end, float amount)
Performs a cubic interpolation between two colors.
Definition: Color4.cs:529
int ToRgba()
Converts the color into a packed integer.
Definition: Color4.cs:267
Color4(Vector3 value, float alpha)
Initializes a new instance of the SharpDX.Color4 struct.
Definition: Color4.cs:121
Represents a color in the form of rgba.
Definition: Color4.cs:42
int ToBgra()
Converts the color into a packed integer.
Definition: Color4.cs:236
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Color4.cs:886
static void AdjustSaturation(ref Color4 value, float saturation, out Color4 result)
Adjusts the saturation of a color.
Definition: Color4.cs:625
Color4(Color3 color, float alpha)
Initializes a new instance of the SharpDX.Color4 struct.
Definition: Color4.cs:189
static void AdjustContrast(ref Color4 value, float contrast, out Color4 result)
Adjusts the contrast of a color.
Definition: Color4.cs:596
Color4(uint rgba)
Initializes a new instance of the SharpDX.Color4 struct.
Definition: Color4.cs:133
static Color4 AdjustContrast(Color4 value, float contrast)
Adjusts the contrast of a color.
Definition: Color4.cs:610
Color4(Vector4 value)
Initializes a new instance of the SharpDX.Color4 struct.
Definition: Color4.cs:108
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
static Color4 Max(Color4 left, Color4 right)
Returns a color containing the largest components of the specified colors.
Definition: Color4.cs:556
float R
The red component of the color.
Definition: Color4.cs:60
static Color4 AdjustSaturation(Color4 value, float saturation)
Adjusts the saturation of a color.
Definition: Color4.cs:641
static void Scale(ref Color4 value, float scale, out Color4 result)
Scales a color.
Definition: Color4.cs:390
Color4(float[] values)
Initializes a new instance of the SharpDX.Color4 struct.
Definition: Color4.cs:159
override int GetHashCode()
Returns a hash code for this instance.
Definition: Color4.cs:905
byte R
The red component of the color.
Definition: ColorBGRA.cs:36
Vector4 ToVector4()
Converts the color into a four component vector.
Definition: Color4.cs:295
string ToString(string format)
Returns a System.String that represents this instance.
Definition: Color4.cs:861
static Color4 Min(Color4 left, Color4 right)
Returns a color containing the smallest components of the specified colors.
Definition: Color4.cs:583
static Color4 Clamp(Color4 value, Color4 min, Color4 max)
Restricts a value to be within a specified range.
Definition: Color4.cs:467
float Y
The Y component of the vector.
Definition: Vector4.cs:89
static void Clamp(ref Color4 value, ref Color4 min, ref Color4 max, out Color4 result)
Restricts a value to be within a specified range.
Definition: Color4.cs:439
static void Add(ref Color4 left, ref Color4 right, out Color4 result)
Adds two colors.
Definition: Color4.cs:315
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
Definition: Color4.cs:929
static void Modulate(ref Color4 left, ref Color4 right, out Color4 result)
Modulates two colors.
Definition: Color4.cs:365
SiliconStudio.Core.Mathematics.Vector3 Vector3
Color4(int rgba)
Initializes a new instance of the SharpDX.Color4 struct.
Definition: Color4.cs:145
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
float Z
The Z component of the vector.
Definition: Vector4.cs:95
override string ToString()
Returns a System.String that represents this instance.
Definition: Color4.cs:849
static Color4 Add(Color4 left, Color4 right)
Adds two colors.
Definition: Color4.cs:329
float Z
The Z component of the vector.
Definition: Vector3.cs:90
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Color4.cs:873
Color4(float value)
Initializes a new instance of the SharpDX.Color4 struct.
Definition: Color4.cs:84
DataStyle
Specifies the style used for textual serialization when an array/list or a dictionary/map must be ser...
Definition: DataStyle.cs:9
static void Max(ref Color4 left, ref Color4 right, out Color4 result)
Returns a color containing the smallest components of the specified colors.
Definition: Color4.cs:542
Color4(float red, float green, float blue, float alpha)
Initializes a new instance of the SharpDX.Color4 struct.
Definition: Color4.cs:96
static Color4 Subtract(Color4 left, Color4 right)
Subtracts two colors.
Definition: Color4.cs:354
Vector3 ToVector3()
Converts the color into a three component vector.
Definition: Color4.cs:286
static Color4 Modulate(Color4 left, Color4 right)
Modulates two colors.
Definition: Color4.cs:379