mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-14 15:33:26 +00:00
fixed style
This commit is contained in:
@@ -12,9 +12,9 @@ namespace omath
|
|||||||
{
|
{
|
||||||
struct HSV
|
struct HSV
|
||||||
{
|
{
|
||||||
float m_hue{};
|
float hue{};
|
||||||
float m_saturation{};
|
float saturation{};
|
||||||
float m_value{};
|
float value{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -26,18 +26,15 @@ namespace omath
|
|||||||
Clamp(0.f, 1.f);
|
Clamp(0.f, 1.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr explicit Color() : Vector4()
|
constexpr explicit Color() = default;
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr static Color FromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
|
constexpr static Color FromRGBA(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a)
|
||||||
{
|
{
|
||||||
return Color{Vector4(r, g, b, a) / 255.f};
|
return Color{Vector4(r, g, b, a) / 255.f};
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr static Color FromHSV(float hue, float saturation, float value)
|
constexpr static Color FromHSV(float hue, const float saturation, const float value)
|
||||||
{
|
{
|
||||||
float r{}, g{}, b{};
|
float r{}, g{}, b{};
|
||||||
|
|
||||||
@@ -51,19 +48,38 @@ namespace omath
|
|||||||
|
|
||||||
switch (i % 6)
|
switch (i % 6)
|
||||||
{
|
{
|
||||||
case 0: r = value, g = t, b = p; break;
|
case 0:
|
||||||
case 1: r = q, g = value, b = p; break;
|
r = value, g = t, b = p;
|
||||||
case 2: r = p, g = value, b = t; break;
|
break;
|
||||||
case 3: r = p, g = q, b = value; break;
|
case 1:
|
||||||
case 4: r = t, g = p, b = value; break;
|
r = q, g = value, b = p;
|
||||||
case 5: r = value, g = p, b = q; break;
|
break;
|
||||||
|
case 2:
|
||||||
|
r = p, g = value, b = t;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
r = p, g = q, b = value;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
r = t, g = p, b = value;
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
r = value, g = p, b = q;
|
||||||
|
break;
|
||||||
|
|
||||||
default: return {0.f, 0.f, 0.f, 0.f};
|
default:
|
||||||
|
return {0.f, 0.f, 0.f, 0.f};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {r, g, b, 1.f};
|
return {r, g, b, 1.f};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr static Color FromHSV(const HSV& hsv)
|
||||||
|
{
|
||||||
|
return FromHSV(hsv.hue, hsv.saturation, hsv.value);
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr HSV ToHSV() const
|
constexpr HSV ToHSV() const
|
||||||
{
|
{
|
||||||
@@ -79,21 +95,21 @@ namespace omath
|
|||||||
|
|
||||||
|
|
||||||
if (delta == 0.f)
|
if (delta == 0.f)
|
||||||
hsvData.m_hue = 0.f;
|
hsvData.hue = 0.f;
|
||||||
|
|
||||||
else if (max == red)
|
else if (max == red)
|
||||||
hsvData.m_hue = 60.f * (std::fmodf(((green - blue) / delta), 6.f));
|
hsvData.hue = 60.f * (std::fmodf(((green - blue) / delta), 6.f));
|
||||||
else if (max == green)
|
else if (max == green)
|
||||||
hsvData.m_hue = 60.f * (((blue - red) / delta) + 2.f);
|
hsvData.hue = 60.f * (((blue - red) / delta) + 2.f);
|
||||||
else if (max == blue)
|
else if (max == blue)
|
||||||
hsvData.m_hue = 60.f * (((red - green) / delta) + 4.f);
|
hsvData.hue = 60.f * (((red - green) / delta) + 4.f);
|
||||||
|
|
||||||
if (hsvData.m_hue < 0.f)
|
if (hsvData.hue < 0.f)
|
||||||
hsvData.m_hue += 360.f;
|
hsvData.hue += 360.f;
|
||||||
|
|
||||||
hsvData.m_hue /= 360.f;
|
hsvData.hue /= 360.f;
|
||||||
hsvData.m_saturation = max == 0.f ? 0.f : delta / max;
|
hsvData.saturation = max == 0.f ? 0.f : delta / max;
|
||||||
hsvData.m_value = max;
|
hsvData.value = max;
|
||||||
|
|
||||||
return hsvData;
|
return hsvData;
|
||||||
}
|
}
|
||||||
@@ -106,17 +122,21 @@ namespace omath
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr Color Blend(const Color& other, float ratio) const
|
constexpr Color Blend(const Color& other, float ratio) const
|
||||||
{
|
{
|
||||||
return Color( (*this * (1.f - ratio)) + (other * ratio) );
|
ratio = std::clamp(ratio, 0.f, 1.f);
|
||||||
|
return Color(*this * (1.f - ratio) + other * ratio);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] static constexpr Color Red() {return {1.f, 0.f, 0.f, 1.f};}
|
[[nodiscard]] static constexpr Color Red()
|
||||||
[[nodiscard]] static constexpr Color Green() {return {0.f, 1.f, 0.f, 1.f};}
|
|
||||||
[[nodiscard]] static constexpr Color Blue() {return {0.f, 0.f, 1.f, 1.f};}
|
|
||||||
};
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
constexpr Color Blend(const Color& first, const Color& second, float ratio)
|
|
||||||
{
|
{
|
||||||
return Color{first * (1.f - std::clamp(ratio, 0.f, 1.f)) + second * ratio};
|
return {1.f, 0.f, 0.f, 1.f};
|
||||||
}
|
}
|
||||||
|
[[nodiscard]] static constexpr Color Green()
|
||||||
|
{
|
||||||
|
return {0.f, 1.f, 0.f, 1.f};
|
||||||
}
|
}
|
||||||
|
[[nodiscard]] static constexpr Color Blue()
|
||||||
|
{
|
||||||
|
return {0.f, 0.f, 1.f, 1.f};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace omath
|
||||||
|
|||||||
@@ -63,9 +63,9 @@ TEST_F(UnitTestColor, FromHSV)
|
|||||||
TEST_F(UnitTestColor, ToHSV)
|
TEST_F(UnitTestColor, ToHSV)
|
||||||
{
|
{
|
||||||
HSV hsv = color1.ToHSV(); // Red color
|
HSV hsv = color1.ToHSV(); // Red color
|
||||||
EXPECT_FLOAT_EQ(hsv.m_hue, 0.0f);
|
EXPECT_FLOAT_EQ(hsv.hue, 0.0f);
|
||||||
EXPECT_FLOAT_EQ(hsv.m_saturation, 1.0f);
|
EXPECT_FLOAT_EQ(hsv.saturation, 1.0f);
|
||||||
EXPECT_FLOAT_EQ(hsv.m_value, 1.0f);
|
EXPECT_FLOAT_EQ(hsv.value, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test color blending
|
// Test color blending
|
||||||
@@ -106,7 +106,7 @@ TEST_F(UnitTestColor, BlendVector3)
|
|||||||
{
|
{
|
||||||
constexpr Color v1(1.0f, 0.0f, 0.0f, 1.f); // Red
|
constexpr Color v1(1.0f, 0.0f, 0.0f, 1.f); // Red
|
||||||
constexpr Color v2(0.0f, 1.0f, 0.0f, 1.f); // Green
|
constexpr Color v2(0.0f, 1.0f, 0.0f, 1.f); // Green
|
||||||
constexpr Color blended = Blend(v1, v2, 0.5f);
|
constexpr Color blended = v1.Blend(v2, 0.5f);
|
||||||
EXPECT_FLOAT_EQ(blended.x, 0.5f);
|
EXPECT_FLOAT_EQ(blended.x, 0.5f);
|
||||||
EXPECT_FLOAT_EQ(blended.y, 0.5f);
|
EXPECT_FLOAT_EQ(blended.y, 0.5f);
|
||||||
EXPECT_FLOAT_EQ(blended.z, 0.0f);
|
EXPECT_FLOAT_EQ(blended.z, 0.0f);
|
||||||
|
|||||||
Reference in New Issue
Block a user