diff --git a/lua.lyx b/lua.lyx index d9906147..e2f52c56 100644 --- a/lua.lyx +++ b/lua.lyx @@ -1480,6 +1480,31 @@ Draw string with custom font to screen. \end_layout +\begin_layout Subsection +gui.adjust_transparency: Adjust transparency of DBITMAP or PALETTE +\end_layout + +\begin_layout Itemize +Syntax: none gui.adjust_transparency(DBITMAP obj, number adj) +\end_layout + +\begin_layout Itemize +Syntax: none gui.adjust_transparency(PALETTE obj, number adj) +\end_layout + +\begin_layout Standard +Multiply alpha channel of by /256. + Useful for making +\begin_inset Quotes eld +\end_inset + +ghosts +\begin_inset Quotes erd +\end_inset + + out of solid bitmaps. +\end_layout + \begin_layout Standard \begin_inset Newpage pagebreak \end_inset diff --git a/lua.pdf b/lua.pdf index e08e4349..78207120 100644 Binary files a/lua.pdf and b/lua.pdf differ diff --git a/src/lua/gui-bitmap.cpp b/src/lua/gui-bitmap.cpp index 13935822..3c0a33dd 100644 --- a/src/lua/gui-bitmap.cpp +++ b/src/lua/gui-bitmap.cpp @@ -492,6 +492,37 @@ namespace messages << "Color #" << (i++) << ": " << c.orig << ":" << c.origa << std::endl; return 0; }); + + inline premultiplied_color tadjust(premultiplied_color c, uint16_t adj) + { + uint32_t rgb = c.orig; + uint32_t a = c.origa; + a = (a * adj) >> 8; + if(a > 256) + a = 256; + if(a == 0) + return premultiplied_color(-1); + else + return premultiplied_color(rgb | ((uint32_t)(256 - a) << 24)); + } + + function_ptr_luafun adjust_trans(LS, "gui.adjust_transparency", [](lua_state& L, const std::string& fname) + -> int { + uint16_t tadj = L.get_numeric_argument(2, fname.c_str()); + if(lua_class::is(L, 1)) { + lua_dbitmap* b = lua_class::get(L, 1, fname.c_str()); + for(auto& c : b->pixels) + c = tadjust(c, tadj); + } else if(lua_class::is(L, 1)) { + lua_palette* p = lua_class::get(L, 1, fname.c_str()); + for(auto& c : p->colors) + c = tadjust(c, tadj); + } else { + throw std::runtime_error("Expected BITMAP or PALETTE as argument 1 for " + "gui.adjust_transparency"); + } + return 2; + }); } DECLARE_LUACLASS(lua_palette, "PALETTE");