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)