From 585e6aa80ba9ee711aebcf3630ab8bbec1e446fd Mon Sep 17 00:00:00 2001 From: Desour Date: Fri, 17 Nov 2023 18:52:23 +0100 Subject: [PATCH] Clamp values in read_ARGB8 --- src/script/common/c_converter.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index 22aa1c5d7..7924b9fc2 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -294,19 +294,23 @@ bool read_color(lua_State *L, int index, video::SColor *color) video::SColor read_ARGB8(lua_State *L, int index) { + auto clamp_col = [](double c) -> u32 { + return std::fmax(0.0, std::fmin(255.0, c)); + }; + video::SColor color(0); CHECK_TYPE(index, "ARGB color", LUA_TTABLE); lua_getfield(L, index, "a"); - color.setAlpha(lua_isnumber(L, -1) ? lua_tonumber(L, -1) : 0xFF); + color.setAlpha(lua_isnumber(L, -1) ? clamp_col(lua_tonumber(L, -1)) : 0xFF); lua_pop(L, 1); lua_getfield(L, index, "r"); - color.setRed(lua_tonumber(L, -1)); + color.setRed(clamp_col(lua_tonumber(L, -1))); lua_pop(L, 1); lua_getfield(L, index, "g"); - color.setGreen(lua_tonumber(L, -1)); + color.setGreen(clamp_col(lua_tonumber(L, -1))); lua_pop(L, 1); lua_getfield(L, index, "b"); - color.setBlue(lua_tonumber(L, -1)); + color.setBlue(clamp_col(lua_tonumber(L, -1))); lua_pop(L, 1); return color; }