【问题标题】:Change icon color by means of awesome wm通过真棒 wm 更改图标颜色
【发布时间】:2014-07-24 18:34:55
【问题描述】:

在很棒的 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 只能正常工作。我哪里出错了?

【问题讨论】:

    标签: lua cairo awesome-wm


    【解决方案1】:

    错误在于你对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)
    

    【讨论】:

      猜你喜欢
      • 2018-09-26
      • 2017-02-19
      • 2014-11-04
      • 2016-09-19
      • 1970-01-01
      • 2016-04-08
      • 2019-02-12
      • 2021-10-24
      • 1970-01-01
      相关资源
      最近更新 更多