From f9c0354af17c655e3e385c982fef0c1b67e61336 Mon Sep 17 00:00:00 2001 From: Gregor Parzefall Date: Fri, 13 Sep 2024 13:14:30 +0200 Subject: [PATCH] Add colorspec_to_table to the Lua API --- doc/lua_api.md | 4 ++++ games/devtest/mods/unittests/color.lua | 17 +++++++++++++++++ games/devtest/mods/unittests/init.lua | 1 + src/script/lua_api/l_util.cpp | 17 +++++++++++++++++ src/script/lua_api/l_util.h | 3 +++ 5 files changed, 42 insertions(+) create mode 100644 games/devtest/mods/unittests/color.lua diff --git a/doc/lua_api.md b/doc/lua_api.md index 34af38abc..ec00b5130 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5655,6 +5655,10 @@ Utilities * `minetest.colorspec_to_bytes(colorspec)`: Converts a ColorSpec to a raw string of four bytes in an RGBA layout, returned as a string. * `colorspec`: The ColorSpec to convert +* `minetest.colorspec_to_table(colorspec)`: Converts a ColorSpec into RGBA table + form. If the ColorSpec is invalid, returns `nil`. You can use this to parse + ColorStrings. + * `colorspec`: The ColorSpec to convert * `minetest.encode_png(width, height, data, [compression])`: Encode a PNG image and return it in string form. * `width`: Width of the image diff --git a/games/devtest/mods/unittests/color.lua b/games/devtest/mods/unittests/color.lua new file mode 100644 index 000000000..86154445c --- /dev/null +++ b/games/devtest/mods/unittests/color.lua @@ -0,0 +1,17 @@ +local function assert_colors_equal(c1, c2) + if type(c1) == "table" and type(c2) == "table" then + assert(c1.r == c2.r and c1.g == c2.g and c1.b == c2.b and c1.a == c2.a) + else + assert(c1 == c2) + end +end + +local function test_color_conversion() + assert_colors_equal(core.colorspec_to_table("#fff"), {r = 255, g = 255, b = 255, a = 255}) + assert_colors_equal(core.colorspec_to_table(0xFF00FF00), {r = 0, g = 255, b = 0, a = 255}) + assert_colors_equal(core.colorspec_to_table("#00000000"), {r = 0, g = 0, b = 0, a = 0}) + assert_colors_equal(core.colorspec_to_table("green"), {r = 0, g = 128, b = 0, a = 255}) + assert_colors_equal(core.colorspec_to_table("gren"), nil) +end + +unittests.register("test_color_conversion", test_color_conversion) diff --git a/games/devtest/mods/unittests/init.lua b/games/devtest/mods/unittests/init.lua index eae003a2a..a967a986f 100644 --- a/games/devtest/mods/unittests/init.lua +++ b/games/devtest/mods/unittests/init.lua @@ -187,6 +187,7 @@ dofile(modpath .. "/raycast.lua") dofile(modpath .. "/inventory.lua") dofile(modpath .. "/load_time.lua") dofile(modpath .. "/on_shutdown.lua") +dofile(modpath .. "/color.lua") -------------- diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 79eb38629..0c66521f7 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -626,6 +626,20 @@ int ModApiUtil::l_colorspec_to_bytes(lua_State *L) return 0; } +// colorspec_to_table(colorspec) +int ModApiUtil::l_colorspec_to_table(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + video::SColor color(0); + if (read_color(L, 1, &color)) { + push_ARGB8(L, color); + return 1; + } + + return 0; +} + // encode_png(w, h, data, level) int ModApiUtil::l_encode_png(lua_State *L) { @@ -726,6 +740,7 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(sha256); API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); + API_FCT(colorspec_to_table); API_FCT(encode_png); @@ -761,6 +776,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top) API_FCT(sha256); API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); + API_FCT(colorspec_to_table); API_FCT(get_last_run_mod); API_FCT(set_last_run_mod); @@ -805,6 +821,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top) API_FCT(sha256); API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); + API_FCT(colorspec_to_table); API_FCT(encode_png); diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index e0daf3e79..442e0749d 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -122,6 +122,9 @@ private: // colorspec_to_bytes(colorspec) static int l_colorspec_to_bytes(lua_State *L); + // colorspec_to_table(colorspec) + static int l_colorspec_to_table(lua_State *L); + // encode_png(w, h, data, level) static int l_encode_png(lua_State *L);