The Battle for Wesnoth  1.15.5+dev
lua_color.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2020-2020 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY.
10 
11 See the COPYING file for more details.
12 */
13 
14 #include "color_range.hpp"
15 #include "scripting/lua_color.hpp"
16 #include "scripting/lua_common.hpp"
17 #include "scripting/push_check.hpp"
18 #include "lua/lauxlib.h"
19 #include "lua/lua.h" // for lua_State, lua_settop, etc
20 #include "log.hpp"
21 #include "game_config.hpp"
22 
23 static lg::log_domain log_scripting_lua("scripting/lua");
24 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
25 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
26 
27 static const char colorKey[] = "color range";
28 
30 {
31  return luaL_testudata(L, index, colorKey) != nullptr;
32 }
33 
35 {
36  if(!luaW_iscolor(L, index)) {
37  luaW_type_error(L, index, "color");
38  throw "luaW_type_error returned";
39  }
40  return *static_cast<color_range*>(lua_touserdata(L, index));
41 }
42 
43 
45 {
46  color_range* res = new(L) color_range(color);
48  return res;
49 }
50 
51 int luaW_pushsinglecolor(lua_State *L, const color_t& color)
52 {
53  lua_createtable(L, 0, 4);
54  luaW_table_set(L, -1, "r", color.r);
55  luaW_table_set(L, -1, "g", color.g);
56  luaW_table_set(L, -1, "b", color.b);
57  luaW_table_set(L, -1, "a", color.a);
58  return 1;
59 }
60 
62 {
63  color_range *c = static_cast<color_range *>(lua_touserdata(L, 1));
64  c->color_range::~color_range();
65  return 0;
66 }
67 
68 /**
69  * Checks two color units for equality. (__eq metamethod)
70  */
72 {
75  const bool equal = left == right;
76  lua_pushboolean(L, equal);
77  return 1;
78 }
79 
80 /**
81  * Turns a lua color to string. (__tostring metamethod)
82  */
84 {
85  color_range& c = LuaW_checkcolor(L, 1);
86  //TODO: is this the best way to call tostring?
87  lua_push(L, c.debug());
88  return 1;
89 }
90 
91 
92 /**
93  * - Arg 1: userdata (ignored).
94  * - Arg 2: string containing the name of the color.
95  * - Ret 1: color_range containing the color.
96  */
97 static int impl_get_color(lua_State *L)
98 {
99  std::string color_id = luaL_checkstring(L, 2);
101  return 1;
102 }
103 
104 static int impl_color_get(lua_State *L)
105 {
106  color_range& c = LuaW_checkcolor(L, 1);
107  char const *m = luaL_checkstring(L, 2);
108 
109  if(strcmp(m, "min") == 0) {
110  return luaW_pushsinglecolor(L, c.min());
111  }
112  if(strcmp(m, "max") == 0) {
113  return luaW_pushsinglecolor(L, c.max());
114  }
115  if(strcmp(m, "mid") == 0) {
116  return luaW_pushsinglecolor(L, c.mid());
117  }
118  if(strcmp(m, "minimap") == 0) {
119  return luaW_pushsinglecolor(L, c.rep());
120  }
121  // TODO: i think this shortcut would be useful, but im not sure yet on the best attribute name.
122  //if(strcmp(m, "pango_hex") == 0) {
123  // lua_push(L, c.mid().to_hex_string());
124  // return 1;
125  //}
126  return 0;
127 }
128 
129 static int impl_color_set(lua_State *L)
130 {
131  return luaL_argerror(L, 2, "color objects canot be modified");
132 }
133 
134 namespace lua_colors {
136  {
137  std::ostringstream cmd_out;
138 
139  // Create the getunit metatable.
140  cmd_out << "Adding color metatable...\n";
141 
144  lua_setfield(L, -2, "__gc");
146  lua_setfield(L, -2, "__eq");
148  lua_setfield(L, -2, "__tostring");
150  lua_setfield(L, -2, "__index");
152  lua_setfield(L, -2, "__newindex");
153  lua_pushstring(L, "color range");
154  lua_setfield(L, -2, "__metatable");
155 
156 
157  // Create the current variable with its metatable.
158  cmd_out << "Adding wesnoth.colors table...\n";
159 
160  lua_getglobal(L, "wesnoth");
161  lua_newuserdata(L, 0);
162  lua_createtable(L, 0, 2);
164  lua_setfield(L, -2, "__index");
165  lua_pushstring(L, "colors table");
166  lua_setfield(L, -2, "__metatable");
167  lua_setmetatable(L, -2);
168  lua_setfield(L, -2, "colors");
169  lua_pop(L, 1);
170 
171  return cmd_out.str();
172  }
173 }
174 
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:684
#define lua_pushcfunction(L, f)
Definition: lua.h:350
int luaW_pushsinglecolor(lua_State *L, const color_t &color)
Definition: lua_color.cpp:51
color_t min() const
Minimum color shade.
Definition: color_range.hpp:93
int luaW_type_error(lua_State *L, int narg, const char *tname)
color_range * luaW_pushcolor(lua_State *L, const color_range &color)
Definition: lua_color.cpp:44
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.cpp:557
static int impl_color_set(lua_State *L)
Definition: lua_color.cpp:129
LUA_API int lua_getglobal(lua_State *L, const char *name)
Definition: lapi.cpp:605
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:312
void lua_push(lua_State *L, const T &val)
Definition: push_check.hpp:400
LUA_API void * lua_newuserdata(lua_State *L, size_t size)
Definition: lapi.cpp:1184
#define lua_pop(L, n)
Definition: lua.h:344
static int impl_color_get(lua_State *L)
Definition: lua_color.cpp:104
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg)
Definition: lauxlib.cpp:164
uint8_t r
Red value.
Definition: color.hpp:177
uint8_t a
Alpha value.
Definition: color.hpp:186
std::string register_metatables(lua_State *L)
Definition: lua_color.cpp:135
LUA_API int lua_setmetatable(lua_State *L, int objindex)
Definition: lapi.cpp:846
LUALIB_API void * luaL_testudata(lua_State *L, int ud, const char *tname)
Definition: lauxlib.cpp:318
static lg::log_domain log_scripting_lua("scripting/lua")
bool luaW_iscolor(lua_State *L, int index)
Definition: lua_color.cpp:29
lu_byte right
Definition: lparser.cpp:1027
static int impl_color_tostring(lua_State *L)
Turns a lua color to string.
Definition: lua_color.cpp:83
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:299
color_t max() const
Maximum color shade.
Definition: color_range.hpp:90
LUA_API void * lua_touserdata(lua_State *L, int idx)
Definition: lapi.cpp:413
static int impl_color_equality(lua_State *L)
Checks two color units for equality.
Definition: lua_color.cpp:71
A color range definition is made of four reference RGB colors, used for calculating conversions from ...
Definition: color_range.hpp:50
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
static int impl_color_collect(lua_State *L)
Definition: lua_color.cpp:61
void luaW_table_set(lua_State *L, int index, utils::string_view k, const T &value)
Definition: push_check.hpp:432
std::string debug() const
Return a string describing the color range for debug output.
color_t rep() const
High-contrast shade, intended for the minimap markers.
Definition: color_range.hpp:96
lu_byte left
Definition: lparser.cpp:1026
const color_range & color_info(const std::string &name)
Standard logging facilities (interface).
static color_range & LuaW_checkcolor(lua_State *L, int index)
Definition: lua_color.cpp:34
static int impl_get_color(lua_State *L)
Definition: lua_color.cpp:97
uint8_t g
Green value.
Definition: color.hpp:180
uint8_t b
Blue value.
Definition: color.hpp:183
mock_char c
static const char colorKey[]
Definition: lua_color.cpp:27
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:491
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:777
color_t mid() const
Average color shade.
Definition: color_range.hpp:87
#define luaL_checkstring(L, n)
Definition: lauxlib.h:124