1 | // Aseprite |
2 | // Copyright (C) 2019 Igara Studio S.A. |
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/app.h" |
12 | #include "app/script/luacpp.h" |
13 | #include "base/file_content.h" |
14 | #include "gfx/color_space.h" |
15 | |
16 | namespace app { |
17 | namespace script { |
18 | |
19 | namespace { |
20 | |
21 | int ColorSpace_new(lua_State* L) |
22 | { |
23 | // Copy color space |
24 | if (auto cs2 = may_get_obj<gfx::ColorSpace>(L, 1)) { |
25 | push_new<gfx::ColorSpace>(L, *cs2); |
26 | return 1; |
27 | } |
28 | else if (lua_istable(L, 1)) { |
29 | // Load ICC profile when ColorSpace{ fromFile="..." } is specified |
30 | if (lua_getfield(L, 1, "fromFile" ) != LUA_TNIL) { |
31 | const char* fn = lua_tostring(L, -1); |
32 | if (fn) { |
33 | auto buf = base::read_file_content(fn); |
34 | lua_pop(L, 1); |
35 | push_new<gfx::ColorSpace>(L, *gfx::ColorSpace::MakeICC(std::move(buf))); |
36 | return 1; |
37 | } |
38 | } |
39 | else |
40 | lua_pop(L, 1); |
41 | |
42 | // Create sRGB profile with ColorSpace{ sRGB } |
43 | if (lua_is_key_true(L, 1, "sRGB" )) { |
44 | lua_pop(L, 1); |
45 | push_new<gfx::ColorSpace>(L, *gfx::ColorSpace::MakeSRGB()); |
46 | return 1; |
47 | } |
48 | else |
49 | lua_pop(L, 1); |
50 | } |
51 | push_new<gfx::ColorSpace>(L, *gfx::ColorSpace::MakeNone()); |
52 | return 1; |
53 | } |
54 | |
55 | int ColorSpace_gc(lua_State* L) |
56 | { |
57 | get_obj<gfx::ColorSpace>(L, 1)->~ColorSpace(); |
58 | return 0; |
59 | } |
60 | |
61 | int ColorSpace_eq(lua_State* L) |
62 | { |
63 | const auto a = get_obj<gfx::ColorSpace>(L, 1); |
64 | const auto b = get_obj<gfx::ColorSpace>(L, 2); |
65 | lua_pushboolean(L, a->nearlyEqual(*b)); |
66 | return 1; |
67 | } |
68 | |
69 | int ColorSpace_get_name(lua_State* L) |
70 | { |
71 | const auto cs = get_obj<gfx::ColorSpace>(L, 1); |
72 | lua_pushstring(L, cs->name().c_str()); |
73 | return 1; |
74 | } |
75 | |
76 | int ColorSpace_set_name(lua_State* L) |
77 | { |
78 | auto cs = get_obj<gfx::ColorSpace>(L, 1); |
79 | if (auto name = lua_tostring(L, 2)) |
80 | cs->setName(name); |
81 | return 0; |
82 | } |
83 | |
84 | const luaL_Reg ColorSpace_methods[] = { |
85 | { "__gc" , ColorSpace_gc }, |
86 | { "__eq" , ColorSpace_eq }, |
87 | { nullptr, nullptr } |
88 | }; |
89 | |
90 | const Property ColorSpace_properties[] = { |
91 | { "name" , ColorSpace_get_name, ColorSpace_set_name }, |
92 | { nullptr, nullptr, nullptr } |
93 | }; |
94 | |
95 | } // anonymous namespace |
96 | |
97 | DEF_MTNAME(gfx::ColorSpace); |
98 | |
99 | void register_color_space_class(lua_State* L) |
100 | { |
101 | using ColorSpace = gfx::ColorSpace; |
102 | REG_CLASS(L, ColorSpace); |
103 | REG_CLASS_NEW(L, ColorSpace); |
104 | REG_CLASS_PROPERTIES(L, ColorSpace); |
105 | } |
106 | |
107 | void push_color_space(lua_State* L, const gfx::ColorSpace& cs) |
108 | { |
109 | push_new<gfx::ColorSpace>(L, cs); |
110 | } |
111 | |
112 | } // namespace script |
113 | } // namespace app |
114 | |