You are not logged in.

#1 2013-07-01 17:17:05

wujek.srujek
Member
Registered: 2013-06-12
Posts: 38

conky - 'really' transparrent background but not text / images

Hi. I'm using openbox, conky-lua (from AUR), compmgr (from official repos) and a custom background script (code below).
I would like to ask if it is possible to have really transparent background in conky (with or without lua), but have the text / bars / images (the 'foreground') be fully opaque? I can't do it. Here are the files:
.conkyrc (this is actually not my setup but rather a minimal config that lets see the problem easily):

alignment top_left
gap_x 700
gap_y 500
update_interval 0.1
minimum_size 200 200
own_window yes
own_window_argb_visual yes
own_window_argb_value 0
double_buffer yes
default_color bbbbbb

lua_load ~/Bin/conky_bg.lua
lua_draw_hook_pre draw_bg

TEXT
${battery_bar 100}
${image some_image.png -p 20,100}

conky_bg.lua (found somewhere on the net and customized):

require 'cairo'

corner_r = 10
bg_color = 0x000000
bg_alpha = 0.2
border_color = 0xffffff
border_alpha = 0.25
border_width = 1.0

function rgb_to_r_g_b(color, alpha)
  return ((color / 0x10000) % 0x100) / 255., ((color / 0x100) % 0x100) / 255., (color % 0x100) / 255., alpha
end

function conky_draw_bg()
    if conky_window == nil then return end
    local w = conky_window.width
    local h = conky_window.height

    local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, w, h)
    local cr = cairo_create(cs)
    
    cairo_move_to(cr, corner_r, 0)
    cairo_line_to(cr, w - corner_r, 0)
    cairo_curve_to(cr, w, 0, w, 0, w, corner_r)
    cairo_line_to(cr, w, h - corner_r)
    cairo_curve_to(cr, w, h, w, h, w - corner_r, h)
    cairo_line_to(cr, corner_r, h)
    cairo_curve_to(cr, 0, h, 0, h, 0, h - corner_r)
    cairo_line_to(cr, 0, corner_r)
    cairo_curve_to(cr, 0, 0, 0, 0, corner_r, 0)
    cairo_close_path(cr)
    
    cairo_set_source_rgba(cr, rgb_to_r_g_b(bg_color, bg_alpha))
    cairo_fill_preserve(cr)

    cairo_set_line_width(cr, border_width)
    cairo_set_source_rgba(cr, rgb_to_r_g_b(border_color, border_alpha))
    cairo_stroke(cr)

    cairo_destroy(cr)
    cairo_surface_destroy(cs)
end

The combinations that I've tested:
1. own_window_argb_visual yes and own_window_argb_value 0 - everything transparent including 'foreground' (both bar and image)
2. own_window_argb_visual yes and own_window_argb_value 255 - everything opaque, no transparency
3. own_window_argb_visual yes own_window_transparent yes, #own_window_argb_value (commented out) - background and images with transparency, bar opaque
4. own_window_argb_visual no, own_window_transparent yes - fake background transparency, opaque image and bar

#4 is the closest one to what I would like to achieve, but uses fake transparency, and it looks weird when I move the window above other windows - I see the background, not the window that is under conky (that's the fake in fake transparency ;d) - and I do do it sometimes.

Is there any combination of settings that would let me achieve what I want?

wujek

Offline

#2 2013-07-01 18:38:28

Roken
Member
From: South Wales, UK
Registered: 2012-01-16
Posts: 1,372

Re: conky - 'really' transparrent background but not text / images

