问题描述
我想在XLib中创建一个半透明的白色窗口,但是该窗口不是半透明的,它仍然是完全不透明的。我使用compton合成器,并且系统中有透明的窗口,因此问题出在代码中:
I would like to create a semi transparent white window in XLib, but the window is not semi translucent, it remains fully opaque. I use the compton compositor and there are transparent windows in the system, so the problem is in the code:
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
Display* display = XOpenDisplay(NULL);
XVisualInfo vinfo;
XMatchVisualInfo(display, DefaultScreen(display), 32, TrueColor, &vinfo);
XSetWindowAttributes attr;
attr.colormap = XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
attr.border_pixel = 0;
attr.background_pixel = 0x80ffffff;
Window win = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 300, 200, 0, vinfo.depth, InputOutput, vinfo.visual, CWColormap | CWBorderPixel | CWBackPixel, &attr);
XSelectInput(display, win, StructureNotifyMask);
GC gc = XCreateGC(display, win, 0, 0);
Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", 0);
XSetWMProtocols(display, win, &wm_delete_window, 1);
XMapWindow(display, win);
int keep_running = 1;
XEvent event;
while (keep_running) {
XNextEvent(display, &event);
switch(event.type) {
case ClientMessage:
if (event.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", 1) && (Atom)event.xclient.data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", 1))
keep_running = 0;
break;
default:
break;
}
}
XDestroyWindow(display, win);
XCloseDisplay(display);
return 0;
}
推荐答案
X11期望 pre相乘的颜色,即真正的不透明颜色需要乘以alpha值(并相应缩放,即当通道宽度为8位时除以256)。当您需要组合多个级别时,此格式更易于使用。请参见的公式。
X11 expects pre-multiplied colours, i.e. real opaque colours need to be multiplied by the alpha value (and scaled accordingly, i.e. divided by 256 when channel widths is 8 bits). This format is easier to work with when you need to combine many levels. See formulas here. There's less computation when everything is pre-multiplied.
因此,您需要将每个R,G和B通道乘以alpha值(0x80),然后除以256,从而减少了计算量。
So you need to multiply each of your R, G and B channels by the alpha value (0x80) and divide by 256.
将背景设置为0x80808080即可得到所需的结果:
Setting the background to 0x80808080 gives the desired result:
请注意,结果与@patthoyts所建议的不同:此处仅窗口是半透明的,WM装饰保持不透明在那里,适当的窗户和装饰都由WM透明化了(WM进行了必要的颜色混合)。
Note the result is different from what @patthoyts suggests: here only the window proper is semi-transparent, the WM decoration stays opaque; there both the window proper and the decoration are made transparent by the WM (and the WM does the necessary colour blending).
这篇关于如何在XLib中创建半透明的白色窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!