Skip to content

Commit

Permalink
Add minetest.colorspec_to_colorstring (#10425)
Browse files Browse the repository at this point in the history
  • Loading branch information
v-rob committed Apr 23, 2021
1 parent 3e2145d commit 074e6a6
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 225 deletions.
8 changes: 5 additions & 3 deletions doc/client_lua_api.txt
Expand Up @@ -651,6 +651,9 @@ Minetest namespace reference
* `minetest.sha1(data, [raw])`: returns the sha1 hash of data
* `data`: string of data to hash
* `raw`: return raw bytes instead of hex digits, default: false
* `minetest.colorspec_to_colorstring(colorspec)`: Converts a ColorSpec to a
ColorString. If the ColorSpec is invalid, returns `nil`.
* `colorspec`: The ColorSpec to convert
* `minetest.get_csm_restrictions()`: returns a table of `Flags` indicating the
restrictions applied to the current mod.
* If a flag in this table is set to true, the feature is RESTRICTED.
Expand Down Expand Up @@ -1348,9 +1351,8 @@ The following functions provide escape sequences:

Named colors are also supported and are equivalent to
[CSS Color Module Level 4](http://dev.w3.org/csswg/css-color/#named-colors).
To specify the value of the alpha channel, append `#AA` to the end of the color name
(e.g. `colorname#08`). For named colors the hexadecimal string representing the alpha
value must (always) be two hexadecimal digits.
To specify the value of the alpha channel, append `#A` or `#AA` to the end of
the color name (e.g. `colorname#08`).

`Color`
-------------
Expand Down
8 changes: 5 additions & 3 deletions doc/lua_api.txt
Expand Up @@ -3100,9 +3100,8 @@ Colors

Named colors are also supported and are equivalent to
[CSS Color Module Level 4](http://dev.w3.org/csswg/css-color/#named-colors).
To specify the value of the alpha channel, append `#AA` to the end of the color
name (e.g. `colorname#08`). For named colors the hexadecimal string
representing the alpha value must (always) be two hexadecimal digits.
To specify the value of the alpha channel, append `#A` or `#AA` to the end of
the color name (e.g. `colorname#08`).

`ColorSpec`
-----------
Expand Down Expand Up @@ -4489,6 +4488,9 @@ Utilities
* `minetest.sha1(data, [raw])`: returns the sha1 hash of data
* `data`: string of data to hash
* `raw`: return raw bytes instead of hex digits, default: false
* `minetest.colorspec_to_colorstring(colorspec)`: Converts a ColorSpec to a
ColorString. If the ColorSpec is invalid, returns `nil`.
* `colorspec`: The ColorSpec to convert

Logging
-------
Expand Down
24 changes: 22 additions & 2 deletions src/script/lua_api/l_util.cpp
Expand Up @@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "irrlichttypes_extrabloated.h"
#include "lua_api/l_util.h"
#include "lua_api/l_internal.h"
#include "lua_api/l_settings.h"
Expand All @@ -40,7 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/hex.h"
#include "util/sha1.h"
#include <algorithm>

#include <cstdio>

// log([level,] text)
// Writes a line to the logger.
Expand Down Expand Up @@ -479,6 +480,23 @@ int ModApiUtil::l_sha1(lua_State *L)
return 1;
}

// colorspec_to_colorstring(colorspec)
int ModApiUtil::l_colorspec_to_colorstring(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;

video::SColor color(0);
if (read_color(L, 1, &color)) {
char colorstring[10];
snprintf(colorstring, 10, "#%02X%02X%02X%02X",
color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
lua_pushstring(L, colorstring);
return 1;
}

return 0;
}

void ModApiUtil::Initialize(lua_State *L, int top)
{
API_FCT(log);
Expand Down Expand Up @@ -513,6 +531,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)

API_FCT(get_version);
API_FCT(sha1);
API_FCT(colorspec_to_colorstring);

LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");
Expand All @@ -537,6 +556,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)

API_FCT(get_version);
API_FCT(sha1);
API_FCT(colorspec_to_colorstring);
}

void ModApiUtil::InitializeAsync(lua_State *L, int top)
Expand Down Expand Up @@ -564,8 +584,8 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)

API_FCT(get_version);
API_FCT(sha1);
API_FCT(colorspec_to_colorstring);

LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");
}

3 changes: 3 additions & 0 deletions src/script/lua_api/l_util.h
Expand Up @@ -101,6 +101,9 @@ class ModApiUtil : public ModApiBase
// sha1(string, raw)
static int l_sha1(lua_State *L);

// colorspec_to_colorstring(colorspec)
static int l_colorspec_to_colorstring(lua_State *L);

public:
static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);
Expand Down

0 comments on commit 074e6a6

Please sign in to comment.