3

我正在使用 Xlib 编写应用程序。我这样设置窗口的前景:

XSetForeground (dpy, gc, WhitePixel (dpy, scr));

但现在我需要将绘图颜色更改为其他颜色,我首先想这样做:

void update_window (Display* d, Window w, GC gc, Colormap cmap) 
{
    XWindowAttributes winatt;
    XColor bcolor;
    char bar_color[] = "#4E4E4E";

    XGetWindowAttributes (d, w, &winatt);

    XParseColor(d, cmap, bar_color, &bcolor);
    XAllocColor(d, cmap, &bcolor);

    // Draws the menu bar.
    XFillRectangle (d, w, gc, 0, 0, winatt.width, 30);

    XFreeColormap (d, cmap);
}

但这不起作用。那么 XParseColor 和 XAllocColor 做了什么?我是否需要再次使用 XSetForeground 来更改颜色?

4

3 回答 3

4

你需要使用XSetForeground. 尝试这样的事情:

XColor xcolour;

// I guess XParseColor will work here
xcolour.red = 32000; xcolour.green = 65000; xcolour.blue = 32000;
xcolour.flags = DoRed | DoGreen | DoBlue;
XAllocColor(d, cmap, &xcolour);

XSetForeground(d, gc, xcolour.pixel);
XFillRectangle(d, w, gc, 0, 0, winatt.width, 30);
XFlush(d);

另外,我认为您不能使用该颜色字符串。看看这个页面:

数字颜色规范由颜色空间名称和一组值组成,语法如下:

<color_space_name>:<value>/.../<value>

以下是有效颜色字符串的示例。

"CIEXYZ:0.3227/0.28133/0.2493"
"RGBi:1.0/0.0/0.0"
"rgb:00/ff/00"
"CIELuv:50.0/0.0/0.0"

编辑/更新:正如@JoL 在评论中提到的,您仍然可以使用旧语法,但不鼓励使用

为了向后兼容,支持旧的 RGB 设备语法,但不鼓励继续使用。语法是一个初始的尖锐符号字符,后跟一个数字规范,采用以下格式之一:

于 2013-09-18T17:18:39.127 回答
2
//I write additional function _RGB(...) where r,g,b is components in range 0...255
unsigned long _RGB(int r,int g, int b)
{
    return b + (g<<8) + (r<<16);
}


void some_fun()
{
  //sample set color, where r=255 g=0 b=127
  XSetForeground(display, gc, _RGB(255,0,127));

  //draw anything
  XFillRectangle( display, window, gc, x, y, len, hei );

}
于 2020-02-04T13:19:24.327 回答
1

所有颜色变化都是针对某个 GC 完成的。然后将该 GC 用于绘图。是的XSetForeground的,这是最方便的方法。

如果您有少量经常使用的颜色,则可以有多个 GC。

于 2013-09-18T17:18:12.953 回答