Colourb.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "Colourb.h"
  29. namespace Rml {
  30. namespace Lua {
  31. template <>
  32. void ExtraInit<Colourb>(lua_State* L, int metatable_index)
  33. {
  34. lua_pushcfunction(L, Colourbnew);
  35. lua_setfield(L, metatable_index - 1, "new");
  36. lua_pushcfunction(L, Colourb__eq);
  37. lua_setfield(L, metatable_index, "__eq");
  38. lua_pushcfunction(L, Colourb__add);
  39. lua_setfield(L, metatable_index, "__add");
  40. lua_pushcfunction(L, Colourb__mul);
  41. lua_setfield(L, metatable_index, "__mul");
  42. return;
  43. }
  44. int Colourbnew(lua_State* L)
  45. {
  46. byte red = (byte)luaL_checkinteger(L, 1);
  47. byte green = (byte)luaL_checkinteger(L, 2);
  48. byte blue = (byte)luaL_checkinteger(L, 3);
  49. byte alpha = (byte)luaL_checkinteger(L, 4);
  50. Colourb* col = new Colourb(red, green, blue, alpha);
  51. LuaType<Colourb>::push(L, col, true);
  52. return 1;
  53. }
  54. int Colourb__eq(lua_State* L)
  55. {
  56. Colourb* lhs = LuaType<Colourb>::check(L, 1);
  57. RMLUI_CHECK_OBJ(lhs);
  58. Colourb* rhs = LuaType<Colourb>::check(L, 2);
  59. RMLUI_CHECK_OBJ(rhs);
  60. lua_pushboolean(L, (*lhs) == (*rhs) ? 1 : 0);
  61. return 1;
  62. }
  63. int Colourb__add(lua_State* L)
  64. {
  65. Colourb* lhs = LuaType<Colourb>::check(L, 1);
  66. RMLUI_CHECK_OBJ(lhs);
  67. Colourb* rhs = LuaType<Colourb>::check(L, 2);
  68. RMLUI_CHECK_OBJ(rhs);
  69. Colourb* res = new Colourb((*lhs) + (*rhs));
  70. LuaType<Colourb>::push(L, res, true);
  71. return 1;
  72. }
  73. int Colourb__mul(lua_State* L)
  74. {
  75. Colourb* lhs = LuaType<Colourb>::check(L, 1);
  76. RMLUI_CHECK_OBJ(lhs);
  77. float rhs = (float)luaL_checknumber(L, 2);
  78. Colourb* res = new Colourb((*lhs) * rhs);
  79. LuaType<Colourb>::push(L, res, true);
  80. return 1;
  81. }
  82. // getters
  83. int ColourbGetAttrred(lua_State* L)
  84. {
  85. Colourb* obj = LuaType<Colourb>::check(L, 1);
  86. RMLUI_CHECK_OBJ(obj);
  87. lua_pushinteger(L, obj->red);
  88. return 1;
  89. }
  90. int ColourbGetAttrgreen(lua_State* L)
  91. {
  92. Colourb* obj = LuaType<Colourb>::check(L, 1);
  93. RMLUI_CHECK_OBJ(obj);
  94. lua_pushinteger(L, obj->green);
  95. return 1;
  96. }
  97. int ColourbGetAttrblue(lua_State* L)
  98. {
  99. Colourb* obj = LuaType<Colourb>::check(L, 1);
  100. RMLUI_CHECK_OBJ(obj);
  101. lua_pushinteger(L, obj->blue);
  102. return 1;
  103. }
  104. int ColourbGetAttralpha(lua_State* L)
  105. {
  106. Colourb* obj = LuaType<Colourb>::check(L, 1);
  107. RMLUI_CHECK_OBJ(obj);
  108. lua_pushinteger(L, obj->alpha);
  109. return 1;
  110. }
  111. int ColourbGetAttrrgba(lua_State* L)
  112. {
  113. Colourb* obj = LuaType<Colourb>::check(L, 1);
  114. RMLUI_CHECK_OBJ(obj);
  115. lua_pushinteger(L, obj->red);
  116. lua_pushinteger(L, obj->green);
  117. lua_pushinteger(L, obj->blue);
  118. lua_pushinteger(L, obj->alpha);
  119. return 4;
  120. }
  121. // setters
  122. int ColourbSetAttrred(lua_State* L)
  123. {
  124. Colourb* obj = LuaType<Colourb>::check(L, 1);
  125. RMLUI_CHECK_OBJ(obj);
  126. byte red = (byte)luaL_checkinteger(L, 2);
  127. obj->red = red;
  128. return 0;
  129. }
  130. int ColourbSetAttrgreen(lua_State* L)
  131. {
  132. Colourb* obj = LuaType<Colourb>::check(L, 1);
  133. RMLUI_CHECK_OBJ(obj);
  134. byte green = (byte)luaL_checkinteger(L, 2);
  135. obj->green = green;
  136. return 0;
  137. }
  138. int ColourbSetAttrblue(lua_State* L)
  139. {
  140. Colourb* obj = LuaType<Colourb>::check(L, 1);
  141. RMLUI_CHECK_OBJ(obj);
  142. byte blue = (byte)luaL_checkinteger(L, 2);
  143. obj->blue = blue;
  144. return 0;
  145. }
  146. int ColourbSetAttralpha(lua_State* L)
  147. {
  148. Colourb* obj = LuaType<Colourb>::check(L, 1);
  149. RMLUI_CHECK_OBJ(obj);
  150. byte alpha = (byte)luaL_checkinteger(L, 2);
  151. obj->alpha = alpha;
  152. return 0;
  153. }
  154. int ColourbSetAttrrgba(lua_State* L)
  155. {
  156. Colourb* obj = nullptr;
  157. int top = lua_gettop(L);
  158. // each of the items are optional.
  159. if (top > 0)
  160. {
  161. obj = LuaType<Colourb>::check(L, 1);
  162. RMLUI_CHECK_OBJ(obj);
  163. if (top > 1)
  164. {
  165. if (top > 2)
  166. {
  167. if (top > 3)
  168. obj->alpha = (byte)luaL_checkinteger(L, 4);
  169. obj->blue = (byte)luaL_checkinteger(L, 3);
  170. }
  171. obj->green = (byte)luaL_checkinteger(L, 2);
  172. }
  173. obj->red = (byte)luaL_checkinteger(L, 1);
  174. }
  175. return 0;
  176. }
  177. RegType<Colourb> ColourbMethods[] = {
  178. {nullptr, nullptr},
  179. };
  180. luaL_Reg ColourbGetters[] = {
  181. RMLUI_LUAGETTER(Colourb, red),
  182. RMLUI_LUAGETTER(Colourb, green),
  183. RMLUI_LUAGETTER(Colourb, blue),
  184. RMLUI_LUAGETTER(Colourb, alpha),
  185. RMLUI_LUAGETTER(Colourb, rgba),
  186. {nullptr, nullptr},
  187. };
  188. luaL_Reg ColourbSetters[] = {
  189. RMLUI_LUASETTER(Colourb, red),
  190. RMLUI_LUASETTER(Colourb, green),
  191. RMLUI_LUASETTER(Colourb, blue),
  192. RMLUI_LUASETTER(Colourb, alpha),
  193. RMLUI_LUASETTER(Colourb, rgba),
  194. {nullptr, nullptr},
  195. };
  196. RMLUI_LUATYPE_DEFINE(Colourb)
  197. } // namespace Lua
  198. } // namespace Rml