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