diff --git a/doc/en/scripting/ui.md b/doc/en/scripting/ui.md index 37c316fd..80750501 100644 --- a/doc/en/scripting/ui.md +++ b/doc/en/scripting/ui.md @@ -199,6 +199,7 @@ Here, *color* can be specified in the following ways: | data:unbind_texture() | unbinds the texture from the canvas | | data:mul(*color* or Canvas) | multiplies a color by the specified color or canvas | | data:add(*color* or Canvas) | adds a color or another canvas to a color | +| data:sub(*color* or Canvas) | subtracts a color or another canvas to a color | ## Inline frame (iframe) diff --git a/doc/ru/scripting/ui.md b/doc/ru/scripting/ui.md index 741c8cf3..c6c769ac 100644 --- a/doc/ru/scripting/ui.md +++ b/doc/ru/scripting/ui.md @@ -199,6 +199,7 @@ document["worlds-panel"]:clear() | data:unbind_texture() | отвязывает текстуру от холста | | data:mul(*цвет* или Canvas) | умножает увет на указанный цвет или холст | | data:add(*цвет* или Canvas) | прибавляет цвет или другой холст к цвету | +| data:sub(*цвет* или Canvas) | вычитает цвет или другой холст к цвету | ## Рамка встраивания (iframe) diff --git a/src/graphics/core/ImageData.cpp b/src/graphics/core/ImageData.cpp index b69b5f4a..bd243832 100644 --- a/src/graphics/core/ImageData.cpp +++ b/src/graphics/core/ImageData.cpp @@ -448,7 +448,7 @@ void ImageData::mulColor(const glm::ivec4& color) { } } -void ImageData::addColor(const ImageData& other) { +void ImageData::addColor(const ImageData& other, int multiplier) { check_matching(*this, other); uint comps; @@ -462,7 +462,7 @@ void ImageData::addColor(const ImageData& other) { for (uint x = 0; x < width; x++) { uint idx = (y * width + x) * comps; for (uint c = 0; c < comps; c++) { - int val = data[idx + c] + other.data[idx + c]; + int val = data[idx + c] + other.data[idx + c] * multiplier; data[idx + c] = static_cast(std::min(std::max(val, 0), 255)); } @@ -470,7 +470,7 @@ void ImageData::addColor(const ImageData& other) { } } -void ImageData::addColor(const glm::ivec4& color) { +void ImageData::addColor(const glm::ivec4& color, int multiplier) { uint comps; switch (format) { case ImageFormat::rgb888: comps = 3; break; @@ -482,7 +482,7 @@ void ImageData::addColor(const glm::ivec4& color) { for (uint x = 0; x < width; x++) { uint idx = (y * width + x) * comps; for (uint c = 0; c < comps; c++) { - int val = data[idx + c] + color[c]; + int val = data[idx + c] + color[c] * multiplier; data[idx + c] = static_cast(std::min(std::max(val, 0), 255)); } diff --git a/src/graphics/core/ImageData.hpp b/src/graphics/core/ImageData.hpp index a0e694e3..a1597a98 100644 --- a/src/graphics/core/ImageData.hpp +++ b/src/graphics/core/ImageData.hpp @@ -34,8 +34,8 @@ public: void fixAlphaColor(); void mulColor(const glm::ivec4& color); void mulColor(const ImageData& other); - void addColor(const glm::ivec4& color); - void addColor(const ImageData& other); + void addColor(const glm::ivec4& color, int multiplier); + void addColor(const ImageData& other, int multiplier); std::unique_ptr cropped(int x, int y, int width, int height) const; diff --git a/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp b/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp index 8adb558c..b93b52c6 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp @@ -261,9 +261,23 @@ static int l_add(State* L) { } if (lua::istable(L, 2)) { RGBA rgba = get_rgba(L, 2); - canvas->getData().addColor(glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a}); + canvas->getData().addColor(glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a}, 1); } else if (auto other = touserdata(L, 2)) { - canvas->getData().addColor(other->getData()); + canvas->getData().addColor(other->getData(), 1); + } + return 0; +} + +static int l_sub(State* L) { + auto canvas = touserdata(L, 1); + if (canvas == nullptr) { + return 0; + } + if (lua::istable(L, 2)) { + RGBA rgba = get_rgba(L, 2); + canvas->getData().addColor(glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a}, -1); + } else if (auto other = touserdata(L, 2)) { + canvas->getData().addColor(other->getData(), -1); } return 0; } @@ -279,6 +293,7 @@ static std::unordered_map methods { {"unbind_texture", lua::wrap}, {"mul", lua::wrap}, {"add", lua::wrap}, + {"sub", lua::wrap}, {"_set_data", lua::wrap}, };