diff --git a/Source/Engine/Core/Math/Color.cpp b/Source/Engine/Core/Math/Color.cpp index 168db71a5..5a00a52e5 100644 --- a/Source/Engine/Core/Math/Color.cpp +++ b/Source/Engine/Core/Math/Color.cpp @@ -137,6 +137,26 @@ String Color::ToHexString() const return String(result, 6); } +bool Color::IsTransparent() const +{ + return Math::IsZero(R + G + B + A); +} + +bool Color::HasOpacity() const +{ + return !Math::IsOne(A); +} + +bool Color::NearEqual(const Color& a, const Color& b) +{ + return Math::NearEqual(a.R, b.R) && Math::NearEqual(a.G, b.G) && Math::NearEqual(a.B, b.B) && Math::NearEqual(a.A, b.A); +} + +bool Color::NearEqual(const Color& a, const Color& b, float epsilon) +{ + return Math::NearEqual(a.R, b.R, epsilon) && Math::NearEqual(a.G, b.G, epsilon) && Math::NearEqual(a.B, b.B, epsilon) && Math::NearEqual(a.A, b.A, epsilon); +} + Vector3 Color::ToVector3() const { return Vector3(R, G, B); @@ -158,6 +178,21 @@ Vector3 Color::ToHSV() const return Vector3(hue, saturation, value); } +void Color::Lerp(const Color& start, const Color& end, float amount, Color& result) +{ + result.R = Math::Lerp(start.R, end.R, amount); + result.G = Math::Lerp(start.G, end.G, amount); + result.B = Math::Lerp(start.B, end.B, amount); + result.A = Math::Lerp(start.A, end.A, amount); +} + +Color Color::Lerp(const Color& start, const Color& end, float amount) +{ + Color result; + Lerp(start, end, amount, result); + return result; +} + Color Color::LinearToSrgb(const Color& linear) { #define LINEAR_TO_SRGB(value) value < 0.00313067f ? value * 12.92f : Math::Pow(value, (1.0f / 2.4f)) * 1.055f - 0.055f diff --git a/Source/Engine/Core/Math/Color.cs b/Source/Engine/Core/Math/Color.cs index 5aee4c428..a98f35126 100644 --- a/Source/Engine/Core/Math/Color.cs +++ b/Source/Engine/Core/Math/Color.cs @@ -716,7 +716,7 @@ namespace FlaxEngine } /// - /// Converts the color to HSV color space (returned as vector). + /// Gets Hue[0-360], Saturation[0-1] and Value[0-1] from RGB color. /// /// The HSV color. public Vector3 ToHSV() @@ -743,51 +743,6 @@ namespace FlaxEngine return new Vector3(hue, saturation, value); } - /// - /// Convert color from the RGB color space to HSV color space. - /// - /// Color of the RGB. - /// The output Hue. - /// The output Saturation. - /// The output Value. - public static void RGBToHSV(Color rgbColor, out float h, out float s, out float v) - { - if ((rgbColor.B > rgbColor.G) && (rgbColor.B > rgbColor.R)) - RGBToHSVHelper(4f, rgbColor.B, rgbColor.R, rgbColor.G, out h, out s, out v); - else if (rgbColor.G <= rgbColor.R) - RGBToHSVHelper(0f, rgbColor.R, rgbColor.G, rgbColor.B, out h, out s, out v); - else - RGBToHSVHelper(2f, rgbColor.G, rgbColor.B, rgbColor.R, out h, out s, out v); - } - - private static void RGBToHSVHelper(float offset, float dominantcolor, float colorone, float colortwo, out float h, out float s, out float v) - { - v = dominantcolor; - if (Mathf.IsZero(v)) - { - s = 0f; - h = 0f; - } - else - { - var single = colorone <= colortwo ? colorone : colortwo; - float vv = v - single; - if (Mathf.IsZero(vv)) - { - s = 0f; - h = offset + (colorone - colortwo); - } - else - { - s = vv / v; - h = offset + (colorone - colortwo) / vv; - } - h = h / 6f; - if (h < 0f) - h = h + 1f; - } - } - /// /// Adjusts the contrast of a color. /// diff --git a/Source/Engine/Core/Math/Color.h b/Source/Engine/Core/Math/Color.h index d11097d5c..3eee6d9f8 100644 --- a/Source/Engine/Core/Math/Color.h +++ b/Source/Engine/Core/Math/Color.h @@ -166,7 +166,7 @@ public: /// Creates RGB color from Hue[0-360], Saturation[0-1] and Value[0-1] packed to XYZ vector. /// /// The HSV color. - /// The alpha value. Default is 1. + /// The alpha value. Default is 1. /// The RGB color. static Color FromHSV(const Vector3& hsv, float alpha = 1.0f); @@ -264,28 +264,15 @@ public: } // Returns true if color is fully transparent (all components are equal zero). - bool IsTransparent() const - { - return Math::IsZero(R + G + B + A); - } + bool IsTransparent() const; // Returns true if color has opacity channel in use (different from 1). - bool HasOpacity() const - { - return !Math::IsOne(A); - } + bool HasOpacity() const; public: - static bool NearEqual(const Color& a, const Color& b) - { - return Math::NearEqual(a.R, b.R) && Math::NearEqual(a.G, b.G) & Math::NearEqual(a.B, b.B) && Math::NearEqual(a.A, b.A); - } - - static bool NearEqual(const Color& a, const Color& b, float epsilon) - { - return Math::NearEqual(a.R, b.R, epsilon) && Math::NearEqual(a.G, b.G, epsilon) & Math::NearEqual(a.B, b.B, epsilon) && Math::NearEqual(a.A, b.A, epsilon); - } + static bool NearEqual(const Color& a, const Color& b); + static bool NearEqual(const Color& a, const Color& b, float epsilon); public: @@ -296,7 +283,7 @@ public: Vector4 ToVector4() const; /// - /// Gets Hue[0-1], Saturation[0-1] and Value[0-360] from RGB color. + /// Gets Hue[0-360], Saturation[0-1] and Value[0-1] from RGB color. /// /// HSV color Vector3 ToHSV() const; @@ -308,13 +295,7 @@ public: /// The end color. /// The value between 0 and 1 indicating the weight of interpolation. /// When the method completes, contains the linear interpolation of the two colors. - static void Lerp(const Color& start, const Color& end, float amount, Color& result) - { - result.R = Math::Lerp(start.R, end.R, amount); - result.G = Math::Lerp(start.G, end.G, amount); - result.B = Math::Lerp(start.B, end.B, amount); - result.A = Math::Lerp(start.A, end.A, amount); - } + static void Lerp(const Color& start, const Color& end, float amount, Color& result); /// /// Performs a linear interpolation between two colors. @@ -323,12 +304,7 @@ public: /// The end color. /// The value between 0 and 1 indicating the weight of interpolation. /// The linear interpolation of the two colors. - static Color Lerp(const Color& start, const Color& end, float amount) - { - Color result; - Lerp(start, end, amount, result); - return result; - } + static Color Lerp(const Color& start, const Color& end, float amount); // Converts a [0.0, 1.0] linear value into a [0.0, 1.0] sRGB value. static Color LinearToSrgb(const Color& linear);