Clamp values in read_ARGB8

This commit is contained in:
Desour 2023-11-17 18:52:23 +01:00 committed by sfan5
parent 1bc74b0ba1
commit 585e6aa80b

@ -294,19 +294,23 @@ bool read_color(lua_State *L, int index, video::SColor *color)
video::SColor read_ARGB8(lua_State *L, int index) 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); video::SColor color(0);
CHECK_TYPE(index, "ARGB color", LUA_TTABLE); CHECK_TYPE(index, "ARGB color", LUA_TTABLE);
lua_getfield(L, index, "a"); 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_pop(L, 1);
lua_getfield(L, index, "r"); lua_getfield(L, index, "r");
color.setRed(lua_tonumber(L, -1)); color.setRed(clamp_col(lua_tonumber(L, -1)));
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "g"); lua_getfield(L, index, "g");
color.setGreen(lua_tonumber(L, -1)); color.setGreen(clamp_col(lua_tonumber(L, -1)));
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "b"); lua_getfield(L, index, "b");
color.setBlue(lua_tonumber(L, -1)); color.setBlue(clamp_col(lua_tonumber(L, -1)));
lua_pop(L, 1); lua_pop(L, 1);
return color; return color;
} }