mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-13 23:58:18 +07:00
color lua api
This commit is contained in:
86
samples/bin/library/scripts/emmy/utils/color.lua
Normal file
86
samples/bin/library/scripts/emmy/utils/color.lua
Normal file
@@ -0,0 +1,86 @@
|
||||
---@class color
|
||||
local color = {
|
||||
---@type number
|
||||
r = 1.0,
|
||||
|
||||
---@type number
|
||||
g = 1.0,
|
||||
|
||||
---@type number
|
||||
b = 1.0,
|
||||
|
||||
---@type number
|
||||
a = 1.0
|
||||
}
|
||||
|
||||
---@overload fun(): color
|
||||
---@overload fun(c: color): color
|
||||
---@overload fun(c: color32): color
|
||||
---@overload fun(v: v4f): color
|
||||
---@overload fun(v: v3f, a: number): color
|
||||
---@overload fun(r: number, g: number, b: number, a: number): color
|
||||
---@return color
|
||||
function color.new() end
|
||||
|
||||
---@return color
|
||||
function color.clear() end
|
||||
|
||||
---@return color
|
||||
function color.black() end
|
||||
|
||||
---@return color
|
||||
function color.white() end
|
||||
|
||||
---@return color
|
||||
function color.red() end
|
||||
|
||||
---@return color
|
||||
function color.green() end
|
||||
|
||||
---@return color
|
||||
function color.blue() end
|
||||
|
||||
---@return color
|
||||
function color.yellow() end
|
||||
|
||||
---@return color
|
||||
function color.magenta() end
|
||||
|
||||
---@return color
|
||||
function color.cyan() end
|
||||
|
||||
---@param l color
|
||||
---@param r color
|
||||
---@return boolean
|
||||
function color.approximately(l, r) end
|
||||
|
||||
---@param c color
|
||||
---@return number
|
||||
function color.minimum(c) end
|
||||
|
||||
---@param c color
|
||||
---@return number
|
||||
function color.maximum(c) end
|
||||
|
||||
---@param c color
|
||||
---@param cmin color
|
||||
---@return color
|
||||
function color.minimized(c,cmin) end
|
||||
|
||||
---@param c color
|
||||
---@param cmax color
|
||||
---@return color
|
||||
function color.maximized(c,cmax) end
|
||||
|
||||
---@param c color
|
||||
---@param cmin color
|
||||
---@param cmax color
|
||||
---@return color
|
||||
function color.clamped(c,cmin,cmax) end
|
||||
|
||||
---@param c color
|
||||
---@return boolean
|
||||
function color.contains_nan(c) end
|
||||
|
||||
---@type color
|
||||
_G.color = _G.color or color
|
||||
86
samples/bin/library/scripts/emmy/utils/color32.lua
Normal file
86
samples/bin/library/scripts/emmy/utils/color32.lua
Normal file
@@ -0,0 +1,86 @@
|
||||
---@class color32
|
||||
local color32 = {
|
||||
---@type number
|
||||
r = 255,
|
||||
|
||||
---@type number
|
||||
g = 255,
|
||||
|
||||
---@type number
|
||||
b = 255,
|
||||
|
||||
---@type number
|
||||
a = 255
|
||||
}
|
||||
|
||||
---@overload fun(): color32
|
||||
---@overload fun(c: color): color32
|
||||
---@overload fun(c: color32): color32
|
||||
---@overload fun(v: v4f): color32
|
||||
---@overload fun(v: v3f, a: number): color32
|
||||
---@overload fun(r: number, g: number, b: number, a: number): color32
|
||||
---@return color32
|
||||
function color32.new() end
|
||||
|
||||
---@return color32
|
||||
function color32.clear() end
|
||||
|
||||
---@return color32
|
||||
function color32.black() end
|
||||
|
||||
---@return color32
|
||||
function color32.white() end
|
||||
|
||||
---@return color32
|
||||
function color32.red() end
|
||||
|
||||
---@return color32
|
||||
function color32.green() end
|
||||
|
||||
---@return color32
|
||||
function color32.blue() end
|
||||
|
||||
---@return color32
|
||||
function color32.yellow() end
|
||||
|
||||
---@return color32
|
||||
function color32.magenta() end
|
||||
|
||||
---@return color32
|
||||
function color32.cyan() end
|
||||
|
||||
---@param l color32
|
||||
---@param r color32
|
||||
---@return boolean
|
||||
function color32.approximately(l, r) end
|
||||
|
||||
---@param c color32
|
||||
---@return number
|
||||
function color32.minimum(c) end
|
||||
|
||||
---@param c color32
|
||||
---@return number
|
||||
function color32.maximum(c) end
|
||||
|
||||
---@param c color32
|
||||
---@param cmin color32
|
||||
---@return color32
|
||||
function color32.minimized(c,cmin) end
|
||||
|
||||
---@param c color32
|
||||
---@param cmax color32
|
||||
---@return color32
|
||||
function color32.maximized(c,cmax) end
|
||||
|
||||
---@param c color32
|
||||
---@param cmin color32
|
||||
---@param cmax color32
|
||||
---@return color32
|
||||
function color32.clamped(c,cmin,cmax) end
|
||||
|
||||
---@param c color32
|
||||
---@return boolean
|
||||
function color32.contains_nan(c) end
|
||||
|
||||
---@type color32
|
||||
_G.color32 = _G.color32 or color32
|
||||
20
samples/bin/library/scripts/emmy/utils/str_hash.lua
Normal file
20
samples/bin/library/scripts/emmy/utils/str_hash.lua
Normal file
@@ -0,0 +1,20 @@
|
||||
---@class str_hash
|
||||
local str_hash = {
|
||||
---@type boolean
|
||||
empty = true,
|
||||
|
||||
---@type number
|
||||
hash = 0
|
||||
}
|
||||
|
||||
---@overload fun(): str_hash
|
||||
---@overload fun(s: string): str_hash
|
||||
---@return str_hash
|
||||
function str_hash.new() end
|
||||
|
||||
---@overload fun(s: str_hash)
|
||||
---@param s str_hash
|
||||
function str_hash.clear(s) end
|
||||
|
||||
---@type str_hash
|
||||
_G.str_hash = _G.str_hash or str_hash
|
||||
@@ -11,8 +11,7 @@ using namespace e2d;
|
||||
namespace e2d::bindings::utils
|
||||
{
|
||||
void bind_color32(sol::state& l) {
|
||||
l["e2d"].get_or_create<sol::table>()
|
||||
.new_usertype<color32>("color32",
|
||||
l.new_usertype<color32>("color32",
|
||||
sol::constructors<
|
||||
color32(),
|
||||
color32(color),
|
||||
@@ -68,9 +67,6 @@ namespace e2d::bindings::utils
|
||||
"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));
|
||||
"contains_nan", sol::resolve<bool(const color32&)>(&math::contains_nan));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,7 @@ using namespace e2d;
|
||||
namespace e2d::bindings::utils
|
||||
{
|
||||
void bind_color(sol::state& l) {
|
||||
l["e2d"].get_or_create<sol::table>()
|
||||
.new_usertype<color>("color",
|
||||
l.new_usertype<color>("color",
|
||||
sol::constructors<
|
||||
color(),
|
||||
color(color),
|
||||
@@ -68,9 +67,6 @@ namespace e2d::bindings::utils
|
||||
"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));
|
||||
"contains_nan", sol::resolve<bool(const color&)>(&math::contains_nan));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,11 @@ using namespace e2d;
|
||||
namespace e2d::bindings::utils
|
||||
{
|
||||
void bind_str_hash(sol::state& l) {
|
||||
l["e2d"].get_or_create<sol::table>()
|
||||
.new_usertype<str_hash>("str_hash",
|
||||
l.new_usertype<str_hash>("str_hash",
|
||||
sol::constructors<
|
||||
str_hash(),
|
||||
str_hash(const char*),
|
||||
str_hash(str_view)>(),
|
||||
str_hash(const char*)
|
||||
>(),
|
||||
|
||||
sol::meta_function::equal_to, sol::resolve<bool(str_hash, str_hash)>(::operator==),
|
||||
sol::meta_function::less_than, sol::resolve<bool(str_hash, str_hash)>(::operator<),
|
||||
|
||||
@@ -31,20 +31,20 @@ TEST_CASE("luasol") {
|
||||
SECTION("vec2/vec3/vec4") {
|
||||
v2f r0 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local v = e2d.v2f.new(1,2)
|
||||
return e2d.v2f.new((v + v + 2).y)
|
||||
local v = v2f.new(1,2)
|
||||
return v2f.new((v + v + 2).y)
|
||||
)lua");
|
||||
});
|
||||
v3f r1 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local v = e2d.v3f.new(1,2,3)
|
||||
return e2d.v3f.new((v + v + 2).y)
|
||||
local v = v3f.new(1,2,3)
|
||||
return v3f.new((v + v + 2).y)
|
||||
)lua");
|
||||
});
|
||||
v4f r2 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local v = e2d.v4f.new(1,2,3,4)
|
||||
return e2d.v4f.new((v + v + 2).y)
|
||||
local v = v4f.new(1,2,3,4)
|
||||
return v4f.new((v + v + 2).y)
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r0 == v2f(6));
|
||||
@@ -55,7 +55,7 @@ TEST_CASE("luasol") {
|
||||
SECTION("quat") {
|
||||
v3f r0 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
return e2d.v3f.new(1,2,3) * e2d.q4f.make_quat_from_axis_angle(e2d.radf.new(10), e2d.v3f.new(1,2,3))
|
||||
return v3f.new(1,2,3) * q4f.make_from_axis_angle(radf.new(10), v3f.new(1,2,3))
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r0 == v3f(1,2,3) * math::make_quat_from_axis_angle(radf(10.f), v3f(1,2,3)));
|
||||
@@ -64,23 +64,23 @@ TEST_CASE("luasol") {
|
||||
SECTION("mat2/mat2/mat3") {
|
||||
std::pair<m2f, bool> r0 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local m = e2d.m2f.make_scale_matrix2(2,3)
|
||||
local rm, s = e2d.m2f.inversed(m)
|
||||
return rm * e2d.m2f.identity(), s
|
||||
local m = m2f.make_scale(2,3)
|
||||
local rm, s = m2f.inversed(m)
|
||||
return rm * m2f.identity(), s
|
||||
)lua");
|
||||
});
|
||||
std::pair<m3f, bool> r1 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local m = e2d.m3f.make_rotation_matrix3(e2d.degf.new(45),2,3,4)
|
||||
local rm, s = e2d.m3f.inversed(m)
|
||||
return rm * e2d.m3f.identity(), s
|
||||
local m = m3f.make_rotation(degf.new(45),2,3,4)
|
||||
local rm, s = m3f.inversed(m)
|
||||
return rm * m3f.identity(), s
|
||||
)lua");
|
||||
});
|
||||
std::pair<m4f, bool> r2 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local m = e2d.m4f.make_translation_matrix4(2,3,4)
|
||||
local rm, s = e2d.m4f.inversed(m)
|
||||
return rm * e2d.m4f.identity(), s
|
||||
local m = m4f.make_translation(2,3,4)
|
||||
local rm, s = m4f.inversed(m)
|
||||
return rm * m4f.identity(), s
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r0.second);
|
||||
@@ -94,15 +94,15 @@ TEST_CASE("luasol") {
|
||||
SECTION("rect/aabb") {
|
||||
bool r0 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local b = e2d.b2f.unit() * 2
|
||||
return b:inside(e2d.v2f.new(1.5,1.5))
|
||||
local b = b2f.unit() * 2
|
||||
return b:inside(v2f.new(1.5,1.5))
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r0);
|
||||
bool r1 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local b = e2d.b3f.unit() * 2
|
||||
return b:overlaps(e2d.b3f.new(1.5,1.5,1.5,2,2,2))
|
||||
local b = b3f.unit() * 2
|
||||
return b:overlaps(b3f.new(1.5,1.5,1.5,2,2,2))
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r1);
|
||||
@@ -111,14 +111,14 @@ TEST_CASE("luasol") {
|
||||
SECTION("trs2/trs3") {
|
||||
radf r0 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local t = e2d.t2f.make_rotation_trs2(e2d.degf.new(45))
|
||||
local t = t2f.make_rotation(degf.new(45))
|
||||
return t.rotation
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r0 == math::to_rad(degf(45.f)));
|
||||
v3f r1 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local t = e2d.t3f.make_translation_trs3(e2d.v3f.new(1,2,3))
|
||||
local t = t3f.make_translation(v3f.new(1,2,3))
|
||||
return t.translation
|
||||
)lua");
|
||||
});
|
||||
@@ -128,15 +128,33 @@ TEST_CASE("luasol") {
|
||||
SECTION("color/color32") {
|
||||
color r0 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
return e2d.color.white() * 0.5
|
||||
return color.white() * 0.5
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r0 == color(0.5f,0.5f,0.5f,0.5f));
|
||||
color32 r1 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
return e2d.color32.white() - 1
|
||||
return color32.white() - 1
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r1 == color32(254,254,254,254));
|
||||
}
|
||||
|
||||
SECTION("str_hash") {
|
||||
str_hash r0 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local s = str_hash.new("hello")
|
||||
s:clear()
|
||||
return s
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r0.empty());
|
||||
u32 r1 = l.with_state([](sol::state& lua){
|
||||
return lua.script(R"lua(
|
||||
local s = str_hash.new("hello")
|
||||
return s.hash
|
||||
)lua");
|
||||
});
|
||||
REQUIRE(r1 == str_hash("hello").hash());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user