From 0fe6827daeb52d9de900204bf59c305d23155425 Mon Sep 17 00:00:00 2001 From: Zughy <63455151+Zughy@users.noreply.github.com> Date: Sat, 20 Dec 2025 15:14:08 +0100 Subject: [PATCH] Add alpha channel in HUD text elements (#16730) --- doc/lua_api.md | 7 +++++-- src/client/hud.cpp | 13 ++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/doc/lua_api.md b/doc/lua_api.md index 03bd1d692..c0418fabf 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -1851,8 +1851,11 @@ Displays text on the HUD. * `text`: The text to be displayed in the HUD element. Supports `core.translate` (always) and `core.colorize` (since protocol version 44) -* `number`: An integer containing the RGB value of the color used to draw the - text. Specify `0xFFFFFF` for white text, `0xFF0000` for red, and so on. +* `number`: An integer containing the (A)RGB value of the color used to draw the + text. Specify `0xFFFFFF` for white text, `0x80FF0000` for semi-transparent red, and so on. + * Alpha only works on Luanti 5.15+ clients. Older clients will see the text as opaque. + * To completely hide a text, set `text` to `""`. Setting the alpha value to `00` + will not work due to compatibility reasons (it'll be treated as `FF`). * `alignment`: The alignment of the text. * `offset`: offset in pixels from position. * `size`: size of the text. diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 4155ce7cf..cffc3dba4 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -394,9 +394,16 @@ void Hud::drawLuaElements(const v3s16 &camera_offset) if (textfont->getType() == gui::EGFT_CUSTOM) ttfont = static_cast(textfont); - video::SColor color(255, (e->number >> 16) & 0xFF, - (e->number >> 8) & 0xFF, - (e->number >> 0) & 0xFF); + u32 num = e->number; + u8 alpha = (num >> 24) & 0xFF; + if (alpha == 0) + alpha = 0xFF; // Backwards compatibility + + video::SColor color = video::SColor(alpha, + (num >> 16) & 0xFF, + (num >> 8) & 0xFF, + (num >> 0) & 0xFF); + EnrichedString text(unescape_string(utf8_to_wide(e->text)), color); core::dimension2d textsize = textfont->getDimension(text.c_str());