summaryrefslogtreecommitdiff
path: root/src/script/common/c_converter.cpp
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2015-05-16 12:26:57 -0400
committerkwolekr <kwolekr@minetest.net>2015-05-16 20:15:03 -0400
commitfb36c471d7c35bafec9c70028fbabf4ea4838ad2 (patch)
treea873df0cec3baa1342c39593f7977cb1700700f5 /src/script/common/c_converter.cpp
parent8f9af57314f71aae1cc77e13f9996e13015d776d (diff)
downloadminetest-fb36c471d7c35bafec9c70028fbabf4ea4838ad2.tar.gz
minetest-fb36c471d7c35bafec9c70028fbabf4ea4838ad2.tar.bz2
minetest-fb36c471d7c35bafec9c70028fbabf4ea4838ad2.zip
SAPI: Accept either ARGB8 table or ColorString to specify colors
Diffstat (limited to 'src/script/common/c_converter.cpp')
-rw-r--r--src/script/common/c_converter.cpp65
1 files changed, 42 insertions, 23 deletions
diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp
index 9d6829815..6fb6f623a 100644
--- a/src/script/common/c_converter.cpp
+++ b/src/script/common/c_converter.cpp
@@ -23,35 +23,23 @@ extern "C" {
}
#include "util/numeric.h"
+#include "util/string.h"
#include "common/c_converter.h"
#include "constants.h"
#define CHECK_TYPE(index, name, type) do { \
- int t = lua_type(L, (index)); \
- if (t != (type)) { \
- throw LuaError(std::string("Invalid ") + (name) + \
- " (expected " + lua_typename(L, (type)) + \
- " got " + lua_typename(L, t) + ")."); \
- } \
-} while(0)
+ int t = lua_type(L, (index)); \
+ if (t != (type)) { \
+ throw LuaError(std::string("Invalid ") + (name) + \
+ " (expected " + lua_typename(L, (type)) + \
+ " got " + lua_typename(L, t) + ")."); \
+ } \
+ } while(0)
#define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
#define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE)
-void push_ARGB8(lua_State *L, video::SColor color)
-{
- lua_newtable(L);
- lua_pushnumber(L, color.getAlpha());
- lua_setfield(L, -2, "a");
- lua_pushnumber(L, color.getRed());
- lua_setfield(L, -2, "r");
- lua_pushnumber(L, color.getGreen());
- lua_setfield(L, -2, "g");
- lua_pushnumber(L, color.getBlue());
- lua_setfield(L, -2, "b");
-}
-
void push_v3f(lua_State *L, v3f p)
{
lua_newtable(L);
@@ -176,6 +164,19 @@ v3f check_v3f(lua_State *L, int index)
return pos;
}
+void push_ARGB8(lua_State *L, video::SColor color)
+{
+ lua_newtable(L);
+ lua_pushnumber(L, color.getAlpha());
+ lua_setfield(L, -2, "a");
+ lua_pushnumber(L, color.getRed());
+ lua_setfield(L, -2, "r");
+ lua_pushnumber(L, color.getGreen());
+ lua_setfield(L, -2, "g");
+ lua_pushnumber(L, color.getBlue());
+ lua_setfield(L, -2, "b");
+}
+
void pushFloatPos(lua_State *L, v3f p)
{
p /= BS;
@@ -212,13 +213,31 @@ v3s16 check_v3s16(lua_State *L, int index)
return floatToInt(pf, 1.0);
}
-video::SColor readARGB8(lua_State *L, int index)
+bool read_color(lua_State *L, int index, video::SColor *color)
+{
+ if (lua_istable(L, index)) {
+ *color = read_ARGB8(L, index);
+ } else if (lua_isnumber(L, index)) {
+ color->set(lua_tonumber(L, index));
+ } else if (lua_isstring(L, index)) {
+ video::SColor parsed_color;
+ if (!parseColorString(lua_tostring(L, index), parsed_color, true))
+ return false;
+
+ *color = parsed_color;
+ } else {
+ return false;
+ }
+
+ return true;
+}
+
+video::SColor read_ARGB8(lua_State *L, int index)
{
video::SColor color(0);
CHECK_TYPE(index, "ARGB color", LUA_TTABLE);
lua_getfield(L, index, "a");
- if(lua_isnumber(L, -1))
- color.setAlpha(lua_tonumber(L, -1));
+ color.setAlpha(lua_isnumber(L, -1) ? lua_tonumber(L, -1) : 0xFF);
lua_pop(L, 1);
lua_getfield(L, index, "r");
color.setRed(lua_tonumber(L, -1));