diff --git a/lib/gears/color.lua b/lib/gears/color.lua index d513e65b..1576d77b 100644 --- a/lib/gears/color.lua +++ b/lib/gears/color.lua @@ -286,9 +286,13 @@ function color.create_opaque_pattern(col) -- gradients can do weird self-intersections) end +--- Fill non-transparent area of an image with a given color. +-- @param image Image or path to it. +-- @param new_color New color. +-- @return Recolored image. function color.recolor_image(image, new_color) if type(image) == 'string' then - image = cairo.ImageSurface.create_from_png(image) + image = surface.duplicate_surface(image) end local cr = cairo.Context.create(image) cr:set_source(color.create_pattern(new_color)) diff --git a/lib/gears/surface.lua b/lib/gears/surface.lua index 946f7508..e4042638 100644 --- a/lib/gears/surface.lua +++ b/lib/gears/surface.lua @@ -70,6 +70,27 @@ function surface.get_size(surf) return w - x, h - y end + +--- Get the duplicate of a cairo surface +-- @param s Source surface. +-- @return The surface's duplicate. +function surface.duplicate_surface(s) + s = surface.load(s) + + -- Figure out surface size (this does NOT work for unbounded recording surfaces) + local cr = cairo.Context(s) + local x, y, w, h = cr:clip_extents() + + -- Create a copy + local result = s:create_similar(s.content, w - x, h - y) + cr = cairo.Context(result) + cr:set_source_surface(s, 0, 0) + cr.operator = cairo.Operator.SOURCE + cr:paint() + return result +end + + return setmetatable(surface, surface.mt) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80