Add colorspec_to_table to the Lua API

This commit is contained in:
Gregor Parzefall 2024-09-13 13:14:30 +02:00 committed by sfan5
parent c54f5a2137
commit f9c0354af1
5 changed files with 42 additions and 0 deletions

View File

@ -5655,6 +5655,10 @@ Utilities
* `minetest.colorspec_to_bytes(colorspec)`: Converts a ColorSpec to a raw * `minetest.colorspec_to_bytes(colorspec)`: Converts a ColorSpec to a raw
string of four bytes in an RGBA layout, returned as a string. string of four bytes in an RGBA layout, returned as a string.
* `colorspec`: The ColorSpec to convert * `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 * `minetest.encode_png(width, height, data, [compression])`: Encode a PNG
image and return it in string form. image and return it in string form.
* `width`: Width of the image * `width`: Width of the image

View File

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

View File

@ -187,6 +187,7 @@ dofile(modpath .. "/raycast.lua")
dofile(modpath .. "/inventory.lua") dofile(modpath .. "/inventory.lua")
dofile(modpath .. "/load_time.lua") dofile(modpath .. "/load_time.lua")
dofile(modpath .. "/on_shutdown.lua") dofile(modpath .. "/on_shutdown.lua")
dofile(modpath .. "/color.lua")
-------------- --------------

View File

@ -626,6 +626,20 @@ int ModApiUtil::l_colorspec_to_bytes(lua_State *L)
return 0; 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) // encode_png(w, h, data, level)
int ModApiUtil::l_encode_png(lua_State *L) 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(sha256);
API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_colorstring);
API_FCT(colorspec_to_bytes); API_FCT(colorspec_to_bytes);
API_FCT(colorspec_to_table);
API_FCT(encode_png); API_FCT(encode_png);
@ -761,6 +776,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
API_FCT(sha256); API_FCT(sha256);
API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_colorstring);
API_FCT(colorspec_to_bytes); API_FCT(colorspec_to_bytes);
API_FCT(colorspec_to_table);
API_FCT(get_last_run_mod); API_FCT(get_last_run_mod);
API_FCT(set_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(sha256);
API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_colorstring);
API_FCT(colorspec_to_bytes); API_FCT(colorspec_to_bytes);
API_FCT(colorspec_to_table);
API_FCT(encode_png); API_FCT(encode_png);

View File

@ -122,6 +122,9 @@ private:
// colorspec_to_bytes(colorspec) // colorspec_to_bytes(colorspec)
static int l_colorspec_to_bytes(lua_State *L); 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) // encode_png(w, h, data, level)
static int l_encode_png(lua_State *L); static int l_encode_png(lua_State *L);