diff --git a/doc/en/scripting/ui.md b/doc/en/scripting/ui.md index 091d7ff8..37c316fd 100644 --- a/doc/en/scripting/ui.md +++ b/doc/en/scripting/ui.md @@ -197,6 +197,8 @@ Here, *color* can be specified in the following ways: | data:set_data(data: table) | replaces pixel data (width * height * 4 numbers) | | data:create_texture(name: str) | creates and shares texture to renderer | | 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 | ## Inline frame (iframe) diff --git a/doc/ru/scripting/ui.md b/doc/ru/scripting/ui.md index 10514edb..741c8cf3 100644 --- a/doc/ru/scripting/ui.md +++ b/doc/ru/scripting/ui.md @@ -197,6 +197,8 @@ document["worlds-panel"]:clear() | data:set_data(data: table) | заменяет данные пикселей (ширина * высота * 4 чисел) | | data:create_texture(name: str) | создаёт и делится текстурой с рендерером | | data:unbind_texture() | отвязывает текстуру от холста | +| data:mul(*цвет* или Canvas) | умножает увет на указанный цвет или холст | +| data:add(*цвет* или Canvas) | прибавляет цвет или другой холст к цвету | ## Рамка встраивания (iframe) diff --git a/src/graphics/core/ImageData.cpp b/src/graphics/core/ImageData.cpp index 3a5e3cb6..b69b5f4a 100644 --- a/src/graphics/core/ImageData.cpp +++ b/src/graphics/core/ImageData.cpp @@ -420,6 +420,99 @@ void ImageData::fixAlphaColor() { } } +static void check_matching(const ImageData& a, const ImageData& b) { + if (b.getWidth() != a.getWidth() || + b.getHeight() != a.getHeight() || + b.getFormat() != a.getFormat()) { + throw std::runtime_error("image sizes or formats do not match"); + } +} + +void ImageData::mulColor(const glm::ivec4& color) { + uint comps; + switch (format) { + case ImageFormat::rgb888: comps = 3; break; + case ImageFormat::rgba8888: comps = 4; break; + default: + throw std::runtime_error("only unsigned byte formats supported"); + } + for (uint y = 0; y < height; y++) { + for (uint x = 0; x < width; x++) { + uint idx = (y * width + x) * comps; + for (uint c = 0; c < comps; c++) { + float val = static_cast(data[idx + c]) * color[c] / 255.0f; + data[idx + c] = + static_cast(std::min(std::max(val, 0.0f), 255.0f)); + } + } + } +} + +void ImageData::addColor(const ImageData& other) { + check_matching(*this, other); + + uint comps; + switch (format) { + case ImageFormat::rgb888: comps = 3; break; + case ImageFormat::rgba8888: comps = 4; break; + default: + throw std::runtime_error("only unsigned byte formats supported"); + } + for (uint y = 0; y < height; y++) { + 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]; + data[idx + c] = + static_cast(std::min(std::max(val, 0), 255)); + } + } + } +} + +void ImageData::addColor(const glm::ivec4& color) { + uint comps; + switch (format) { + case ImageFormat::rgb888: comps = 3; break; + case ImageFormat::rgba8888: comps = 4; break; + default: + throw std::runtime_error("only unsigned byte formats supported"); + } + for (uint y = 0; y < height; y++) { + 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]; + data[idx + c] = + static_cast(std::min(std::max(val, 0), 255)); + } + } + } +} + +void ImageData::mulColor(const ImageData& other) { + check_matching(*this, other); + + uint comps; + switch (format) { + case ImageFormat::rgb888: comps = 3; break; + case ImageFormat::rgba8888: comps = 4; break; + default: + throw std::runtime_error("only unsigned byte formats supported"); + } + for (uint y = 0; y < height; y++) { + for (uint x = 0; x < width; x++) { + uint idx = (y * width + x) * comps; + for (uint c = 0; c < comps; c++) { + float val = static_cast(data[idx + c]) * + static_cast(other.data[idx + c]) / 255.0f; + data[idx + c] = + static_cast(std::min(std::max(val, 0.0f), 255.0f)); + } + } + } +} + std::unique_ptr add_atlas_margins(ImageData* image, int grid_size) { // RGBA is only supported assert(image->getFormat() == ImageFormat::rgba8888); diff --git a/src/graphics/core/ImageData.hpp b/src/graphics/core/ImageData.hpp index 86396568..a0e694e3 100644 --- a/src/graphics/core/ImageData.hpp +++ b/src/graphics/core/ImageData.hpp @@ -32,6 +32,10 @@ public: void blit(const ImageData& image, int x, int y); void extrude(int x, int y, int w, int h); void fixAlphaColor(); + void mulColor(const glm::ivec4& color); + void mulColor(const ImageData& other); + void addColor(const glm::ivec4& color); + void addColor(const ImageData& other); 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 40af6e81..8adb558c 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_canvas.cpp @@ -240,6 +240,34 @@ static int l_create_texture(State* L) { return 0; } +static int l_mul(State* L) { + auto canvas = touserdata(L, 1); + if (canvas == nullptr) { + return 0; + } + if (lua::isnumber(L, 2)) { + RGBA rgba = get_rgba(L, 2); + canvas->getData().mulColor(glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a}); + } else if (auto other = touserdata(L, 2)) { + canvas->getData().mulColor(other->getData()); + } + return 0; +} + +static int l_add(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}); + } else if (auto other = touserdata(L, 2)) { + canvas->getData().addColor(other->getData()); + } + return 0; +} + static std::unordered_map methods { {"at", lua::wrap}, {"set", lua::wrap}, @@ -249,6 +277,8 @@ static std::unordered_map methods { {"update", lua::wrap}, {"create_texture", lua::wrap}, {"unbind_texture", lua::wrap}, + {"mul", lua::wrap}, + {"add", lua::wrap}, {"_set_data", lua::wrap}, };