4 using System.Globalization;
5 using System.Runtime.InteropServices;
6 using SiliconStudio.Core.Serialization;
8 namespace SiliconStudio.Core.Mathematics
13 [DataContract(
"ColorBGRA")]
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 ColorBGRA(byte red, byte green, byte blue, byte alpha)
84 public ColorBGRA(
float red,
float green,
float blue,
float alpha)
123 A = (byte)((bgra >> 24) & 255);
124 R = (byte)((bgra >> 16) & 255);
125 G = (byte)((bgra >> 8) & 255);
126 B = (byte)(bgra & 255);
135 A = (byte)((bgra >> 24) & 255);
136 R = (byte)((bgra >> 16) & 255);
137 G = (byte)((bgra >> 8) & 255);
138 B = (byte)(bgra & 255);
150 throw new ArgumentNullException(
"values");
151 if (values.Length != 4)
152 throw new ArgumentOutOfRangeException(
"values",
"There must be four and only four input values for ColorBGRA.");
154 B = ToByte(values[0]);
155 G = ToByte(values[1]);
156 R = ToByte(values[2]);
157 A = ToByte(values[3]);
169 throw new ArgumentNullException(
"values");
170 if (values.Length != 4)
171 throw new ArgumentOutOfRangeException(
"values",
"There must be four and only four input values for ColorBGRA.");
186 public byte
this[
int index]
198 throw new ArgumentOutOfRangeException(
"index",
"Indices for ColorBGRA run from 0 to 3, inclusive.");
205 case 0: B = value;
break;
206 case 1: G = value;
break;
207 case 2: R = value;
break;
208 case 3: A = value;
break;
209 default:
throw new ArgumentOutOfRangeException(
"index",
"Indices for ColorBGRA run from 0 to 3, inclusive.");
248 return new Vector3(R / 255.0f, G / 255.0f, B / 255.0f);
257 return new Color3(R / 255.0f, G / 255.0f, B / 255.0f);
266 return new Vector4(R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f);
275 return new[] { B, G, R, A };
284 float r = (float)R / 255.0f;
285 float g = (float)G / 255.0f;
286 float b = (float)B / 255.0f;
292 if (g > max) max = g;
293 if (b > max) max =
b;
295 if (g < min) min = g;
296 if (b < min) min =
b;
298 return (max + min) / 2;
307 if (R == G && G == B)
310 float r = (float)R / 255.0f;
311 float g = (float)G / 255.0f;
312 float b = (float)B / 255.0f;
320 if (g > max) max = g;
321 if (b > max) max =
b;
323 if (g < min) min = g;
324 if (b < min) min =
b;
330 hue = (g -
b) / delta;
334 hue = 2 + (b - r) / delta;
338 hue = 4 + (r - g) / delta;
355 float r = (float)R / 255.0f;
356 float g = (float)G / 255.0f;
357 float b = (float)B / 255.0f;
364 if (g > max) max = g;
365 if (b > max) max =
b;
367 if (g < min) min = g;
368 if (b < min) min =
b;
379 s = (max - min) / (max + min);
383 s = (max - min) / (2 - max - min);
416 return new ColorBGRA((byte)(color & 255), (byte)((color >> 8) & 255), (byte)((color >> 16) & 255), (byte)((color >> 24) & 255));
426 return FromRgba(unchecked((
int)color));
437 result.A = (byte)(left.A + right.A);
438 result.R = (byte)(left.R + right.R);
439 result.G = (byte)(left.G + right.G);
440 result.B = (byte)(left.B + right.B);
451 return new ColorBGRA(left.
R + right.
R, left.
G + right.
G, left.
B + right.
B, left.
A + right.
A);
462 result.A = (byte)(left.A - right.A);
463 result.R = (byte)(left.R - right.R);
464 result.G = (byte)(left.G - right.G);
465 result.B = (byte)(left.B - right.B);
476 return new ColorBGRA(left.
R - right.
R, left.
G - right.
G, left.
B - right.
B, left.
A - right.
A);
487 result.A = (byte)(left.A * right.A / 255.0f);
488 result.R = (byte)(left.R * right.R / 255.0f);
489 result.G = (byte)(left.G * right.G / 255.0f);
490 result.B = (byte)(left.B * right.B / 255.0f);
501 return new ColorBGRA((left.
R * right.
R) >> 8, (left.
G * right.
G) >> 8, (left.
B * right.
B) >> 8, (left.
A * right.
A) >> 8);
512 result.A = (byte)(value.A * scale);
513 result.R = (byte)(value.R * scale);
514 result.G = (byte)(value.G * scale);
515 result.B = (byte)(value.B * scale);
526 return new ColorBGRA((byte)(value.
R * scale), (byte)(value.
G * scale), (byte)(value.
B * scale), (byte)(value.
A * scale));
536 result.A = (byte)(255 - value.A);
537 result.R = (byte)(255 - value.R);
538 result.G = (byte)(255 - value.G);
539 result.B = (byte)(255 - value.B);
549 return new ColorBGRA(255 - value.
R, 255 - value.
G, 255 - value.
B, 255 - value.
A);
561 byte alpha = value.A;
562 alpha = (alpha > max.A) ? max.A : alpha;
563 alpha = (alpha < min.A) ? min.A : alpha;
566 red = (red > max.R) ? max.R : red;
567 red = (red < min.R) ? min.R : red;
569 byte green = value.G;
570 green = (green > max.G) ? max.G : green;
571 green = (green < min.G) ? min.G : green;
574 blue = (blue > max.B) ? max.B : blue;
575 blue = (blue < min.B) ? min.B : blue;
577 result =
new ColorBGRA(red, green, blue, alpha);
590 Clamp(ref value, ref min, ref max, out result);
606 result.B = MathUtil.Lerp(start.B, end.B, amount);
607 result.G = MathUtil.Lerp(start.G, end.G, amount);
608 result.R = MathUtil.Lerp(start.R, end.R, amount);
609 result.A = MathUtil.Lerp(start.A, end.A, amount);
625 Lerp(ref start, ref end, amount, out result);
638 amount = MathUtil.SmoothStep(amount);
639 Lerp(ref start, ref end, amount, out result);
652 SmoothStep(ref start, ref end, amount, out result);
664 result.A = (left.A > right.A) ? left.A : right.A;
665 result.R = (left.R > right.R) ? left.R : right.R;
666 result.G = (left.G > right.G) ? left.G : right.G;
667 result.B = (left.B > right.B) ? left.B : right.B;
679 Max(ref left, ref right, out result);
691 result.A = (left.A < right.A) ? left.A : right.A;
692 result.R = (left.R < right.R) ? left.R : right.R;
693 result.G = (left.G < right.G) ? left.G : right.G;
694 result.B = (left.B < right.B) ? left.B : right.B;
706 Min(ref left, ref right, out result);
719 result.R = ToByte(0.5f + contrast * (value.R / 255.0f - 0.5f));
720 result.G = ToByte(0.5f + contrast * (value.G / 255.0f - 0.5f));
721 result.B = ToByte(0.5f + contrast * (value.B / 255.0f - 0.5f));
733 ToByte(0.5f + contrast * (value.
R / 255.0f - 0.5f)),
734 ToByte(0.5f + contrast * (value.
G / 255.0f - 0.5f)),
735 ToByte(0.5f + contrast * (value.
B / 255.0f - 0.5f)),
747 float grey = value.R / 255.0f * 0.2125f + value.G / 255.0f * 0.7154f + value.B / 255.0f * 0.0721f;
750 result.R = ToByte(grey + saturation * (value.R / 255.0f - grey));
751 result.G = ToByte(grey + saturation * (value.G / 255.0f - grey));
752 result.B = ToByte(grey + saturation * (value.B / 255.0f - grey));
763 float grey = value.R / 255.0f * 0.2125f + value.G / 255.0f * 0.7154f + value.B / 255.0f * 0.0721f;
766 ToByte(grey + saturation * (value.
R / 255.0f - grey)),
767 ToByte(grey + saturation * (value.
G / 255.0f - grey)),
768 ToByte(grey + saturation * (value.
B / 255.0f - grey)),
780 return new ColorBGRA(left.
R + right.
R, left.
G + right.
G, left.
B + right.
B, left.
A + right.
A);
801 return new ColorBGRA(left.
R - right.
R, left.
G - right.
G, left.
B - right.
B, left.
A - right.
A);
811 return new ColorBGRA(-value.
R, -value.
G, -value.
B, -value.
A);
822 return new ColorBGRA((byte)(value.
R * scale), (byte)(value.
G * scale), (byte)(value.
B * scale), (byte)(value.
A * scale));
833 return new ColorBGRA((byte)(value.
R * scale), (byte)(value.
G * scale), (byte)(value.
B * scale), (byte)(value.
A * scale));
844 return new ColorBGRA((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));
855 return left.Equals(right);
866 return !left.Equals(right);
876 return new Color3(value.
R, value.
G, value.
B);
886 return new Vector3(value.
R / 255.0f, value.
G / 255.0f, value.
B / 255.0f);
896 return new Vector4(value.
R / 255.0f, value.
G / 255.0f, value.
B / 255.0f, value.
A / 255.0f);
906 return new Color4(value.
R / 255.0f, value.
G / 255.0f, value.
B / 255.0f, value.
A / 255.0f);
916 return new ColorBGRA(value.
X / 255.0f, value.
Y / 255.0f, value.
Z / 255.0f, 1.0f);
966 return new Color(value.
R, value.
G, value.
B, value.
A);
978 return value.ToBgra();
1001 return ToString(CultureInfo.CurrentCulture);
1013 return ToString(format, CultureInfo.CurrentCulture);
1025 return string.Format(formatProvider, toStringFormat, A, R, G, B);
1039 return ToString(formatProvider);
1041 return string.Format(formatProvider,
1043 A.ToString(
format, formatProvider),
1044 R.ToString(format, formatProvider),
1045 G.ToString(
format, formatProvider),
1046 B.ToString(format, formatProvider));
1057 return A.GetHashCode() + R.GetHashCode() + G.GetHashCode() + B.GetHashCode();
1069 return R == other.R && G == other.G && B == other.B && A == other.A;
1084 if (!ReferenceEquals(value.GetType(), typeof(
ColorBGRA)))
1090 private static byte ToByte(
float component)
1092 var value = (int)(component * 255.0f);
1093 return (byte)(value < 0 ? 0 : value > 255 ? 255 : value);
static void Scale(ref ColorBGRA value, float scale, out ColorBGRA result)
Scales a color.
byte B
The blue component of the color.
static ColorBGRA FromRgba(int color)
Converts the color from a packed RGBA integer.
FbxDouble3 operator*(double factor, FbxDouble3 vector)
float Y
The Y component of the vector.
float A
The alpha component of the color.
static void AdjustContrast(ref ColorBGRA value, float contrast, out ColorBGRA result)
Adjusts the contrast of a color.
float W
The W component of the vector.
float GetHue()
Gets the hue.
byte G
The green component of the color.
static void Min(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
Returns a color containing the smallest components of the specified colors.
static void Add(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
Adds two colors.
static ColorBGRA Subtract(ColorBGRA left, ColorBGRA right)
Subtracts two colors.
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
byte B
The blue component of the color.
bool Equals(ColorBGRA other)
Determines whether the specified ColorBGRA is equal to this instance.
static ColorBGRA Lerp(ColorBGRA start, ColorBGRA end, float amount)
Performs a linear interpolation between two colors.
byte A
The alpha component of the color.
Represents a color in the form of rgb.
ColorBGRA(byte red, byte green, byte blue, byte alpha)
Initializes a new instance of the ColorBGRA struct.
float B
The blue component of the color.
ColorBGRA(byte[] values)
Initializes a new instance of the ColorBGRA struct.
ColorBGRA(float[] values)
Initializes a new instance of the ColorBGRA struct.
override string ToString()
Returns a System.String that represents this instance.
static ColorBGRA FromBgra(uint color)
Converts the color from a packed BGRA integer.
ColorBGRA(Vector3 value, float alpha)
Initializes a new instance of the ColorBGRA struct.
float X
The X component of the vector.
float R
The red component of the color.
static ColorBGRA Scale(ColorBGRA value, float scale)
Scales a color.
byte R
The red component of the color.
float X
The X component of the vector.
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).
static void Lerp(ref ColorBGRA start, ref ColorBGRA end, float amount, out ColorBGRA result)
Performs a linear interpolation between two colors.
float G
The green component of the color.
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
Represents a color in the form of rgba.
Vector3 ToVector3()
Converts the color into a three component vector.
ColorBGRA(int bgra)
Initializes a new instance of the ColorBGRA struct.
static void Clamp(ref ColorBGRA value, ref ColorBGRA min, ref ColorBGRA max, out ColorBGRA result)
Restricts a value to be within a specified range.
Color3 ToColor3()
Converts the color into a three component color.
int ToBgra()
Converts the color into a packed integer.
float GetSaturation()
Gets the saturation.
static void Max(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
Returns a color containing the smallest components of the specified colorss.
Represents a four dimensional mathematical vector.
byte A
The alpha component of the color.
float R
The red component of the color.
static void Modulate(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
Modulates two colors.
SiliconStudio.Core.Mathematics.Color Color
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
byte[] ToArray()
Creates an array containing the elements of the color.
byte R
The red component of the color.
static void Negate(ref ColorBGRA value, out ColorBGRA result)
Negates a color.
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Vector4 ToVector4()
Converts the color into a four component vector.
static void Subtract(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
Subtracts two colors.
float Y
The Y component of the vector.
ColorBGRA(uint bgra)
Initializes a new instance of the ColorBGRA struct.
static ColorBGRA FromRgba(uint color)
Converts the color from a packed RGBA integer.
static ColorBGRA SmoothStep(ColorBGRA start, ColorBGRA end, float amount)
Performs a cubic interpolation between two colors.
static ColorBGRA Modulate(ColorBGRA left, ColorBGRA right)
Modulates two colors.
int ToRgba()
Converts the color into a packed integer.
float G
The green component of the color.
static ColorBGRA Negate(ColorBGRA value)
Negates a color.
SiliconStudio.Core.Mathematics.Vector3 Vector3
string ToString(string format)
Returns a System.String that represents this instance.
static ColorBGRA Min(ColorBGRA left, ColorBGRA right)
Returns a color containing the smallest components of the specified colors.
static void AdjustSaturation(ref ColorBGRA value, float saturation, out ColorBGRA result)
Adjusts the saturation of a color.
static ColorBGRA Clamp(ColorBGRA value, ColorBGRA min, ColorBGRA max)
Restricts a value to be within a specified range.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
ColorBGRA(byte value)
Initializes a new instance of the ColorBGRA struct.
float Z
The Z component of the vector.
override int GetHashCode()
Returns a hash code for this instance.
float GetBrightness()
Gets the brightness.
float Z
The Z component of the vector.
ColorBGRA(Vector4 value)
Initializes a new instance of the ColorBGRA struct.
static ColorBGRA FromBgra(int color)
Converts the color from a packed BGRA integer.
static ColorBGRA Max(ColorBGRA left, ColorBGRA right)
Returns a color containing the largest components of the specified colorss.
static ColorBGRA AdjustContrast(ColorBGRA value, float contrast)
Adjusts the contrast of a color.
static ColorBGRA AdjustSaturation(ColorBGRA value, float saturation)
Adjusts the saturation of a color.
DataStyle
Specifies the style used for textual serialization when an array/list or a dictionary/map must be ser...
ColorBGRA(float value)
Initializes a new instance of the ColorBGRA struct.
byte G
The green component of the color.
static void SmoothStep(ref ColorBGRA start, ref ColorBGRA end, float amount, out ColorBGRA result)
Performs a cubic interpolation between two colors.
static ColorBGRA Add(ColorBGRA left, ColorBGRA right)
Adds two colors.
ColorBGRA(float red, float green, float blue, float alpha)
Initializes a new instance of the ColorBGRA struct.