// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
namespace math
{
///
/// Represents a 32-bit color (4 bytes) in the form of BGRA (in byte order: B, G, R, A).
///
[DataContract( Name = "ColorBGRA")]
[DataStyle(DataStyle.Compact)]
[StructLayout(LayoutKind.Sequential, Size = 4)]
public partial struct ColorBGRA : IEquatable, IFormattable
{
private const string ToStringFormat = "A:{0} R:{1} G:{2} B:{3}";
///
/// The blue component of the color.
///
[DataMember( Order = 0 )]
public byte B;
///
/// The green component of the color.
///
[DataMember( Order = 1 )]
public byte G;
///
/// The red component of the color.
///
[DataMember( Order = 2 )]
public byte R;
///
/// The alpha component of the color.
///
[DataMember( Order = 3 )]
public byte A;
///
/// Initializes a new instance of the struct.
///
/// The value that will be assigned to all components.
public ColorBGRA(byte value)
{
A = R = G = B = value;
}
///
/// Initializes a new instance of the struct.
///
/// The value that will be assigned to all components.
public ColorBGRA(float value)
{
A = R = G = B = ToByte(value);
}
///
/// Initializes a new instance of the struct.
///
/// The red component of the color.
/// The green component of the color.
/// The blue component of the color.
/// The alpha component of the color.
public ColorBGRA(byte red, byte green, byte blue, byte alpha)
{
R = red;
G = green;
B = blue;
A = alpha;
}
///
/// Initializes a new instance of the struct.
///
/// The red component of the color.
/// The green component of the color.
/// The blue component of the color.
/// The alpha component of the color.
public ColorBGRA(float red, float green, float blue, float alpha)
{
R = ToByte(red);
G = ToByte(green);
B = ToByte(blue);
A = ToByte(alpha);
}
///
/// Initializes a new instance of the struct.
///
/// The red, green, blue, and alpha components of the color.
public ColorBGRA(Vec4 value)
{
R = ToByte(value.X);
G = ToByte(value.Y);
B = ToByte(value.Z);
A = ToByte(value.W);
}
///
/// Initializes a new instance of the struct.
///
/// The red, green, and blue components of the color.
/// The alpha component of the color.
public ColorBGRA(Vec3 value, float alpha)
{
R = ToByte(value.X);
G = ToByte(value.Y);
B = ToByte(value.Z);
A = ToByte(alpha);
}
///
/// Initializes a new instance of the struct.
///
/// A packed integer containing all four color components in BGRA order.
public ColorBGRA(uint bgra)
{
A = (byte)((bgra >> 24) & 255);
R = (byte)((bgra >> 16) & 255);
G = (byte)((bgra >> 8) & 255);
B = (byte)(bgra & 255);
}
///
/// Initializes a new instance of the struct.
///
/// A packed integer containing all four color components in BGRA.
public ColorBGRA(int bgra)
{
A = (byte)((bgra >> 24) & 255);
R = (byte)((bgra >> 16) & 255);
G = (byte)((bgra >> 8) & 255);
B = (byte)(bgra & 255);
}
///
/// Initializes a new instance of the struct.
///
/// The values to assign to the red, green, and blue, alpha components of the color. This must be an array with four elements.
/// Thrown when is null.
/// Thrown when contains more or less than four elements.
public ColorBGRA(float[] values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
if (values.Length != 4)
throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for ColorBGRA.");
B = ToByte(values[0]);
G = ToByte(values[1]);
R = ToByte(values[2]);
A = ToByte(values[3]);
}
///
/// Initializes a new instance of the struct.
///
/// The values to assign to the red, green, and blue, alpha components of the color. This must be an array with four elements.
/// Thrown when is null.
/// Thrown when contains more or less than four elements.
public ColorBGRA(byte[] values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
if (values.Length != 4)
throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for ColorBGRA.");
B = values[0];
G = values[1];
R = values[2];
A = values[3];
}
///
/// Gets or sets the component at the specified index.
///
/// The value of the alpha, red, green, or blue component, depending on the 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.
/// The value of the component at the specified index.
/// Thrown when the is out of the range [0, 3].
public byte this[int index]
{
get
{
switch (index)
{
case 0: return B;
case 1: return G;
case 2: return R;
case 3: return A;
}
throw new ArgumentOutOfRangeException(nameof(index), "Indices for ColorBGRA run from 0 to 3, inclusive.");
}
set
{
switch (index)
{
case 0: B = value; break;
case 1: G = value; break;
case 2: R = value; break;
case 3: A = value; break;
default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for ColorBGRA run from 0 to 3, inclusive.");
}
}
}
///
/// Converts the color into a packed integer.
///
/// A packed integer containing all four color components.
public int ToBgra()
{
int value = B;
value |= G << 8;
value |= R << 16;
value |= A << 24;
return value;
}
///
/// Converts the color into a packed integer.
///
/// A packed integer containing all four color components.
public int ToRgba()
{
int value = R;
value |= G << 8;
value |= B << 16;
value |= A << 24;
return value;
}
///
/// Converts the color into a three component vector.
///
/// A three component vector containing the red, green, and blue components of the color.
public Vec3 ToVector3()
{
return new Vec3(R / 255.0f, G / 255.0f, B / 255.0f);
}
///
/// Converts the color into a three component color.
///
/// A three component color containing the red, green, and blue components of the color.
public Color3 ToColor3()
{
return new Color3(R / 255.0f, G / 255.0f, B / 255.0f);
}
///
/// Converts the color into a four component vector.
///
/// A four component vector containing all four color components.
public Vec4 ToVector4()
{
return new Vec4(R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f);
}
///
/// Creates an array containing the elements of the color.
///
/// A four-element array containing the components of the color in BGRA order.
public byte[] ToArray()
{
return new[] { B, G, R, A };
}
///
/// Gets the brightness.
///
/// The Hue-Saturation-Brightness (HSB) saturation for this
public float GetBrightness()
{
float r = R / 255.0f;
float g = G / 255.0f;
float b = B / 255.0f;
float max, min;
max = r;
min = r;
if (g > max) max = g;
if (b > max) max = b;
if (g < min) min = g;
if (b < min) min = b;
return (max + min) / 2;
}
///
/// Gets the hue.
///
/// The Hue-Saturation-Brightness (HSB) saturation for this
public float GetHue()
{
if (R == G && G == B)
return 0; // 0 makes as good an UNDEFINED value as any
float r = R / 255.0f;
float g = G / 255.0f;
float b = B / 255.0f;
float max, min;
float delta;
float hue = 0.0f;
max = r;
min = r;
if (g > max) max = g;
if (b > max) max = b;
if (g < min) min = g;
if (b < min) min = b;
delta = max - min;
if (r == max)
{
hue = (g - b) / delta;
}
else if (g == max)
{
hue = 2 + (b - r) / delta;
}
else if (b == max)
{
hue = 4 + (r - g) / delta;
}
hue *= 60;
if (hue < 0.0f)
{
hue += 360.0f;
}
return hue;
}
///
/// Gets the saturation.
///
/// The Hue-Saturation-Brightness (HSB) saturation for this
public float GetSaturation()
{
float r = R / 255.0f;
float g = G / 255.0f;
float b = B / 255.0f;
float max, min;
float l, s = 0;
max = r;
min = r;
if (g > max) max = g;
if (b > max) max = b;
if (g < min) min = g;
if (b < min) min = b;
// if max == min, then there is no color and
// the saturation is zero.
if (max != min)
{
l = (max + min) / 2;
if (l <= .5)
{
s = (max - min) / (max + min);
}
else
{
s = (max - min) / (2 - max - min);
}
}
return s;
}
///
/// Converts the color from a packed BGRA integer.
///
/// A packed integer containing all four color components in BGRA order
/// A color.
public static ColorBGRA FromBgra(int color)
{
return new ColorBGRA(color);
}
///
/// Converts the color from a packed BGRA integer.
///
/// A packed integer containing all four color components in BGRA order
/// A color.
public static ColorBGRA FromBgra(uint color)
{
return new ColorBGRA(color);
}
///
/// Converts the color from a packed RGBA integer.
///
/// A packed integer containing all four color components in RGBA order
/// A color.
public static ColorBGRA FromRgba(int color)
{
return new ColorBGRA((byte)(color & 255), (byte)((color >> 8) & 255), (byte)((color >> 16) & 255), (byte)((color >> 24) & 255));
}
///
/// Converts the color from a packed RGBA integer.
///
/// A packed integer containing all four color components in RGBA order
/// A color.
public static ColorBGRA FromRgba(uint color)
{
return FromRgba(unchecked((int)color));
}
///
/// Adds two colors.
///
/// The first color to add.
/// The second color to add.
/// When the method completes, completes the sum of the two colors.
public static void Add(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
{
result.A = (byte)(left.A + right.A);
result.R = (byte)(left.R + right.R);
result.G = (byte)(left.G + right.G);
result.B = (byte)(left.B + right.B);
}
///
/// Adds two colors.
///
/// The first color to add.
/// The second color to add.
/// The sum of the two colors.
public static ColorBGRA Add(ColorBGRA left, ColorBGRA right)
{
return new ColorBGRA((byte)(left.R + right.R), (byte)(left.G + right.G), (byte)(left.B + right.B), (byte)(left.A + right.A));
}
///
/// Subtracts two colors.
///
/// The first color to subtract.
/// The second color to subtract.
/// WHen the method completes, contains the difference of the two colors.
public static void Subtract(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
{
result.A = (byte)(left.A - right.A);
result.R = (byte)(left.R - right.R);
result.G = (byte)(left.G - right.G);
result.B = (byte)(left.B - right.B);
}
///
/// Subtracts two colors.
///
/// The first color to subtract.
/// The second color to subtract
/// The difference of the two colors.
public static ColorBGRA Subtract(ColorBGRA left, ColorBGRA right)
{
return new ColorBGRA((byte)(left.R - right.R), (byte)(left.G - right.G), (byte)(left.B - right.B), (byte)(left.A - right.A));
}
///
/// Modulates two colors.
///
/// The first color to modulate.
/// The second color to modulate.
/// When the method completes, contains the modulated color.
public static void Modulate(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
{
result.A = (byte)(left.A * right.A / 255);
result.R = (byte)(left.R * right.R / 255);
result.G = (byte)(left.G * right.G / 255);
result.B = (byte)(left.B * right.B / 255);
}
///
/// Modulates two colors.
///
/// The first color to modulate.
/// The second color to modulate.
/// The modulated color.
public static ColorBGRA Modulate(ColorBGRA left, ColorBGRA right)
{
return new ColorBGRA((byte)((left.R * right.R) / 255), (byte)((left.G * right.G) / 255), (byte)((left.B * right.B) / 255), (byte)((left.A * right.A) / 255));
}
///
/// Scales a color.
///
/// The color to scale.
/// The amount by which to scale.
/// When the method completes, contains the scaled color.
public static void Scale(ref ColorBGRA value, float scale, out ColorBGRA result)
{
result.A = (byte)(value.A * scale);
result.R = (byte)(value.R * scale);
result.G = (byte)(value.G * scale);
result.B = (byte)(value.B * scale);
}
///
/// Scales a color.
///
/// The color to scale.
/// The amount by which to scale.
/// The scaled color.
public static ColorBGRA Scale(ColorBGRA value, float scale)
{
return new ColorBGRA((byte)(value.R * scale), (byte)(value.G * scale), (byte)(value.B * scale), (byte)(value.A * scale));
}
///
/// Negates a color.
///
/// The color to negate.
/// When the method completes, contains the negated color.
public static void Negate(ref ColorBGRA value, out ColorBGRA result)
{
result.A = (byte)(255 - value.A);
result.R = (byte)(255 - value.R);
result.G = (byte)(255 - value.G);
result.B = (byte)(255 - value.B);
}
///
/// Negates a color.
///
/// The color to negate.
/// The negated color.
public static ColorBGRA Negate(ColorBGRA value)
{
return new ColorBGRA((byte)(255 - value.R), (byte)(255 - value.G), (byte)(255 - value.B), (byte)(255 - value.A));
}
///
/// Restricts a value to be within a specified range.
///
/// The value to clamp.
/// The minimum value.
/// The maximum value.
/// When the method completes, contains the clamped value.
public static void Clamp(ref ColorBGRA value, ref ColorBGRA min, ref ColorBGRA max, out ColorBGRA result)
{
byte alpha = value.A;
alpha = (alpha > max.A) ? max.A : alpha;
alpha = (alpha < min.A) ? min.A : alpha;
byte red = value.R;
red = (red > max.R) ? max.R : red;
red = (red < min.R) ? min.R : red;
byte green = value.G;
green = (green > max.G) ? max.G : green;
green = (green < min.G) ? min.G : green;
byte blue = value.B;
blue = (blue > max.B) ? max.B : blue;
blue = (blue < min.B) ? min.B : blue;
result = new ColorBGRA(red, green, blue, alpha);
}
///
/// Restricts a value to be within a specified range.
///
/// The value to clamp.
/// The minimum value.
/// The maximum value.
/// The clamped value.
public static ColorBGRA Clamp(ColorBGRA value, ColorBGRA min, ColorBGRA max)
{
ColorBGRA result;
Clamp(ref value, ref min, ref max, out result);
return result;
}
///
/// Performs a linear interpolation between two colors.
///
/// Start color.
/// End color.
/// Value between 0 and 1 indicating the weight of .
/// When the method completes, contains the linear interpolation of the two colors.
///
/// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned.
///
public static void Lerp(ref ColorBGRA start, ref ColorBGRA end, float amount, out ColorBGRA result)
{
result.B = MathUtil.Lerp(start.B, end.B, amount);
result.G = MathUtil.Lerp(start.G, end.G, amount);
result.R = MathUtil.Lerp(start.R, end.R, amount);
result.A = MathUtil.Lerp(start.A, end.A, amount);
}
///
/// Performs a linear interpolation between two colors.
///
/// Start color.
/// End color.
/// Value between 0 and 1 indicating the weight of .
/// The linear interpolation of the two colors.
///
/// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned.
///
public static ColorBGRA Lerp(ColorBGRA start, ColorBGRA end, float amount)
{
ColorBGRA result;
Lerp(ref start, ref end, amount, out result);
return result;
}
///
/// Performs a cubic interpolation between two colors.
///
/// Start color.
/// End color.
/// Value between 0 and 1 indicating the weight of .
/// When the method completes, contains the cubic interpolation of the two colors.
public static void SmoothStep(ref ColorBGRA start, ref ColorBGRA end, float amount, out ColorBGRA result)
{
amount = MathUtil.SmoothStep(amount);
Lerp(ref start, ref end, amount, out result);
}
///
/// Performs a cubic interpolation between two colors.
///
/// Start color.
/// End color.
/// Value between 0 and 1 indicating the weight of .
/// The cubic interpolation of the two colors.
public static ColorBGRA SmoothStep(ColorBGRA start, ColorBGRA end, float amount)
{
ColorBGRA result;
SmoothStep(ref start, ref end, amount, out result);
return result;
}
///
/// Returns a color containing the smallest components of the specified colorss.
///
/// The first source color.
/// The second source color.
/// When the method completes, contains an new color composed of the largest components of the source colorss.
public static void Max(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
{
result.A = (left.A > right.A) ? left.A : right.A;
result.R = (left.R > right.R) ? left.R : right.R;
result.G = (left.G > right.G) ? left.G : right.G;
result.B = (left.B > right.B) ? left.B : right.B;
}
///
/// Returns a color containing the largest components of the specified colorss.
///
/// The first source color.
/// The second source color.
/// A color containing the largest components of the source colors.
public static ColorBGRA Max(ColorBGRA left, ColorBGRA right)
{
ColorBGRA result;
Max(ref left, ref right, out result);
return result;
}
///
/// Returns a color containing the smallest components of the specified colors.
///
/// The first source color.
/// The second source color.
/// When the method completes, contains an new color composed of the smallest components of the source colors.
public static void Min(ref ColorBGRA left, ref ColorBGRA right, out ColorBGRA result)
{
result.A = (left.A < right.A) ? left.A : right.A;
result.R = (left.R < right.R) ? left.R : right.R;
result.G = (left.G < right.G) ? left.G : right.G;
result.B = (left.B < right.B) ? left.B : right.B;
}
///
/// Returns a color containing the smallest components of the specified colors.
///
/// The first source color.
/// The second source color.
/// A color containing the smallest components of the source colors.
public static ColorBGRA Min(ColorBGRA left, ColorBGRA right)
{
ColorBGRA result;
Min(ref left, ref right, out result);
return result;
}
///
/// Adjusts the contrast of a color.
///
/// The color whose contrast is to be adjusted.
/// The amount by which to adjust the contrast.
/// When the method completes, contains the adjusted color.
public static void AdjustContrast(ref ColorBGRA value, float contrast, out ColorBGRA result)
{
result.A = value.A;
result.R = ToByte(0.5f + contrast * (value.R / 255.0f - 0.5f));
result.G = ToByte(0.5f + contrast * (value.G / 255.0f - 0.5f));
result.B = ToByte(0.5f + contrast * (value.B / 255.0f - 0.5f));
}
///
/// Adjusts the contrast of a color.
///
/// The color whose contrast is to be adjusted.
/// The amount by which to adjust the contrast.
/// The adjusted color.
public static ColorBGRA AdjustContrast(ColorBGRA value, float contrast)
{
return new ColorBGRA(
ToByte(0.5f + contrast * (value.R / 255.0f - 0.5f)),
ToByte(0.5f + contrast * (value.G / 255.0f - 0.5f)),
ToByte(0.5f + contrast * (value.B / 255.0f - 0.5f)),
value.A);
}
///
/// Adjusts the saturation of a color.
///
/// The color whose saturation is to be adjusted.
/// The amount by which to adjust the saturation.
/// When the method completes, contains the adjusted color.
public static void AdjustSaturation(ref ColorBGRA value, float saturation, out ColorBGRA result)
{
float grey = value.R / 255.0f * 0.2125f + value.G / 255.0f * 0.7154f + value.B / 255.0f * 0.0721f;
result.A = value.A;
result.R = ToByte(grey + saturation * (value.R / 255.0f - grey));
result.G = ToByte(grey + saturation * (value.G / 255.0f - grey));
result.B = ToByte(grey + saturation * (value.B / 255.0f - grey));
}
///
/// Adjusts the saturation of a color.
///
/// The color whose saturation is to be adjusted.
/// The amount by which to adjust the saturation.
/// The adjusted color.
public static ColorBGRA AdjustSaturation(ColorBGRA value, float saturation)
{
float grey = value.R / 255.0f * 0.2125f + value.G / 255.0f * 0.7154f + value.B / 255.0f * 0.0721f;
return new ColorBGRA(
ToByte(grey + saturation * (value.R / 255.0f - grey)),
ToByte(grey + saturation * (value.G / 255.0f - grey)),
ToByte(grey + saturation * (value.B / 255.0f - grey)),
value.A);
}
///
/// Adds two colors.
///
/// The first color to add.
/// The second color to add.
/// The sum of the two colors.
public static ColorBGRA operator +(ColorBGRA left, ColorBGRA right)
{
return new ColorBGRA((byte)(left.R + right.R), (byte)(left.G + right.G), (byte)(left.B + right.B), (byte)(left.A + right.A));
}
///
/// Assert a color (return it unchanged).
///
/// The color to assert (unchange).
/// The asserted (unchanged) color.
public static ColorBGRA operator +(ColorBGRA value)
{
return value;
}
///
/// Subtracts two colors.
///
/// The first color to subtract.
/// The second color to subtract.
/// The difference of the two colors.
public static ColorBGRA operator -(ColorBGRA left, ColorBGRA right)
{
return new ColorBGRA((byte)(left.R - right.R), (byte)(left.G - right.G), (byte)(left.B - right.B), (byte)(left.A - right.A));
}
///
/// Negates a color.
///
/// The color to negate.
/// A negated color.
public static ColorBGRA operator -(ColorBGRA value)
{
return new ColorBGRA(-value.R, -value.G, -value.B, -value.A);
}
///
/// Scales a color.
///
/// The factor by which to scale the color.
/// The color to scale.
/// The scaled color.
public static ColorBGRA operator *(float scale, ColorBGRA value)
{
return new ColorBGRA((byte)(value.R * scale), (byte)(value.G * scale), (byte)(value.B * scale), (byte)(value.A * scale));
}
///
/// Scales a color.
///
/// The factor by which to scale the color.
/// The color to scale.
/// The scaled color.
public static ColorBGRA operator *(ColorBGRA value, float scale)
{
return new ColorBGRA((byte)(value.R * scale), (byte)(value.G * scale), (byte)(value.B * scale), (byte)(value.A * scale));
}
///
/// Modulates two colors.
///
/// The first color to modulate.
/// The second color to modulate.
/// The modulated color.
public static ColorBGRA operator *(ColorBGRA left, ColorBGRA right)
{
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));
}
///
/// Tests for equality between two objects.
///
/// The first value to compare.
/// The second value to compare.
/// true if has the same value as ; otherwise, false.
public static bool operator ==(ColorBGRA left, ColorBGRA right)
{
return left.Equals(right);
}
///
/// Tests for inequality between two objects.
///
/// The first value to compare.
/// The second value to compare.
/// true if has a different value than ; otherwise, false.
public static bool operator !=(ColorBGRA left, ColorBGRA right)
{
return !left.Equals(right);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Color3(ColorBGRA value)
{
return new Color3(value.R, value.G, value.B);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vec3(ColorBGRA value)
{
return new Vec3(value.R / 255.0f, value.G / 255.0f, value.B / 255.0f);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vec4(ColorBGRA value)
{
return new Vec4(value.R / 255.0f, value.G / 255.0f, value.B / 255.0f, value.A / 255.0f);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Color4(ColorBGRA value)
{
return new Color4(value.R / 255.0f, value.G / 255.0f, value.B / 255.0f, value.A / 255.0f);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator ColorBGRA(Vec3 value)
{
return new ColorBGRA(value.X / 255.0f, value.Y / 255.0f, value.Z / 255.0f, 1.0f);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator ColorBGRA(Color3 value)
{
return new ColorBGRA(value.R, value.G, value.B, 1.0f);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator ColorBGRA(Vec4 value)
{
return new ColorBGRA(value.X, value.Y, value.Z, value.W);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator ColorBGRA(Color4 value)
{
return new ColorBGRA(value.R, value.G, value.B, value.A);
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator ColorBGRA(Color value)
{
return new ColorBGRA(value.R, value.G, value.B, value.A);
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator Color(ColorBGRA value)
{
return new Color(value.R, value.G, value.B, value.A);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
///
/// The result of the conversion.
///
public static explicit operator int(ColorBGRA value)
{
return value.ToBgra();
}
///
/// Performs an explicit conversion from to .
///
/// The value.
///
/// The result of the conversion.
///
public static explicit operator ColorBGRA(int value)
{
return new ColorBGRA(value);
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return ToString(CultureInfo.CurrentCulture);
}
///
/// Returns a that represents this instance.
///
/// The format to apply to each channel (byte).
///
/// A that represents this instance.
///
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
///
/// Returns a that represents this instance.
///
/// The format provider.
///
/// A that represents this instance.
///
public string ToString(IFormatProvider formatProvider)
{
return string.Format(formatProvider, ToStringFormat, A, R, G, B);
}
///
/// Returns a that represents this instance.
///
/// The format to apply to each channel (byte).
/// The format provider.
///
/// A that represents this instance.
///
public string ToString(string format, IFormatProvider formatProvider)
{
if (format == null)
return ToString(formatProvider);
return string.Format(formatProvider, ToStringFormat,
A.ToString(format, formatProvider),
R.ToString(format, formatProvider),
G.ToString(format, formatProvider),
B.ToString(format, formatProvider));
}
///
/// Returns a hash code for this instance.
///
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
///
public override int GetHashCode()
{
return A.GetHashCode() + R.GetHashCode() + G.GetHashCode() + B.GetHashCode();
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public bool Equals(ColorBGRA other)
{
return R == other.R && G == other.G && B == other.B && A == other.A;
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public override bool Equals(object value)
{
if (value == null)
return false;
if (!ReferenceEquals(value.GetType(), typeof(ColorBGRA)))
return false;
return Equals((ColorBGRA)value);
}
private static byte ToByte(float component)
{
var value = (int)(component * 255.0f);
return (byte)(value < 0 ? 0 : value > 255 ? 255 : value);
}
}
}