diff --git a/headers/enduro2d/utils/color.hpp b/headers/enduro2d/utils/color.hpp index 05db038d..45447ac9 100644 --- a/headers/enduro2d/utils/color.hpp +++ b/headers/enduro2d/utils/color.hpp @@ -17,23 +17,25 @@ namespace e2d f32 b = 1.f; f32 a = 1.f; public: - static const color& clear() noexcept; - static const color& black() noexcept; - static const color& white() noexcept; - static const color& red() noexcept; - static const color& green() noexcept; - static const color& blue() noexcept; - static const color& yellow() noexcept; - static const color& magenta() noexcept; - static const color& cyan() noexcept; + static constexpr color clear() noexcept; + static constexpr color black() noexcept; + static constexpr color white() noexcept; + static constexpr color red() noexcept; + static constexpr color green() noexcept; + static constexpr color blue() noexcept; + static constexpr color yellow() noexcept; + static constexpr color magenta() noexcept; + static constexpr color cyan() noexcept; public: - color() noexcept = default; + constexpr color() noexcept = default; + constexpr color(const color& other) noexcept = default; + constexpr color& operator=(const color& other) noexcept = default; - color(const color& other) noexcept = default; - color& operator=(const color& other) noexcept = default; + constexpr color(f32 r, f32 g, f32 b, f32 a = 1.f) noexcept; - color(f32 r, f32 g, f32 b, f32 a = 1.f) noexcept; explicit color(const color32& other) noexcept; + explicit color(const vec4& rgba) noexcept; + explicit color(const vec3& rgb, f32 a = 1.f) noexcept; f32* data() noexcept; const f32* data() const noexcept; @@ -53,6 +55,51 @@ namespace e2d }; } +namespace e2d +{ + constexpr color color::clear() noexcept { + return {0, 0, 0, 0}; + } + + constexpr color color::black() noexcept { + return {0, 0, 0, 1}; + } + + constexpr color color::white() noexcept { + return {1, 1, 1, 1}; + } + + constexpr color color::red() noexcept { + return {1, 0, 0, 1}; + } + + constexpr color color::green() noexcept { + return {0, 1, 0, 1}; + } + + constexpr color color::blue() noexcept { + return {0, 0, 1, 1}; + } + + constexpr color color::yellow() noexcept { + return {1, 1, 0, 1}; + } + + constexpr color color::magenta() noexcept { + return {1, 0, 1, 1}; + } + + constexpr color color::cyan() noexcept { + return {0, 1, 1, 1}; + } + + constexpr color::color(f32 r, f32 g, f32 b, f32 a) noexcept + : r(r) + , g(g) + , b(b) + , a(a) {} +} + namespace e2d { vec3 make_vec3(const color& c) noexcept; diff --git a/headers/enduro2d/utils/color32.hpp b/headers/enduro2d/utils/color32.hpp index 8055683f..8d091370 100644 --- a/headers/enduro2d/utils/color32.hpp +++ b/headers/enduro2d/utils/color32.hpp @@ -17,23 +17,25 @@ namespace e2d u8 b = 255; u8 a = 255; public: - static const color32& clear() noexcept; - static const color32& black() noexcept; - static const color32& white() noexcept; - static const color32& red() noexcept; - static const color32& green() noexcept; - static const color32& blue() noexcept; - static const color32& yellow() noexcept; - static const color32& magenta() noexcept; - static const color32& cyan() noexcept; + static constexpr color32 clear() noexcept; + static constexpr color32 black() noexcept; + static constexpr color32 white() noexcept; + static constexpr color32 red() noexcept; + static constexpr color32 green() noexcept; + static constexpr color32 blue() noexcept; + static constexpr color32 yellow() noexcept; + static constexpr color32 magenta() noexcept; + static constexpr color32 cyan() noexcept; public: - color32() noexcept = default; + constexpr color32() noexcept = default; + constexpr color32(const color32& other) noexcept = default; + constexpr color32& operator=(const color32& other) noexcept = default; - color32(const color32& other) noexcept = default; - color32& operator=(const color32& other) noexcept = default; + constexpr color32(u8 r, u8 g, u8 b, u8 a = 255) noexcept; - color32(u8 r, u8 g, u8 b, u8 a = 255) noexcept; explicit color32(const color& other) noexcept; + explicit color32(const vec4& rgba) noexcept; + explicit color32(const vec3& rgb, u8 a = 255) noexcept; u8* data() noexcept; const u8* data() const noexcept; @@ -53,6 +55,51 @@ namespace e2d }; } +namespace e2d +{ + constexpr color32 color32::clear() noexcept { + return {0, 0, 0, 0}; + } + + constexpr color32 color32::black() noexcept { + return {0, 0, 0, 255}; + } + + constexpr color32 color32::white() noexcept { + return {255, 255, 255, 255}; + } + + constexpr color32 color32::red() noexcept { + return {255, 0, 0, 255}; + } + + constexpr color32 color32::green() noexcept { + return {0, 255, 0, 255}; + } + + constexpr color32 color32::blue() noexcept { + return {0, 0, 255, 255}; + } + + constexpr color32 color32::yellow() noexcept { + return {255, 255, 0, 255}; + } + + constexpr color32 color32::magenta() noexcept { + return {255, 0, 255, 255}; + } + + constexpr color32 color32::cyan() noexcept { + return {0, 255, 255, 255}; + } + + constexpr color32::color32(u8 r, u8 g, u8 b, u8 a) noexcept + : r(r) + , g(g) + , b(b) + , a(a) {} +} + namespace e2d { vec3 make_vec3(const color32& c) noexcept; diff --git a/sources/enduro2d/high/bindings/utils_binds.cpp b/sources/enduro2d/high/bindings/utils_binds.cpp index 16295a13..48dde059 100644 --- a/sources/enduro2d/high/bindings/utils_binds.cpp +++ b/sources/enduro2d/high/bindings/utils_binds.cpp @@ -6,10 +6,141 @@ #include "bindings.hpp" +namespace +{ + using namespace e2d; + + void bind_color(sol::state& l) { + l.new_usertype("color", + sol::constructors< + color(), + color(color), + color(color32), + color(vec4), + color(vec3, f32), + color(f32,f32,f32,f32)>(), + + "clear", &color::clear, + "black", &color::black, + "white", &color::white, + "red", &color::red, + "green", &color::green, + "blue", &color::blue, + "yellow", &color::yellow, + "magenta", &color::magenta, + "cyan", &color::cyan, + + "r", &color::r, + "g", &color::g, + "b", &color::b, + "a", &color::a, + + sol::meta_function::equal_to, sol::resolve(::operator==), + sol::meta_function::less_than, sol::resolve(::operator<), + + sol::meta_function::addition, sol::overload( + sol::resolve(::operator+), + sol::resolve(::operator+), + sol::resolve(::operator+)), + + sol::meta_function::subtraction, sol::overload( + sol::resolve(::operator-), + sol::resolve(::operator-), + sol::resolve(::operator-)), + + sol::meta_function::multiplication, sol::overload( + sol::resolve(::operator*), + sol::resolve(::operator*), + sol::resolve(::operator*)), + + sol::meta_function::division, sol::overload( + sol::resolve(::operator/), + sol::resolve(::operator/), + sol::resolve(::operator/)), + + "approximately", [](const color& l, const color& r){ return math::approximately(l,r); }, + + "minimum", sol::resolve(&math::minimum), + "maximum", sol::resolve(&math::maximum), + + "minimized", sol::resolve(&math::minimized), + "maximized", sol::resolve(&math::maximized), + "clamped", sol::resolve(&math::clamped), + + "contains_nan", sol::resolve(&math::contains_nan), + + "pack_color", sol::resolve(&colors::pack_color), + "unpack_color", sol::resolve(&colors::unpack_color)); + } + + void bind_color32(sol::state& l) { + l.new_usertype("color32", + sol::constructors< + color32(), + color32(color), + color32(color32), + color32(vec4), + color32(vec3, u8), + color32(u8,u8,u8,u8)>(), + + "clear", &color32::clear, + "black", &color32::black, + "white", &color32::white, + "red", &color32::red, + "green", &color32::green, + "blue", &color32::blue, + "yellow", &color32::yellow, + "magenta", &color32::magenta, + "cyan", &color32::cyan, + + "r", &color32::r, + "g", &color32::g, + "b", &color32::b, + "a", &color32::a, + + sol::meta_function::equal_to, sol::resolve(::operator==), + sol::meta_function::less_than, sol::resolve(::operator<), + + sol::meta_function::addition, sol::overload( + sol::resolve(::operator+), + sol::resolve(::operator+), + sol::resolve(::operator+)), + + sol::meta_function::subtraction, sol::overload( + sol::resolve(::operator-), + sol::resolve(::operator-), + sol::resolve(::operator-)), + + sol::meta_function::multiplication, sol::overload( + sol::resolve(::operator*), + sol::resolve(::operator*), + sol::resolve(::operator*)), + + sol::meta_function::division, sol::overload( + sol::resolve(::operator/), + sol::resolve(::operator/), + sol::resolve(::operator/)), + + "approximately", [](const color32& l, const color32& r){ return math::approximately(l,r); }, + + "minimum", sol::resolve(&math::minimum), + "maximum", sol::resolve(&math::maximum), + + "minimized", sol::resolve(&math::minimized), + "maximized", sol::resolve(&math::maximized), + "clamped", sol::resolve(&math::clamped), + + "contains_nan", sol::resolve(&math::contains_nan), + + "pack_color", sol::resolve(&colors::pack_color32), + "unpack_color", sol::resolve(&colors::unpack_color32)); + } +} + namespace e2d::bindings { void bind_utils(sol::state& l) { - //TODO(BlackMat): implme - E2D_UNUSED(l); + bind_color(l); + bind_color32(l); } } diff --git a/sources/enduro2d/utils/color.cpp b/sources/enduro2d/utils/color.cpp index aae50e81..c9444e5e 100644 --- a/sources/enduro2d/utils/color.cpp +++ b/sources/enduro2d/utils/color.cpp @@ -9,62 +9,23 @@ namespace e2d { - const color& color::clear() noexcept { - static const color c(0, 0, 0, 0); - return c; - } - - const color& color::black() noexcept { - static const color c(0, 0, 0, 1); - return c; - } - - const color& color::white() noexcept { - static const color c(1, 1, 1, 1); - return c; - } - - const color& color::red() noexcept { - static const color c(1, 0, 0, 1); - return c; - } - - const color& color::green() noexcept { - static const color c(0, 1, 0, 1); - return c; - } - - const color& color::blue() noexcept { - static const color c(0, 0, 1, 1); - return c; - } - - const color& color::yellow() noexcept { - static const color c(1, 1, 0, 1); - return c; - } - - const color& color::magenta() noexcept { - static const color c(1, 0, 1, 1); - return c; - } - - const color& color::cyan() noexcept { - static const color c(0, 1, 1, 1); - return c; - } - color::color(const color32& other) noexcept : r(other.r / 255.f) , g(other.g / 255.f) , b(other.b / 255.f) , a(other.a / 255.f) {} - color::color(f32 nr, f32 ng, f32 nb, f32 na) noexcept - : r(nr) - , g(ng) - , b(nb) - , a(na) {} + color::color(const vec4& rgba) noexcept + : r(rgba.x) + , g(rgba.y) + , b(rgba.z) + , a(rgba.w) {} + + color::color(const vec3& rgb, f32 a) noexcept + : r(rgb.x) + , g(rgb.y) + , b(rgb.z) + , a(a) {} f32* color::data() noexcept { return &r; diff --git a/sources/enduro2d/utils/color32.cpp b/sources/enduro2d/utils/color32.cpp index a98e8bee..82e4e4c4 100644 --- a/sources/enduro2d/utils/color32.cpp +++ b/sources/enduro2d/utils/color32.cpp @@ -9,62 +9,23 @@ namespace e2d { - const color32& color32::clear() noexcept { - static const color32 c(0, 0, 0, 0); - return c; - } - - const color32& color32::black() noexcept { - static const color32 c(0, 0, 0, 255); - return c; - } - - const color32& color32::white() noexcept { - static const color32 c(255, 255, 255, 255); - return c; - } - - const color32& color32::red() noexcept { - static const color32 c(255, 0, 0, 255); - return c; - } - - const color32& color32::green() noexcept { - static const color32 c(0, 255, 0, 255); - return c; - } - - const color32& color32::blue() noexcept { - static const color32 c(0, 0, 255, 255); - return c; - } - - const color32& color32::yellow() noexcept { - static const color32 c(255, 255, 0, 255); - return c; - } - - const color32& color32::magenta() noexcept { - static const color32 c(255, 0, 255, 255); - return c; - } - - const color32& color32::cyan() noexcept { - static const color32 c(0, 255, 255, 255); - return c; - } - color32::color32(const color& other) noexcept : r(static_cast(math::saturate(other.r) * 255.f + 0.5f)) , g(static_cast(math::saturate(other.g) * 255.f + 0.5f)) , b(static_cast(math::saturate(other.b) * 255.f + 0.5f)) , a(static_cast(math::saturate(other.a) * 255.f + 0.5f)) {} - color32::color32(u8 nr, u8 ng, u8 nb, u8 na) noexcept - : r(nr) - , g(ng) - , b(nb) - , a(na) {} + color32::color32(const vec4& rgba) noexcept + : r(rgba.x) + , g(rgba.y) + , b(rgba.z) + , a(rgba.w) {} + + color32::color32(const vec3& rgb, u8 a) noexcept + : r(rgb.x) + , g(rgb.y) + , b(rgb.z) + , a(a) {} u8* color32::data() noexcept { return &r; diff --git a/untests/sources/untests_high/luasol.cpp b/untests/sources/untests_high/luasol.cpp index b29d41c7..0cf217be 100644 --- a/untests/sources/untests_high/luasol.cpp +++ b/untests/sources/untests_high/luasol.cpp @@ -102,4 +102,15 @@ TEST_CASE("luasol") { )lua"); REQUIRE(r1 == v3f(1,2,3)); } + + SECTION("color/color32") { + color r0 = l.lua().script(R"lua( + return color.white() * 0.5 + )lua"); + REQUIRE(r0 == color(0.5f,0.5f,0.5f,0.5f)); + color32 r1 = l.lua().script(R"lua( + return color32.white() - 1 + )lua"); + REQUIRE(r1 == color32(254,254,254,254)); + } }