1// Aseprite
2// Copyright (C) 2017-2018 David Capello
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "app/script/luacpp.h"
12#include "doc/color.h"
13#include "doc/tile.h"
14
15namespace app {
16namespace script {
17
18namespace {
19
20int PixelColor_rgba(lua_State* L)
21{
22 const int r = lua_tointeger(L, 1);
23 const int g = lua_tointeger(L, 2);
24 const int b = lua_tointeger(L, 3);
25 const int a = (lua_isnoneornil(L, 4) ? 255: lua_tointeger(L, 4));
26 lua_pushinteger(L, doc::rgba(r, g, b, a));
27 return 1;
28}
29
30int PixelColor_rgbaR(lua_State* L)
31{
32 lua_pushinteger(L, doc::rgba_getr(lua_tointeger(L, 1)));
33 return 1;
34}
35
36int PixelColor_rgbaG(lua_State* L)
37{
38 lua_pushinteger(L, doc::rgba_getg(lua_tointeger(L, 1)));
39 return 1;
40}
41
42int PixelColor_rgbaB(lua_State* L)
43{
44 lua_pushinteger(L, doc::rgba_getb(lua_tointeger(L, 1)));
45 return 1;
46}
47
48int PixelColor_rgbaA(lua_State* L)
49{
50 lua_pushinteger(L, doc::rgba_geta(lua_tointeger(L, 1)));
51 return 1;
52}
53
54int PixelColor_graya(lua_State* L)
55{
56 int v = lua_tointeger(L, 1);
57 int a = (lua_isnoneornil(L, 2) ? 255: lua_tointeger(L, 2));
58 lua_pushinteger(L, doc::graya(v, a));
59 return 1;
60}
61
62int PixelColor_grayaV(lua_State* L)
63{
64 lua_pushinteger(L, doc::graya_getv(lua_tointeger(L, 1)));
65 return 1;
66}
67
68int PixelColor_grayaA(lua_State* L)
69{
70 lua_pushinteger(L, doc::graya_geta(lua_tointeger(L, 1)));
71 return 1;
72}
73
74int PixelColor_tile(lua_State* L)
75{
76 const int i = lua_tointeger(L, 1);
77 const int f = lua_tointeger(L, 2);
78 lua_pushinteger(L, doc::tile(i, f));
79 return 1;
80}
81
82int PixelColor_tileI(lua_State* L)
83{
84 lua_pushinteger(L, doc::tile_geti(lua_tointeger(L, 1)));
85 return 1;
86}
87
88int PixelColor_tileF(lua_State* L)
89{
90 lua_pushinteger(L, doc::tile_getf(lua_tointeger(L, 1)));
91 return 1;
92}
93
94const luaL_Reg PixelColor_methods[] = {
95 { "rgba", PixelColor_rgba },
96 { "rgbaR", PixelColor_rgbaR },
97 { "rgbaG", PixelColor_rgbaG },
98 { "rgbaB", PixelColor_rgbaB },
99 { "rgbaA", PixelColor_rgbaA },
100 { "graya", PixelColor_graya },
101 { "grayaV", PixelColor_grayaV },
102 { "grayaA", PixelColor_grayaA },
103 { "tile", PixelColor_tile },
104 { "tileI", PixelColor_tileI },
105 { "tileF", PixelColor_tileF },
106 { nullptr, nullptr }
107};
108
109} // anonymous namespace
110
111void register_app_pixel_color_object(lua_State* L)
112{
113 lua_getglobal(L, "app");
114 lua_newtable(L); // New table for pixelColor
115 lua_pushstring(L, "pixelColor");
116 lua_pushvalue(L, -2); // Copy table
117 lua_rawset(L, -4);
118 luaL_setfuncs(L, PixelColor_methods, 0);
119 lua_pop(L, 2); // Pop table & app global
120}
121
122} // namespace script
123} // namespace app
124