diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
commit | 6352e8e3196f78388b6c771073f9e03eaa612673 (patch) | |
tree | e23772f79a7fbc41bc9108951e9e136857484bf4 /source/core/StarLuaConverters.cpp | |
parent | 6741a057e5639280d85d0f88ba26f000baa58f61 (diff) |
everything everywhere
all at once
Diffstat (limited to 'source/core/StarLuaConverters.cpp')
-rw-r--r-- | source/core/StarLuaConverters.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/source/core/StarLuaConverters.cpp b/source/core/StarLuaConverters.cpp new file mode 100644 index 0000000..e09876a --- /dev/null +++ b/source/core/StarLuaConverters.cpp @@ -0,0 +1,42 @@ +#include "StarLuaConverters.hpp" +#include "StarColor.hpp" + +namespace Star { + +LuaValue LuaConverter<Color>::from(LuaEngine& engine, Color const& c) { + if (c.alpha() == 255) + return engine.createArrayTable(initializer_list<uint8_t>{c.red(), c.green(), c.blue()}); + else + return engine.createArrayTable(initializer_list<uint8_t>{c.red(), c.green(), c.blue(), c.alpha()}); +} + +Maybe<Color> LuaConverter<Color>::to(LuaEngine& engine, LuaValue const& v) { + if (auto t = v.ptr<LuaTable>()) { + Color c = Color::rgba(0, 0, 0, 255); + Maybe<int> r = engine.luaMaybeTo<int>(t->get(1)); + Maybe<int> g = engine.luaMaybeTo<int>(t->get(2)); + Maybe<int> b = engine.luaMaybeTo<int>(t->get(3)); + if (!r || !g || !b) + return {}; + + c.setRed(*r); + c.setGreen(*g); + c.setBlue(*b); + + if (Maybe<int> a = engine.luaMaybeTo<int>(t->get(4))) { + if (!a) + return {}; + c.setAlpha(*a); + } + + return c; + } else if (auto s = v.ptr<LuaString>()) { + try { + return Color(s->ptr()); + } catch (ColorException) {} + } + + return {}; +} + +} |