簡體   English   中英

使用 Xlib 更改繪圖顏色

[英]Changing the drawing color using Xlib

我正在使用 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 來更改顏色?

您需要使用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 設備的舊語法,但不鼓勵繼續使用。 語法是一個初始的尖號字符,后跟一個數字規范,采用以下格式之一:

所有顏色更改都是針對某個 GC 完成的。 然后使用該 GC 進行繪圖。 是的XSetForeground是最方便的方法。

如果您有一些經常使用的顏色,則可以有多個 GC。

//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 );

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM