xcb library: how to draw in colours

1k Views Asked by At

I am creating C code that must draw lines and symbols in several graphic windows, in several colors. Consulting the www I learned that I need to use a colormap, however I cannot get them to work. Most code I found is about creating and populating a new colormap, however I only need a handful of colours (yellow, red, magenta, ...) so I thought the default map would be sufficient. How can I read what colours are available in the default colormap screen->default_colormap ? Could someone point me to an elementary example of creating a window and its graphic context, then drawing (say) a green circle in it, or a blue square?

1

There are 1 best solutions below

0
On

The following code is a few lines extracted from a project I'm working on, in which I draw with quite a number of colors. It works for me - I don't mean to say this is the best, or even a very good way, but I'm just a hobbyist trying to teach myself and out of the sparse examples I've found, this is what I've cobbled together.

check this manpage for: xcb_change_gc(etc.,);

I actually keep a simple array of the rgb values, stored is uint32_ts, which I draw with. Since I only program for my own satisfaction, I've no idea whether this is very portable. Also, it took some experimenting to discover the coding for the colors. I'm sure that's hardware dependent. I think the advantage of colormaps is that they ARE PORTABLE and if you don't have truecolor, colormaps will help you draw with the right colors. I personally don't need to know. I just like drawing using pixels or filled rectangles to explore some maths.

uint32_t    rgb = 0xFFFFFFFF; // white on my system

uint32_t    mask = XCB_GC_FOREGROUND,
            foreground_value = rgb;

xcb_gcontext_t gc;

gc = xcb_generate_id(connection);
xcb_create_gc (connection, gc, drawing_buffer_pixmap, mask, &foreground_value);

// below, the color of the gc is changed rather than making a new one

xcb_change_gc(connection, *gc, mask, &foreground_value);