问题描述
我想在 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-multiplied 颜色,即真正的不透明颜色需要乘以 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 中创建半透明的白色窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!