From d4d18c99be9e8c252ae08632908c17c6b356f92a Mon Sep 17 00:00:00 2001 From: Gaspar Capello Date: Mon, 10 Apr 2023 10:27:20 -0300 Subject: [PATCH] [lua] Add Image:clear(Rectangle, color) overload (fix #3799) We can use: Image:clear() Image:clear(color) Image:clear(rectangle) Image:clear(rectangle, color) If the color is not specified it will be the transparent color of the image. Co-authored-by: David Capello --- src/app/script/image_class.cpp | 22 +++++++++++++++++----- tests/scripts/image.lua | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/app/script/image_class.cpp b/src/app/script/image_class.cpp index 2cf28683f..3dae4672a 100644 --- a/src/app/script/image_class.cpp +++ b/src/app/script/image_class.cpp @@ -220,13 +220,25 @@ int Image_clear(lua_State* L) auto obj = get_obj(L, 1); auto img = obj->image(L); doc::color_t color; - if (lua_isnone(L, 2)) + gfx::Rect rc; + int i = 2; + + if (auto rcPtr = may_get_obj(L, i)) { + rc = *rcPtr; + ++i; + } + else { + rc = img->bounds(); // Clear the whole image + } + + if (lua_isnone(L, i)) color = img->maskColor(); - else if (lua_isinteger(L, 2)) - color = lua_tointeger(L, 2); + else if (lua_isinteger(L, i)) + color = lua_tointeger(L, i); else - color = convert_args_into_pixel_color(L, 2, img->pixelFormat()); - doc::clear_image(img, color); + color = convert_args_into_pixel_color(L, i, img->pixelFormat()); + + doc::fill_rect(img, rc, color); // Clips the rectangle to the image bounds return 0; } diff --git a/tests/scripts/image.lua b/tests/scripts/image.lua index aafd48172..cc72897bf 100644 --- a/tests/scripts/image.lua +++ b/tests/scripts/image.lua @@ -41,6 +41,27 @@ do assert(not a:isEmpty()) end +-- Clear +do + local spec = ImageSpec{ + width=2, height=2, + colorMode=ColorMode.INDEXED, + transparentColor=1 } + + local img = Image(spec) + img:clear() + expect_img(img, { 1, 1, + 1, 1 }) + + img:clear(img.bounds) + expect_img(img, { 1, 1, + 1, 1 }) + + img:clear(Rectangle(1, 0, 1, 2), 2) + expect_img(img, { 1, 2, + 1, 2 }) +end + -- Clone do local c = Image(a)