1

在很棒的 wm 3.5 中,您可以使用 cairo 创建自定义小部件来绘制其视觉效果。我想要一个显示单色 PNG 图标的小部件(就像 wibox.widget.imagebox 一样)并允许快速更改其颜色。我尝试在 wibox.widget.imagebox 的绘制函数中修改几行

local cairo = require("lgi").cairo

--- Draw an imagebox with the given cairo context in the given geometry.
function imagebox:draw(wibox, cr, width, height)
    if not self._image then return end
    if width == 0 or height == 0 then return end

    cr:save()

    if not self.resize_forbidden then
        -- Let's scale the image so that it fits into (width, height)
        local w = self._image:get_width()
        local h = self._image:get_height()
        local aspect = width / w
        local aspect_h = height / h
        if aspect > aspect_h then aspect = aspect_h end

        cr:scale(aspect, aspect)
    end    

    -- Here is my modifications
    cr:set_source_surface(self._image, 0, 0)
    cr:paint()
    cr:set_operator(cairo.Operator.IN)
    cr:set_source_rgba(0, 0, 1, 0.5)
    cr:paint()
    -- End of my my modifications

    -- This is original draw code how it was
    --cr:set_source_surface(self._image, 0, 0)
    --cr:paint()

    cr:restore()
end

但它不起作用。我尝试设置其他几个 cairo 的合成运算符,但大多数都没有按预期工作。错误的重叠区域和黑色区域,而不是 wibox 背景颜色。SOURCE 和 OVER 只能正常工作。我在哪里犯错了?

4

1 回答 1

0

错误在于你对cairo绘画方法的理解。黑色/透明正是 IN 在它没有触及的地方留下的东西。换句话说,您首先在背景上绘制了其他东西,因此背景丢失了。

试试这个:

local pat = require("lgi").cairo.Pattern
cr:set_source_rgba(0, 0, 1, 0.5)
cr:mask(pat.create_for_surface(self._image), 0, 0)
于 2014-07-24T10:34:55.903 回答