fix(lib: gears: color): use surface for recolor

This commit is contained in:
actionless 2015-08-06 18:54:06 +02:00 committed by Daniel Hahler
parent 9c750ede63
commit e023fd0640
2 changed files with 26 additions and 1 deletions

View File

@ -286,9 +286,13 @@ function color.create_opaque_pattern(col)
-- gradients can do weird self-intersections) -- gradients can do weird self-intersections)
end 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) function color.recolor_image(image, new_color)
if type(image) == 'string' then if type(image) == 'string' then
image = cairo.ImageSurface.create_from_png(image) image = surface.duplicate_surface(image)
end end
local cr = cairo.Context.create(image) local cr = cairo.Context.create(image)
cr:set_source(color.create_pattern(new_color)) cr:set_source(color.create_pattern(new_color))

View File

@ -70,6 +70,27 @@ function surface.get_size(surf)
return w - x, h - y return w - x, h - y
end 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) return setmetatable(surface, surface.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80