30 using System.Globalization;
31 using System.Runtime.InteropServices;
32 using SiliconStudio.Core.Serialization;
34 namespace SiliconStudio.Core.Mathematics
39 [DataContract(
"Color4")]
41 [StructLayout(LayoutKind.Sequential, Pack = 4)]
44 private const string toStringFormat =
"A:{0} R:{1} G:{2} B:{3}";
49 public static readonly
Color4 Black =
new Color4(0.0f, 0.0f, 0.0f, 1.0f);
54 public static readonly
Color4 White =
new Color4(1.0f, 1.0f, 1.0f, 1.0f);
86 A = R = G = B = value;
96 public Color4(
float red,
float green,
float blue,
float alpha)
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;
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;
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.");
204 public float this[
int index]
216 throw new ArgumentOutOfRangeException(
"index",
"Indices for Color4 run from 0 to 3, inclusive.");
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.");
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;
255 public void ToBgra(out byte r, out byte g, out byte
b, out byte
a)
257 a = (byte)(A * 255.0f);
258 r = (byte)(R * 255.0f);
259 g = (byte)(G * 255.0f);
260 b = (byte)(B * 255.0f);
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;
297 return new Vector4(R, G, B, A);
306 return new float[] { R, G, B, A };
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;
331 return new Color4(left.
R + right.
R, left.
G + right.
G, left.
B + right.
B, left.
A + right.
A);
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;
356 return new Color4(left.
R - right.
R, left.
G - right.
G, left.
B - right.
B, left.
A - right.
A);
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;
381 return new Color4(left.
R * right.
R, left.
G * right.
G, left.
B * right.
B, left.
A * right.
A);
392 result.A = value.A * scale;
393 result.R = value.R * scale;
394 result.G = value.G * scale;
395 result.B = value.B * scale;
406 return new Color4(value.
R * scale, value.
G * scale, value.
B * scale, value.
A * scale);
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;
429 return new Color4(1.0f - value.
R, 1.0f - value.
G, 1.0f - value.
B, 1.0f - value.
A);
441 float alpha = value.A;
442 alpha = (alpha > max.A) ? max.A : alpha;
443 alpha = (alpha < min.A) ? min.A : alpha;
446 red = (red > max.R) ? max.R : red;
447 red = (red < min.R) ? min.R : red;
449 float green = value.G;
450 green = (green > max.G) ? max.G : green;
451 green = (green < min.G) ? min.G : green;
453 float blue = value.B;
454 blue = (blue > max.B) ? max.B : blue;
455 blue = (blue < min.B) ? min.B : blue;
457 result =
new Color4(red, green, blue, alpha);
470 Clamp(ref value, ref min, ref max, out result);
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);
505 Lerp(ref start, ref end, amount, out result);
518 amount = MathUtil.SmoothStep(amount);
519 Lerp(ref start, ref end, amount, out result);
532 SmoothStep(ref start, ref end, amount, out result);
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;
559 Max(ref left, ref right, out result);
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;
586 Min(ref left, ref right, out result);
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);
613 0.5f + contrast * (value.
R - 0.5f),
614 0.5f + contrast * (value.
G - 0.5f),
615 0.5f + contrast * (value.
B - 0.5f),
627 float grey = value.R * 0.2125f + value.G * 0.7154f + value.B * 0.0721f;
630 result.R = grey + saturation * (value.R - grey);
631 result.G = grey + saturation * (value.G - grey);
632 result.B = grey + saturation * (value.B - grey);
643 float grey = value.R * 0.2125f + value.G * 0.7154f + value.B * 0.0721f;
646 grey + saturation * (value.
R - grey),
647 grey + saturation * (value.
G - grey),
648 grey + saturation * (value.
B - grey),
660 return new Color4(left.
R + right.
R, left.
G + right.
G, left.
B + right.
B, left.
A + right.
A);
681 return new Color4(left.
R - right.
R, left.
G - right.
G, left.
B - right.
B, left.
A - right.
A);
691 return new Color4(-value.
R, -value.
G, -value.
B, -value.
A);
702 return new Color4(value.
R * scale, value.
G * scale, value.
B * scale, value.
A * scale);
713 return new Color4(value.
R * scale, value.
G * scale, value.
B * scale, value.
A * scale);
724 return new Color4(left.
R * right.
R, left.
G * right.
G, left.
B * right.
B, left.
A * right.
A);
735 return left.Equals(right);
746 return !left.Equals(right);
756 return new Color3(value.
R, value.
G, value.
B);
766 return new Vector3(value.
R, value.
G, value.
B);
776 return new Vector4(value.
R, value.
G, value.
B, value.
A);
786 return new Color4(value.
X, value.
Y, value.
Z, 1.0f);
796 return new Color4(value.
X, value.
Y, value.
Z, value.
W);
806 return new Color4(value.
R, value.
G, value.
B, value.
A);
826 public static explicit operator int(
Color4 value)
828 return value.ToRgba();
838 public static explicit operator Color4(
int value)
851 return ToString(CultureInfo.CurrentCulture);
863 return ToString(format, CultureInfo.CurrentCulture);
873 public string ToString(IFormatProvider formatProvider)
875 return string.Format(formatProvider, toStringFormat, A, R, G, B);
889 return ToString(formatProvider);
891 return string.Format(formatProvider,
893 A.ToString(
format, formatProvider),
894 R.ToString(format, formatProvider),
895 G.ToString(
format, formatProvider),
896 B.ToString(format, formatProvider));
907 return A.GetHashCode() + R.GetHashCode() + G.GetHashCode() + B.GetHashCode();
919 return A == other.A && R == other.R && G == other.G && B == other.B;
929 public override bool Equals(
object value)
934 if (!ReferenceEquals(value.GetType(), typeof(
Color4)))
937 return Equals((
Color4)value);
static Color4 Scale(Color4 value, float scale)
Scales a color.
bool Equals(Color4 other)
Determines whether the specified SharpDX.Color4 is equal to this instance.
static void Negate(ref Color4 value, out Color4 result)
Negates a color.
static Color4 Negate(Color4 value)
Negates a color.
FbxDouble3 operator*(double factor, FbxDouble3 vector)
float Y
The Y component of the vector.
float A
The alpha component of the color.
float W
The W component of the vector.
byte G
The green component of the color.
byte B
The blue component of the color.
static void Subtract(ref Color4 left, ref Color4 right, out Color4 result)
Subtracts two colors.
Color4(Color3 color)
Initializes a new instance of the SharpDX.Color4 struct.
byte A
The alpha component of the color.
Represents a color in the form of rgb.
float X
The X component of the vector.
static void SmoothStep(ref Color4 start, ref Color4 end, float amount, out Color4 result)
Performs a cubic interpolation between two colors.
static void Lerp(ref Color4 start, ref Color4 end, float amount, out Color4 result)
Performs a linear interpolation between two colors.
static void Min(ref Color4 left, ref Color4 right, out Color4 result)
Returns a color containing the smallest components of the specified colors.
float X
The X component of the vector.
static Color4 Lerp(Color4 start, Color4 end, float amount)
Performs a linear interpolation between two colors.
Represents a three dimensional mathematical vector.
float B
The blue component of the color.
Represents a 32-bit color (4 bytes) in the form of BGRA (in byte order: B, G, B, A).
void ToBgra(out byte r, out byte g, out byte b, out byte a)
Converts the color into a packed integer.
float[] ToArray()
Creates an array containing the elements of the color.
float G
The green component of the color.
static Color4 SmoothStep(Color4 start, Color4 end, float amount)
Performs a cubic interpolation between two colors.
int ToRgba()
Converts the color into a packed integer.
Color4(Vector3 value, float alpha)
Initializes a new instance of the SharpDX.Color4 struct.
Represents a color in the form of rgba.
int ToBgra()
Converts the color into a packed integer.
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
static void AdjustSaturation(ref Color4 value, float saturation, out Color4 result)
Adjusts the saturation of a color.
Color4(Color3 color, float alpha)
Initializes a new instance of the SharpDX.Color4 struct.
static void AdjustContrast(ref Color4 value, float contrast, out Color4 result)
Adjusts the contrast of a color.
Color4(uint rgba)
Initializes a new instance of the SharpDX.Color4 struct.
static Color4 AdjustContrast(Color4 value, float contrast)
Adjusts the contrast of a color.
Color4(Vector4 value)
Initializes a new instance of the SharpDX.Color4 struct.
Represents a four dimensional mathematical vector.
static Color4 Max(Color4 left, Color4 right)
Returns a color containing the largest components of the specified colors.
float R
The red component of the color.
static Color4 AdjustSaturation(Color4 value, float saturation)
Adjusts the saturation of a color.
static void Scale(ref Color4 value, float scale, out Color4 result)
Scales a color.
Color4(float[] values)
Initializes a new instance of the SharpDX.Color4 struct.
override int GetHashCode()
Returns a hash code for this instance.
byte R
The red component of the color.
Vector4 ToVector4()
Converts the color into a four component vector.
string ToString(string format)
Returns a System.String that represents this instance.
static Color4 Min(Color4 left, Color4 right)
Returns a color containing the smallest components of the specified colors.
static Color4 Clamp(Color4 value, Color4 min, Color4 max)
Restricts a value to be within a specified range.
float Y
The Y component of the vector.
static void Clamp(ref Color4 value, ref Color4 min, ref Color4 max, out Color4 result)
Restricts a value to be within a specified range.
static void Add(ref Color4 left, ref Color4 right, out Color4 result)
Adds two colors.
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
static void Modulate(ref Color4 left, ref Color4 right, out Color4 result)
Modulates two colors.
SiliconStudio.Core.Mathematics.Vector3 Vector3
Color4(int rgba)
Initializes a new instance of the SharpDX.Color4 struct.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
float Z
The Z component of the vector.
override string ToString()
Returns a System.String that represents this instance.
static Color4 Add(Color4 left, Color4 right)
Adds two colors.
float Z
The Z component of the vector.
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Color4(float value)
Initializes a new instance of the SharpDX.Color4 struct.
DataStyle
Specifies the style used for textual serialization when an array/list or a dictionary/map must be ser...
static void Max(ref Color4 left, ref Color4 right, out Color4 result)
Returns a color containing the smallest components of the specified colors.
Color4(float red, float green, float blue, float alpha)
Initializes a new instance of the SharpDX.Color4 struct.
static Color4 Subtract(Color4 left, Color4 right)
Subtracts two colors.
Vector3 ToVector3()
Converts the color into a three component vector.
static Color4 Modulate(Color4 left, Color4 right)
Modulates two colors.