// 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. // // ----------------------------------------------------------------------------- // Original code from SlimMath project. http://code.google.com/p/slimmath/ // Greetings to SlimDX Group. Original code published with the following license: // ----------------------------------------------------------------------------- /* * Copyright (c) 2007-2011 SlimDX Group * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using System; using System.Globalization; using System.Runtime.InteropServices; using System.Runtime.Serialization; namespace math { /// /// Represents a color in the form of rgba. /// [DataContract( Name = "Color4" )] [DataStyle( DataStyle.Compact )] [StructLayout( LayoutKind.Sequential, Pack = 4 )] public struct Color4 : IEquatable, IFormattable { private const string ToStringFormat = "A:{0} R:{1} G:{2} B:{3}"; /// /// The Black color (0, 0, 0, 1). /// public static readonly Color4 Black = new Color4( 0.0f, 0.0f, 0.0f, 1.0f ); /// /// The White color (1, 1, 1, 1). /// public static readonly Color4 White = new Color4( 1.0f, 1.0f, 1.0f, 1.0f ); /// /// The red component of the color. /// [DataMember( Order = 0 )] public float R; /// /// The green component of the color. /// [DataMember( Order = 1 )] public float G; /// /// The blue component of the color. /// [DataMember( Order = 2 )] public float B; /// /// The alpha component of the color. /// [DataMember( Order = 3 )] public float A; /// /// Initializes a new instance of the struct. /// /// The value that will be assigned to all components. public Color4( float value ) { A = R = G = B = 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 Color4( float red, float green, float blue, float alpha ) { R = red; G = green; B = blue; A = alpha; } /// /// Initializes a new instance of the struct. /// /// The red, green, blue, and alpha components of the color. public Color4( Vec4 value ) { R = value.X; G = value.Y; B = value.Z; A = 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 Color4( Vec3 value, float alpha ) { R = value.X; G = value.Y; B = value.Z; A = alpha; } /// /// Initializes a new instance of the struct. /// /// A packed integer containing all four color components in RGBA order. public Color4( uint rgba ) { A = ( ( rgba >> 24 ) & 255 ) / 255.0f; B = ( ( rgba >> 16 ) & 255 ) / 255.0f; G = ( ( rgba >> 8 ) & 255 ) / 255.0f; R = ( rgba & 255 ) / 255.0f; } /// /// Initializes a new instance of the struct. /// /// A packed integer containing all four color components in RGBA order. public Color4( int rgba ) { A = ( ( rgba >> 24 ) & 255 ) / 255.0f; B = ( ( rgba >> 16 ) & 255 ) / 255.0f; G = ( ( rgba >> 8 ) & 255 ) / 255.0f; R = ( rgba & 255 ) / 255.0f; } /// /// Initializes a new instance of the struct. /// /// The values to assign to the red, green, blue, and 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 Color4( 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 Color4." ); R = values[0]; G = values[1]; B = values[2]; A = values[3]; } /// /// Initializes a new instance of the struct. /// /// used to initialize the color. public Color4( Color3 color ) { R = color.R; G = color.G; B = color.B; A = 1.0f; } /// /// Initializes a new instance of the struct. /// /// used to initialize the color. public Color4( Color color ) { R = color.R / 255.0f; G = color.G / 255.0f; B = color.B / 255.0f; A = color.A / 255.0f; } /// /// Initializes a new instance of the struct. /// /// used to initialize the color. public Color4( ColorBGRA color ) { R = color.R / 255.0f; G = color.G / 255.0f; B = color.B / 255.0f; A = color.A / 255.0f; } /// /// Initializes a new instance of the struct. /// /// used to initialize the color. /// The alpha component of the color. public Color4( Color3 color, float alpha ) { R = color.R; G = color.G; B = color.B; A = alpha; } /// /// Gets or sets the component at the specified index. /// /// The value of the red, green, blue, and alpha components, 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 float this[int index] { get { switch( index ) { case 0: return R; case 1: return G; case 2: return B; case 3: return A; } throw new ArgumentOutOfRangeException( nameof( index ), "Indices for Color4 run from 0 to 3, inclusive." ); } set { switch( index ) { case 0: R = value; break; case 1: G = value; break; case 2: B = value; break; case 3: A = value; break; default: throw new ArgumentOutOfRangeException( nameof( index ), "Indices for Color4 run from 0 to 3, inclusive." ); } } } /// /// Converts the color into a packed integer. /// /// A packed integer containing all four color components. public int ToBgra() { uint a = (uint)( A * 255.0f ) & 255; uint r = (uint)( R * 255.0f ) & 255; uint g = (uint)( G * 255.0f ) & 255; uint b = (uint)( B * 255.0f ) & 255; uint value = b; value |= g << 8; value |= r << 16; value |= a << 24; return (int)value; } /// /// Converts the color into a packed integer. /// public void ToBgra( out byte r, out byte g, out byte b, out byte a ) { a = (byte)( A * 255.0f ); r = (byte)( R * 255.0f ); g = (byte)( G * 255.0f ); b = (byte)( B * 255.0f ); } /// /// Converts the color into a packed integer. /// /// A packed integer containing all four color components. public int ToRgba() { uint a = (uint)( A * 255.0f ) & 255; uint r = (uint)( R * 255.0f ) & 255; uint g = (uint)( G * 255.0f ) & 255; uint b = (uint)( B * 255.0f ) & 255; uint value = r; value |= g << 8; value |= b << 16; value |= a << 24; return (int)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, G, B ); } /// /// Converts the color into a four component vector. /// /// A four component vector containing all four color components. public Vec4 ToVector4() { return new Vec4( R, G, B, A ); } /// /// Creates an array containing the elements of the color. /// /// A four-element array containing the components of the color. public float[] ToArray() { return new[] { R, G, B, A }; } /// /// Converts this color from linear space to sRGB space. /// /// A color3 in sRGB space. public Color4 ToSRgb() { return new Color4( MathUtil.LinearToSRgb( R ), MathUtil.LinearToSRgb( G ), MathUtil.LinearToSRgb( B ), A ); } /// /// Converts this color from sRGB space to linear space. /// /// A color4 in linear space. public Color4 ToLinear() { return new Color4( MathUtil.SRgbToLinear( R ), MathUtil.SRgbToLinear( G ), MathUtil.SRgbToLinear( B ), A ); } /// /// 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 Color4 left, ref Color4 right, out Color4 result ) { result.A = left.A + right.A; result.R = left.R + right.R; result.G = left.G + right.G; result.B = 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 Color4 Add( Color4 left, Color4 right ) { return new Color4( left.R + right.R, left.G + right.G, left.B + right.B, 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 Color4 left, ref Color4 right, out Color4 result ) { result.A = left.A - right.A; result.R = left.R - right.R; result.G = left.G - right.G; result.B = 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 Color4 Subtract( Color4 left, Color4 right ) { return new Color4( left.R - right.R, left.G - right.G, left.B - right.B, 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 Color4 left, ref Color4 right, out Color4 result ) { result.A = left.A * right.A; result.R = left.R * right.R; result.G = left.G * right.G; result.B = left.B * right.B; } /// /// Modulates two colors. /// /// The first color to modulate. /// The second color to modulate. /// The modulated color. public static Color4 Modulate( Color4 left, Color4 right ) { return new Color4( left.R * right.R, left.G * right.G, left.B * right.B, left.A * right.A ); } /// /// 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 Color4 value, float scale, out Color4 result ) { result.A = value.A * scale; result.R = value.R * scale; result.G = value.G * scale; result.B = value.B * scale; } /// /// Scales a color. /// /// The color to scale. /// The amount by which to scale. /// The scaled color. public static Color4 Scale( Color4 value, float scale ) { return new Color4( value.R * scale, value.G * scale, value.B * scale, value.A * scale ); } /// /// Negates a color. /// /// The color to negate. /// When the method completes, contains the negated color. public static void Negate( ref Color4 value, out Color4 result ) { result.A = 1.0f - value.A; result.R = 1.0f - value.R; result.G = 1.0f - value.G; result.B = 1.0f - value.B; } /// /// Negates a color. /// /// The color to negate. /// The negated color. public static Color4 Negate( Color4 value ) { return new Color4( 1.0f - value.R, 1.0f - value.G, 1.0f - value.B, 1.0f - 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 Color4 value, ref Color4 min, ref Color4 max, out Color4 result ) { float alpha = value.A; alpha = ( alpha > max.A ) ? max.A : alpha; alpha = ( alpha < min.A ) ? min.A : alpha; float red = value.R; red = ( red > max.R ) ? max.R : red; red = ( red < min.R ) ? min.R : red; float green = value.G; green = ( green > max.G ) ? max.G : green; green = ( green < min.G ) ? min.G : green; float blue = value.B; blue = ( blue > max.B ) ? max.B : blue; blue = ( blue < min.B ) ? min.B : blue; result = new Color4( 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 Color4 Clamp( Color4 value, Color4 min, Color4 max ) { Color4 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 Color4 start, ref Color4 end, float amount, out Color4 result ) { result.R = MathUtil.Lerp( start.R, end.R, amount ); result.G = MathUtil.Lerp( start.G, end.G, amount ); result.B = MathUtil.Lerp( start.B, end.B, 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 Color4 Lerp( Color4 start, Color4 end, float amount ) { Color4 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 Color4 start, ref Color4 end, float amount, out Color4 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 Color4 SmoothStep( Color4 start, Color4 end, float amount ) { Color4 result; SmoothStep( ref start, ref end, amount, 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 largest components of the source colors. public static void Max( ref Color4 left, ref Color4 right, out Color4 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 colors. /// /// The first source color. /// The second source color. /// A color containing the largest components of the source colors. public static Color4 Max( Color4 left, Color4 right ) { Color4 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 Color4 left, ref Color4 right, out Color4 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 Color4 Min( Color4 left, Color4 right ) { Color4 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 Color4 value, float contrast, out Color4 result ) { result.A = value.A; result.R = 0.5f + contrast * ( value.R - 0.5f ); result.G = 0.5f + contrast * ( value.G - 0.5f ); result.B = 0.5f + contrast * ( value.B - 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 Color4 AdjustContrast( Color4 value, float contrast ) { return new Color4( 0.5f + contrast * ( value.R - 0.5f ), 0.5f + contrast * ( value.G - 0.5f ), 0.5f + contrast * ( value.B - 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 Color4 value, float saturation, out Color4 result ) { float grey = value.R * 0.2125f + value.G * 0.7154f + value.B * 0.0721f; result.A = value.A; result.R = grey + saturation * ( value.R - grey ); result.G = grey + saturation * ( value.G - grey ); result.B = grey + saturation * ( value.B - 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 Color4 AdjustSaturation( Color4 value, float saturation ) { float grey = value.R * 0.2125f + value.G * 0.7154f + value.B * 0.0721f; return new Color4( grey + saturation * ( value.R - grey ), grey + saturation * ( value.G - grey ), grey + saturation * ( value.B - grey ), value.A ); } /// /// Premultiplies the color components by the alpha value. /// /// The color to premultiply. /// A color with premultiplied alpha. public static Color4 PremultiplyAlpha( Color4 value ) { return new Color4( value.R * value.A, value.G * value.A, value.B * value.A, value.A ); } /// /// Adds two colors. /// /// The first color to add. /// The second color to add. /// The sum of the two colors. public static Color4 operator +( Color4 left, Color4 right ) { return new Color4( left.R + right.R, left.G + right.G, left.B + right.B, left.A + right.A ); } /// /// Assert a color (return it unchanged). /// /// The color to assert (unchanged). /// The asserted (unchanged) color. public static Color4 operator +( Color4 value ) { return value; } /// /// Subtracts two colors. /// /// The first color to subtract. /// The second color to subtract. /// The difference of the two colors. public static Color4 operator -( Color4 left, Color4 right ) { return new Color4( left.R - right.R, left.G - right.G, left.B - right.B, left.A - right.A ); } /// /// Negates a color. /// /// The color to negate. /// A negated color. public static Color4 operator -( Color4 value ) { return new Color4( -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 Color4 operator *( float scale, Color4 value ) { return new Color4( value.R * scale, value.G * scale, value.B * scale, value.A * scale ); } /// /// Scales a color. /// /// The factor by which to scale the color. /// The color to scale. /// The scaled color. public static Color4 operator *( Color4 value, float scale ) { return new Color4( value.R * scale, value.G * scale, value.B * scale, value.A * scale ); } /// /// Modulates two colors. /// /// The first color to modulate. /// The second color to modulate. /// The modulated color. public static Color4 operator *( Color4 left, Color4 right ) { return new Color4( left.R * right.R, left.G * right.G, left.B * right.B, left.A * right.A ); } /// /// 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 ==( Color4 left, Color4 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 !=( Color4 left, Color4 right ) { return !left.Equals( right ); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Color3( Color4 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( Color4 value ) { return new Vec3( value.R, value.G, value.B ); } /// /// Performs an implicit conversion from to . /// /// The value. /// The result of the conversion. public static implicit operator Vec4( Color4 value ) { return new Vec4( 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 Color4( Vec3 value ) { return new Color4( value.X, value.Y, value.Z, 1.0f ); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Color4( Vec4 value ) { return new Color4( 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 Color4( ColorBGRA value ) { return new Color4( value ); } /// /// 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 explicit conversion from to . /// /// The value. /// /// The result of the conversion. /// public static explicit operator int( Color4 value ) { return value.ToRgba(); } /// /// Performs an explicit conversion from to . /// /// The value. /// /// The result of the conversion. /// public static explicit operator Color4( int value ) { return new Color4( value ); } /// /// Converts this color to an equivalent , discarding the alpha channel. /// /// An equivalent , discarding the alpha channel. public Color3 ToColor3() { return new Color3( R, G, B ); } /// /// 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 (float). /// /// 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 (float). /// 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( Color4 other ) { return A == other.A && R == other.R && G == other.G && B == other.B; } /// /// 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( Color4 ) ) ) return false; return Equals( (Color4)value ); } } }