add color/color32 bindings

This commit is contained in:
2019-10-04 03:53:26 +07:00
parent c7e2b52141
commit ad37e6fe84
6 changed files with 286 additions and 128 deletions

View File

@@ -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<f32>& rgba) noexcept;
explicit color(const vec3<f32>& 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<f32> make_vec3(const color& c) noexcept;

View File

@@ -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<u8>& rgba) noexcept;
explicit color32(const vec3<u8>& 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<u8> make_vec3(const color32& c) noexcept;

View File

@@ -6,10 +6,141 @@
#include "bindings.hpp"
namespace
{
using namespace e2d;
void bind_color(sol::state& l) {
l.new_usertype<color>("color",
sol::constructors<
color(),
color(color),
color(color32),
color(vec4<f32>),
color(vec3<f32>, 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<bool(const color&, const color&)>(::operator==),
sol::meta_function::less_than, sol::resolve<bool(const color&, const color&)>(::operator<),
sol::meta_function::addition, sol::overload(
sol::resolve<color(f32, const color&)>(::operator+),
sol::resolve<color(color, f32)>(::operator+),
sol::resolve<color(color, const color&)>(::operator+)),
sol::meta_function::subtraction, sol::overload(
sol::resolve<color(f32, const color&)>(::operator-),
sol::resolve<color(color, f32)>(::operator-),
sol::resolve<color(color, const color&)>(::operator-)),
sol::meta_function::multiplication, sol::overload(
sol::resolve<color(f32, const color&)>(::operator*),
sol::resolve<color(color, f32)>(::operator*),
sol::resolve<color(color, const color&)>(::operator*)),
sol::meta_function::division, sol::overload(
sol::resolve<color(f32, const color&)>(::operator/),
sol::resolve<color(color, f32)>(::operator/),
sol::resolve<color(color, const color&)>(::operator/)),
"approximately", [](const color& l, const color& r){ return math::approximately(l,r); },
"minimum", sol::resolve<f32(const color&)>(&math::minimum),
"maximum", sol::resolve<f32(const color&)>(&math::maximum),
"minimized", sol::resolve<color(const color&, const color&)>(&math::minimized),
"maximized", sol::resolve<color(const color&, const color&)>(&math::maximized),
"clamped", sol::resolve<color(const color&, const color&, const color&)>(&math::clamped),
"contains_nan", sol::resolve<bool(const color&)>(&math::contains_nan),
"pack_color", sol::resolve<u32(const color&)>(&colors::pack_color),
"unpack_color", sol::resolve<color(u32)>(&colors::unpack_color));
}
void bind_color32(sol::state& l) {
l.new_usertype<color32>("color32",
sol::constructors<
color32(),
color32(color),
color32(color32),
color32(vec4<u8>),
color32(vec3<u8>, 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<bool(const color32&, const color32&)>(::operator==),
sol::meta_function::less_than, sol::resolve<bool(const color32&, const color32&)>(::operator<),
sol::meta_function::addition, sol::overload(
sol::resolve<color32(u8, const color32&)>(::operator+),
sol::resolve<color32(color32, u8)>(::operator+),
sol::resolve<color32(color32, const color32&)>(::operator+)),
sol::meta_function::subtraction, sol::overload(
sol::resolve<color32(u8, const color32&)>(::operator-),
sol::resolve<color32(color32, u8)>(::operator-),
sol::resolve<color32(color32, const color32&)>(::operator-)),
sol::meta_function::multiplication, sol::overload(
sol::resolve<color32(u8, const color32&)>(::operator*),
sol::resolve<color32(color32, u8)>(::operator*),
sol::resolve<color32(color32, const color32&)>(::operator*)),
sol::meta_function::division, sol::overload(
sol::resolve<color32(u8, const color32&)>(::operator/),
sol::resolve<color32(color32, u8)>(::operator/),
sol::resolve<color32(color32, const color32&)>(::operator/)),
"approximately", [](const color32& l, const color32& r){ return math::approximately(l,r); },
"minimum", sol::resolve<u8(const color32&)>(&math::minimum),
"maximum", sol::resolve<u8(const color32&)>(&math::maximum),
"minimized", sol::resolve<color32(const color32&, const color32&)>(&math::minimized),
"maximized", sol::resolve<color32(const color32&, const color32&)>(&math::maximized),
"clamped", sol::resolve<color32(const color32&, const color32&, const color32&)>(&math::clamped),
"contains_nan", sol::resolve<bool(const color32&)>(&math::contains_nan),
"pack_color", sol::resolve<u32(const color32&)>(&colors::pack_color32),
"unpack_color", sol::resolve<color32(u32)>(&colors::unpack_color32));
}
}
namespace e2d::bindings
{
void bind_utils(sol::state& l) {
//TODO(BlackMat): implme
E2D_UNUSED(l);
bind_color(l);
bind_color32(l);
}
}

View File

@@ -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<f32>& rgba) noexcept
: r(rgba.x)
, g(rgba.y)
, b(rgba.z)
, a(rgba.w) {}
color::color(const vec3<f32>& rgb, f32 a) noexcept
: r(rgb.x)
, g(rgb.y)
, b(rgb.z)
, a(a) {}
f32* color::data() noexcept {
return &r;

View File

@@ -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<u8>(math::saturate(other.r) * 255.f + 0.5f))
, g(static_cast<u8>(math::saturate(other.g) * 255.f + 0.5f))
, b(static_cast<u8>(math::saturate(other.b) * 255.f + 0.5f))
, a(static_cast<u8>(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<u8>& rgba) noexcept
: r(rgba.x)
, g(rgba.y)
, b(rgba.z)
, a(rgba.w) {}
color32::color32(const vec3<u8>& rgb, u8 a) noexcept
: r(rgb.x)
, g(rgb.y)
, b(rgb.z)
, a(a) {}
u8* color32::data() noexcept {
return &r;

View File

@@ -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));
}
}