4 using System.Globalization;
5 using System.Runtime.InteropServices;
6 using SiliconStudio.Core.Serialization;
8 namespace SiliconStudio.Core.Mathematics
13 [DataContract(
"Color")]
15 [StructLayout(LayoutKind.Sequential, Size = 4)]
18 private const string toStringFormat =
"A:{0} R:{1} G:{2} B:{3}";
50 A = R = G = B = value;
59 A = R = G = B = ToByte(value);
69 public Color(byte red, byte green, byte blue, byte alpha)
83 public Color(byte red, byte green, byte blue)
98 public Color(
float red,
float green,
float blue,
float alpha)
112 public Color(
float red,
float green,
float blue)
163 A = (byte)((rgba >> 24) & 255);
164 B = (byte)((rgba >> 16) & 255);
165 G = (byte)((rgba >> 8) & 255);
166 R = (byte)(rgba & 255);
175 A = (byte)((rgba >> 24) & 255);
176 B = (byte)((rgba >> 16) & 255);
177 G = (byte)((rgba >> 8) & 255);
178 R = (byte)(rgba & 255);
190 throw new ArgumentNullException(
"values");
191 if (values.Length != 4)
192 throw new ArgumentOutOfRangeException(
"values",
"There must be four and only four input values for Color.");
194 R = ToByte(values[0]);
195 G = ToByte(values[1]);
196 B = ToByte(values[2]);
197 A = ToByte(values[3]);
209 throw new ArgumentNullException(
"values");
210 if (values.Length != 4)
211 throw new ArgumentOutOfRangeException(
"values",
"There must be four and only four input values for Color.");
226 public byte
this[
int index]
238 throw new ArgumentOutOfRangeException(
"index",
"Indices for Color run from 0 to 3, inclusive.");
245 case 0: R = value;
break;
246 case 1: G = value;
break;
247 case 2: B = value;
break;
248 case 3: A = value;
break;
249 default:
throw new ArgumentOutOfRangeException(
"index",
"Indices for Color run from 0 to 3, inclusive.");
302 return new Vector3(R / 255.0f, G / 255.0f, B / 255.0f);
311 return new Color3(R / 255.0f, G / 255.0f, B / 255.0f);
320 return new Vector4(R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f);
329 return new[] { R, G, B, A };
338 float r = (float)R / 255.0f;
339 float g = (float)G / 255.0f;
340 float b = (float)B / 255.0f;
346 if (g > max) max = g;
347 if (b > max) max =
b;
349 if (g < min) min = g;
350 if (b < min) min =
b;
352 return (max + min) / 2;
361 if (R == G && G == B)
364 float r = (float)R / 255.0f;
365 float g = (float)G / 255.0f;
366 float b = (float)B / 255.0f;
374 if (g > max) max = g;
375 if (b > max) max =
b;
377 if (g < min) min = g;
378 if (b < min) min =
b;
384 hue = (g -
b) / delta;
388 hue = 2 + (b - r) / delta;
392 hue = 4 + (r - g) / delta;
409 float r = (float)R / 255.0f;
410 float g = (float)G / 255.0f;
411 float b = (float)B / 255.0f;
418 if (g > max) max = g;
419 if (b > max) max =
b;
421 if (g < min) min = g;
422 if (b < min) min =
b;
433 s = (max - min) / (max + min);
437 s = (max - min) / (2 - max - min);
451 result.A = (byte)(left.A + right.A);
452 result.R = (byte)(left.R + right.R);
453 result.G = (byte)(left.G + right.G);
454 result.B = (byte)(left.B + right.B);
465 return new Color(left.
R + right.
R, left.
G + right.
G, left.
B + right.
B, left.
A + right.
A);
476 result.A = (byte)(left.A - right.A);
477 result.R = (byte)(left.R - right.R);
478 result.G = (byte)(left.G - right.G);
479 result.B = (byte)(left.B - right.B);
490 return new Color(left.
R - right.
R, left.
G - right.
G, left.
B - right.
B, left.
A - right.
A);
501 result.A = (byte)(left.A * right.A / 255.0f);
502 result.R = (byte)(left.R * right.R / 255.0f);
503 result.G = (byte)(left.G * right.G / 255.0f);
504 result.B = (byte)(left.B * right.B / 255.0f);
515 return new Color(left.
R * right.
R, left.
G * right.
G, left.
B * right.
B, left.
A * right.
A);
526 result.A = (byte)(value.A * scale);
527 result.R = (byte)(value.R * scale);
528 result.G = (byte)(value.G * scale);
529 result.B = (byte)(value.B * scale);
540 return new Color((byte)(value.
R * scale), (byte)(value.
G * scale), (byte)(value.
B * scale), (byte)(value.
A * scale));
550 result.A = (byte)(255 - value.A);
551 result.R = (byte)(255 - value.R);
552 result.G = (byte)(255 - value.G);
553 result.B = (byte)(255 - value.B);
563 return new Color(255 - value.
R, 255 - value.
G, 255 - value.
B, 255 - value.
A);
575 byte alpha = value.A;
576 alpha = (alpha > max.A) ? max.A : alpha;
577 alpha = (alpha < min.A) ? min.A : alpha;
580 red = (red > max.R) ? max.R : red;
581 red = (red < min.R) ? min.R : red;
583 byte green = value.G;
584 green = (green > max.G) ? max.G : green;
585 green = (green < min.G) ? min.G : green;
588 blue = (blue > max.B) ? max.B : blue;
589 blue = (blue < min.B) ? min.B : blue;
591 result =
new Color(red, green, blue, alpha);
601 return new Color((byte)((color >> 16) & 255), (byte)((color >> 8) & 255), (byte)(color & 255), (byte)((color >> 24) & 255));
611 return FromBgra(unchecked((
int)color));
621 return new Color((byte)(color >> 24), (byte)(color >> 16), (byte)(color >> 8), (byte)color);
631 return FromAbgr(unchecked((
int)color));
641 return new Color(color);
651 return new Color(color);
664 Clamp(ref value, ref min, ref max, out result);
680 result.R = MathUtil.Lerp(start.R, end.R, amount);
681 result.G = MathUtil.Lerp(start.G, end.G, amount);
682 result.B = MathUtil.Lerp(start.B, end.B, amount);
683 result.A = MathUtil.Lerp(start.A, end.A, amount);
699 Lerp(ref start, ref end, amount, out result);
712 amount = MathUtil.SmoothStep(amount);
713 Lerp(ref start, ref end, amount, out result);
726 SmoothStep(ref start, ref end, amount, out result);
738 result.A = (left.A > right.A) ? left.A : right.A;
739 result.R = (left.R > right.R) ? left.R : right.R;
740 result.G = (left.G > right.G) ? left.G : right.G;
741 result.B = (left.B > right.B) ? left.B : right.B;
753 Max(ref left, ref right, out result);
765 result.A = (left.A < right.A) ? left.A : right.A;
766 result.R = (left.R < right.R) ? left.R : right.R;
767 result.G = (left.G < right.G) ? left.G : right.G;
768 result.B = (left.B < right.B) ? left.B : right.B;
780 Min(ref left, ref right, out result);
793 result.R = ToByte(0.5f + contrast * (value.R / 255.0f - 0.5f));
794 result.G = ToByte(0.5f + contrast * (value.G / 255.0f - 0.5f));
795 result.B = ToByte(0.5f + contrast * (value.B / 255.0f - 0.5f));
807 ToByte(0.5f + contrast * (value.
R / 255.0f - 0.5f)),
808 ToByte(0.5f + contrast * (value.
G / 255.0f - 0.5f)),
809 ToByte(0.5f + contrast * (value.
B / 255.0f - 0.5f)),
821 float grey = value.R / 255.0f * 0.2125f + value.G / 255.0f * 0.7154f + value.B / 255.0f * 0.0721f;
824 result.R = ToByte(grey + saturation * (value.R / 255.0f - grey));
825 result.G = ToByte(grey + saturation * (value.G / 255.0f - grey));
826 result.B = ToByte(grey + saturation * (value.B / 255.0f - grey));
837 float grey = value.R / 255.0f * 0.2125f + value.G / 255.0f * 0.7154f + value.B / 255.0f * 0.0721f;
840 ToByte(grey + saturation * (value.
R / 255.0f - grey)),
841 ToByte(grey + saturation * (value.
G / 255.0f - grey)),
842 ToByte(grey + saturation * (value.
B / 255.0f - grey)),
854 return new Color(left.
R + right.
R, left.
G + right.
G, left.
B + right.
B, left.
A + right.
A);
875 return new Color(left.
R - right.
R, left.
G - right.
G, left.
B - right.
B, left.
A - right.
A);
885 return new Color(-value.
R, -value.
G, -value.
B, -value.
A);
896 return new Color((byte)(value.
R * scale), (byte)(value.
G * scale), (byte)(value.
B * scale), (byte)(value.
A * scale));
907 return new Color((byte)(value.
R * scale), (byte)(value.
G * scale), (byte)(value.
B * scale), (byte)(value.
A * scale));
918 return new Color((byte)(left.
R * right.
R / 255.0f), (byte)(left.
G * right.
G / 255.0f), (byte)(left.
B * right.
B / 255.0f), (byte)(left.
A * right.
A / 255.0f));
929 return left.Equals(right);
940 return !left.Equals(right);
950 return value.ToColor3();
960 return new Vector3(value.
R / 255.0f, value.
G / 255.0f, value.
B / 255.0f);
970 return new Vector4(value.
R / 255.0f, value.
G / 255.0f, value.
B / 255.0f, value.
A / 255.0f);
979 return new Color4(R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f);
989 return value.ToColor4();
999 return new Color(value.
X, value.
Y, value.
Z, 1.0f);
1009 return new Color(value.
R, value.
G, value.
B, 1.0f);
1019 return new Color(value.
X, value.
Y, value.
Z, value.
W);
1029 return new Color(value.
R, value.
G, value.
B, value.
A);
1039 public static explicit operator int(
Color value)
1041 return value.ToRgba();
1051 public static explicit operator Color(
int value)
1053 return new Color(value);
1064 return ToString(CultureInfo.CurrentCulture);
1076 return ToString(format, CultureInfo.CurrentCulture);
1088 return string.Format(formatProvider, toStringFormat, A, R, G, B);
1102 return ToString(formatProvider);
1104 return string.Format(formatProvider,
1106 A.ToString(
format, formatProvider),
1107 R.ToString(format, formatProvider),
1108 G.ToString(
format, formatProvider),
1109 B.ToString(format, formatProvider));
1120 return A.GetHashCode() + R.GetHashCode() + G.GetHashCode() + B.GetHashCode();
1132 return R == other.R && G == other.G && B == other.B && A == other.A;
1147 if (!ReferenceEquals(value.GetType(), typeof(
Color)))
1150 return Equals((
Color)value);
1153 private static byte ToByte(
float component)
1155 var value = (int)(component * 255.0f);
1156 return (byte)(value < 0 ? 0 : value > 255 ? 255 : value);
static void AdjustSaturation(ref Color value, float saturation, out Color result)
Adjusts the saturation of a color.
byte B
The blue component of the color.
Color(byte red, byte green, byte blue)
Initializes a new instance of the Color struct. Alpha is set to 255.
FbxDouble3 operator*(double factor, FbxDouble3 vector)
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
bool Equals(Color other)
Determines whether the specified Color is equal to this instance.
static void Negate(ref Color value, out Color result)
Negates a color.
float Y
The Y component of the vector.
float A
The alpha component of the color.
Color(byte red, byte green, byte blue, byte alpha)
Initializes a new instance of the Color struct.
static void Min(ref Color left, ref Color right, out Color result)
Returns a color containing the smallest components of the specified colors.
Color(float value)
Initializes a new instance of the Color struct.
float W
The W component of the vector.
int ToAbgr()
Converts the color into a packed integer.
Color(byte value)
Initializes a new instance of the Color struct.
static Color Scale(Color value, float scale)
Scales a color.
Color(uint rgba)
Initializes a new instance of the Color struct.
Represents a color in the form of rgb.
Color3 ToColor3()
Converts the color into a three component color.
static Color Clamp(Color value, Color min, Color max)
Restricts a value to be within a specified range.
float B
The blue component of the color.
static void Lerp(ref Color start, ref Color end, float amount, out Color result)
Performs a linear interpolation between two colors.
float X
The X component of the vector.
static Color SmoothStep(Color start, Color end, float amount)
Performs a cubic interpolation between two colors.
static Color FromBgra(int color)
Converts the color from a packed BGRA integer.
float R
The red component of the color.
string ToString(string format)
Returns a System.String that represents this instance.
byte R
The red component of the color.
float X
The X component of the vector.
Color(Vector3 value)
Initializes a new instance of the Color struct. Alpha is set to 255.
Color(float red, float green, float blue)
Initializes a new instance of the Color struct. Alpha is set to 255.
Represents a three dimensional mathematical vector.
float B
The blue component of the color.
float GetSaturation()
Gets the saturation.
float G
The green component of the color.
Color(Vector4 value)
Initializes a new instance of the Color struct.
static Color Subtract(Color left, Color right)
Subtracts two colors.
Represents a color in the form of rgba.
static Color FromBgra(uint color)
Converts the color from a packed BGRA integer.
static Color FromAbgr(int color)
Converts the color from a packed ABGR integer.
static Color Negate(Color value)
Negates a color.
override int GetHashCode()
Returns a hash code for this instance.
static void Scale(ref Color value, float scale, out Color result)
Scales a color.
static void Modulate(ref Color left, ref Color right, out Color result)
Modulates two colors.
Represents a four dimensional mathematical vector.
byte A
The alpha component of the color.
float R
The red component of the color.
Vector4 ToVector4()
Converts the color into a four component vector.
SiliconStudio.Core.Mathematics.Color Color
static Color FromAbgr(uint color)
Converts the color from a packed ABGR integer.
static void Max(ref Color left, ref Color right, out Color result)
Returns a color containing the smallest components of the specified colors.
static Color FromRgba(int color)
Converts the color from a packed BGRA integer.
override string ToString()
Returns a System.String that represents this instance.
int ToBgra()
Converts the color into a packed integer.
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
Color4 ToColor4()
Convert this instance to a Color4
Color(Vector3 value, float alpha)
Initializes a new instance of the Color struct.
byte[] ToArray()
Creates an array containing the elements of the color.
static Color Modulate(Color left, Color right)
Modulates two colors.
static Color Add(Color left, Color right)
Adds two colors.
static Color AdjustContrast(Color value, float contrast)
Adjusts the contrast of a color.
Color(int rgba)
Initializes a new instance of the Color struct.
float Y
The Y component of the vector.
int ToRgba()
Converts the color into a packed integer.
static void Clamp(ref Color value, ref Color min, ref Color max, out Color result)
Restricts a value to be within a specified range.
Vector3 ToVector3()
Converts the color into a three component vector.
static Color Lerp(Color start, Color end, float amount)
Performs a linear interpolation between two colors.
float G
The green component of the color.
SiliconStudio.Core.Mathematics.Vector3 Vector3
static void Subtract(ref Color left, ref Color right, out Color result)
Subtracts two colors.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
float Z
The Z component of the vector.
float GetHue()
Gets the hue.
float Z
The Z component of the vector.
Color(float red, float green, float blue, float alpha)
Initializes a new instance of the Color struct.
static void Add(ref Color left, ref Color right, out Color result)
Adds two colors.
static void SmoothStep(ref Color start, ref Color end, float amount, out Color result)
Performs a cubic interpolation between two colors.
static Color FromRgba(uint color)
Converts the color from a packed BGRA integer.
Color(float[] values)
Initializes a new instance of the Color struct.
static Color AdjustSaturation(Color value, float saturation)
Adjusts the saturation of a color.
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
DataStyle
Specifies the style used for textual serialization when an array/list or a dictionary/map must be ser...
static void AdjustContrast(ref Color value, float contrast, out Color result)
Adjusts the contrast of a color.
byte G
The green component of the color.
static Color Min(Color left, Color right)
Returns a color containing the smallest components of the specified colors.
static Color Max(Color left, Color right)
Returns a color containing the largest components of the specified colorss.
float GetBrightness()
Gets the brightness.
Color(byte[] values)
Initializes a new instance of the Color struct.
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.