add canvas:sub method

This commit is contained in:
MihailRis
2025-11-11 19:34:40 +03:00
parent 83f7bf80c4
commit 34d13442b6
5 changed files with 25 additions and 8 deletions
+4 -4
View File
@@ -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<ubyte>(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<ubyte>(std::min(std::max(val, 0), 255));
}
+2 -2
View File
@@ -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<ImageData> cropped(int x, int y, int width, int height) const;
@@ -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<LuaCanvas>(L, 2)) {
canvas->getData().addColor(other->getData());
canvas->getData().addColor(other->getData(), 1);
}
return 0;
}
static int l_sub(State* L) {
auto canvas = touserdata<LuaCanvas>(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<LuaCanvas>(L, 2)) {
canvas->getData().addColor(other->getData(), -1);
}
return 0;
}
@@ -279,6 +293,7 @@ static std::unordered_map<std::string, lua_CFunction> methods {
{"unbind_texture", lua::wrap<l_unbind_texture>},
{"mul", lua::wrap<l_mul>},
{"add", lua::wrap<l_add>},
{"sub", lua::wrap<l_sub>},
{"_set_data", lua::wrap<l_set_data>},
};