[Xcb] Setting colors and drawing

Uli Schlachter psychon at znc.in
Fri Mar 13 15:15:20 UTC 2020


Hi,

On 12.03.20 19:18, Spam Hater wrote:
> How to set color other than black?

You are looking for the AllocColor request:

https://www.x.org/releases/X11R7.6/doc/xproto/x11protocol.html#requests:AllocColor

You provide this with the amount of red/green/blue that you want (but as
16 bit value, so e.g. "full red" is red=0xffff) and in the reply you get
back a "pixel" value that you can use. If you only need few colors, this
is the easiest way to go.

Alternatively, you can read up on Visuals, e.g. here:
https://tronche.com/gui/x/xlib/window/visual-types.html

Most visuals that you will come across will be DirectColor or TrueColor.
For these, the xcb_visual_info_t contains some information on how to
create "pixel" values yourself.

For example, let's look at a randomly picked example (output is from the
program "xdpyinfo" that you will most likely also have on your own
computer):

  visual:
    visual id:    0x17a
    class:    DirectColor
    depth:    24 planes
    available colormap entries:    256 per subfield
    red, green, blue masks:    0xff0000, 0xff00, 0xff
    significant bits in color specification:    8 bits

This visual is "called" 0x17a. In your example program, you are using
screen->root_visual. That's the same kind of ID (and you would then have
to get the information for this visual).

Next, we see that this visual has class DirectColor. Yay, we can
construct pixel values ourselves. The next few fields describe how to do
that. The most important thing are the red/green/blue masks. These tell
us (in this example), that we can just use 0xRRGGBB to produce a color
value (where RR is the value for the red component etc).

Hope this helps,
Uli
-- 
Sent from my Game Boy.


More information about the Xcb mailing list