From 4755c703f4b01cd7de01bda0c72686b93e34e120 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 18 Feb 2017 16:11:18 +0100 Subject: move color_to_rgba to LuaAPI for consistency --- libs/ardour/ardour/lua_api.h | 16 ++++++++++++++++ libs/ardour/lua_api.cc | 26 ++++++++++++++++++++++++++ libs/ardour/luabindings.cc | 1 + 3 files changed, 43 insertions(+) diff --git a/libs/ardour/ardour/lua_api.h b/libs/ardour/ardour/lua_api.h index c5ce0986ce..cbbe750164 100644 --- a/libs/ardour/ardour/lua_api.h +++ b/libs/ardour/ardour/lua_api.h @@ -166,6 +166,22 @@ namespace ARDOUR { namespace LuaAPI { */ int hsla_to_rgba (lua_State *lua); + /** + * A convenience function to expand RGBA parameters from an integer + * + * convert a Canvas::Color (uint32_t 0xRRGGBBAA) into + * double RGBA values which can be passed as parameters to + * Cairo::Context::set_source_rgba + * + * Example: + * @code + * local r, g, b, a = ARDOUR.LuaAPI.color_to_rgba (0x88aa44ff) + * cairo_ctx:set_source_rgba (ARDOUR.LuaAPI.color_to_rgba (0x11336699) + * @endcode + * @returns 4 parameters: red, green, blue, alpha (in range 0..1) + */ + int color_to_rgba (lua_State *lua); + /** * Creates a filename from a series of elements using the correct separator for filenames. * diff --git a/libs/ardour/lua_api.cc b/libs/ardour/lua_api.cc index 080ba12841..7da81b2965 100644 --- a/libs/ardour/lua_api.cc +++ b/libs/ardour/lua_api.cc @@ -502,6 +502,32 @@ ARDOUR::LuaAPI::hsla_to_rgba (lua_State *L) return 4; } +int +ARDOUR::LuaAPI::color_to_rgba (lua_State *L) +{ + int top = lua_gettop (L); + if (top < 1) { + return luaL_argerror (L, 1, "invalid number of arguments, color_to_rgba (uint32_t)"); + } + uint32_t color = luabridge::Stack::get (L, 1); + double r, g, b, a; + + /* libardour is no user of libcanvas, otherwise + * we could just call + * ArdourCanvas::color_to_rgba (color, r, g, b, a); + */ + r = ((color >> 24) & 0xff) / 255.0; + g = ((color >> 16) & 0xff) / 255.0; + b = ((color >> 8) & 0xff) / 255.0; + a = ((color >> 0) & 0xff) / 255.0; + + luabridge::Stack ::push (L, r); + luabridge::Stack ::push (L, g); + luabridge::Stack ::push (L, b); + luabridge::Stack ::push (L, a); + return 4; +} + int ARDOUR::LuaAPI::build_filename (lua_State *L) { diff --git a/libs/ardour/luabindings.cc b/libs/ardour/luabindings.cc index 586d3fd99c..51cd397efd 100644 --- a/libs/ardour/luabindings.cc +++ b/libs/ardour/luabindings.cc @@ -1930,6 +1930,7 @@ LuaBindings::common (lua_State* L) .addRefFunction ("get_plugin_insert_param", ARDOUR::LuaAPI::get_plugin_insert_param) .addCFunction ("plugin_automation", ARDOUR::LuaAPI::plugin_automation) .addCFunction ("hsla_to_rgba", ARDOUR::LuaAPI::hsla_to_rgba) + .addCFunction ("color_to_rgba", ARDOUR::LuaAPI::color_to_rgba) .addFunction ("usleep", Glib::usleep) .addCFunction ("build_filename", ARDOUR::LuaAPI::build_filename) .addFunction ("new_noteptr", ARDOUR::LuaAPI::new_noteptr) -- cgit v1.2.3