I have similar frustrations (xfce, but I don't think it matters) - the only way I could do it was:

#Setup Windows
own_window yes
#own_window_type conky
own_window_argb_visual no 
#own_window_argb_value 0 
own_window_title conky
own_window_transparent yes 
own_window_class conky-semi
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
double_buffer yes

Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Fractal Design Define 7 XL, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703

/ is the root of all problems.

Offline

#3 2013-07-02 11:36:40

wujek.srujek
Member
Registered: 2013-06-12
Posts: 38

Re: conky - 'really' transparrent background but not text / images

Hi. Yes, that's seems pretty much as my #4. I read somewhere else that it is simply the way it is, and if I don't want real background transparency and no images transparency I need to paint the images myself in lua. Fair enough, this can be done. Most of the time, though, this 'fake' transparency will be good enough for me, so I will leave it at that.
Thanks.

Offline

#4 2013-07-04 08:23:31

wujek.srujek
Member
Registered: 2013-06-12
Posts: 38

Re: conky - 'really' transparrent background but not text / images

Hi again. I had some time the other day to look into it, and here is what I came up with:

function image(cr, file, setup)
  cairo_save(cr)
  local cs = cairo_image_surface_create_from_png(file)
  local x = setup.x or 0
  local y = setup.y or 0
  if setup.w or setup.h then -- want horizontal or vertical scaling
    local w =  cairo_image_surface_get_width(cs)
    local h =  cairo_image_surface_get_height(cs)
    local w_ratio = setup.w and setup.w / w or 1
    local h_ratio = setup.h and setup.h / h or 1
    x = x / w_ratio
    y = y / h_ratio
    cairo_scale(cr, w_ratio, h_ratio)
  end
  cairo_set_source_surface(cr, cs, x, y)
  cairo_paint_with_alpha(cr, setup.a or 1)
  cairo_restore(cr)
  cairo_surface_destroy(cs)
end

The setup is a map (I think it is called a table in lua) that can contain the following attributes:

  1. x, y - where the top-left image corner of the image should be, defaults to 0, 0

  2. w, h - the width and height of the image, default to real image dimensions; this can be useful to scale the image

  3. a - the alpha channel, defaults to 1 (fully opaque, the original problem that made me start this topic)

They all can be specified in various combinations. A few examples how you call it (let's assume the image is 100 pixels in width and height):

local file = "/home/wujek/Playground/dot.png"

image(cr, file, {}) -- put the image at (0, 0), full size, opaque
image(cr, file, {y=100,w=50, h=50}) -- put image at (0,100), scale down to half the size in both dimensions
image(cr, file, {x=50,y=50,w=200, h=200}) -- put image at (50, 50), scale up to twice the size in both directions
image(cr, file, {w=300, h=50, a=0.5}) -- put image at (0, 0), scale width up to three times the size, scale height down to half the size, half-translucent
image(cr, file, {a=0}) -- put image at (0, 0), full size, but fully transparent so invisible anyways; can come in handy to take some place and be shown only under certain conditions and prevent conky from resizing (I've just came up with the use case ;d)

This works for me just fine, but it has a few problems:

  • it only supports *.png images, but this shouldn't be that much of a problem, as one can convert from other types with gimp / whatever

  • it uses cairo_image_surface_create_from_png() which Cairo describes as 'toy API' not ready for production (see text under 'Destription' at http://cairographics.org/manual/cairo-PNG-Support.html )

  • both issues should be fixed in conky 2.0 when it comes out - see https://github.com/jrk-/conky/commit/56 … 179285667d; this code adds a wrapper function to conky lua scripts that can read about any picture format supported by imlib2[\*]

Currently there is a problem in the conky / lua / tolua++ / cairo that prevents doing such image operations ourselves, as far as I understand it after my tests. The cairo_image_surface_create_for_data() takes the data as unsigned char*, which tolua++ converts to a string, whereas is is in reality a chunk of bytes; I have failed to call this function correctly (I have no idea how tolua++ works, but this is as much as I could hather after taking a look at the generated). Lua in conky will always complain that it expects a string and not a userdata (a chunk of raw bytes) and hence fail. That's why the wrapper is needed. The problem with this solution is: it doesn't seem to always work for me, but conky 2 is not ready yet so there may be some other issues that make the code work weird for me (scaling down / up messes transparent image background and the like); and secondly, I think it does't allow one to paint the image with alpha, but this should be easy enough to add to the function (one more parameter). I contacted the conky author about all these issues, let's see if and what he answers.

Nonetheless, the above code works fine for me so maybe it will come in handy for others as well. I must also say that I learned Lua just for conky, so this code may be written much better by pros - feel free to comment on it and make it better.

wujek

Offline

Board footer

Powered by FluxBB