Advertisement Jump to content
Sign in to follow this  

X11 true color

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sry if this is a no-brainer, but how do you specify a RGB-color? XWS has apperently a very powerfull color-system that can work on anything from grayscale and palette colors to true 24-bits color. I don't want palette-colors since my program is only going to run on new computers anyway. Also I don't care if the colors won't be EXACTLY the same on different machines, I just want a damn true color!! *pulls his hair* Like for example in OpenGL:

glColor3f(0.5, 1.0f. 0.3);

Ive tried this but it doesn't seem to work
  win = XCreateSimpleWindow(display, RootWindow(display, screen_num),
                            x, y, width, height, win_border_width,

// et.c.

Would be really nice if someone could post a short program that draws a line with some arbitrary RGB-color. Greatfull for any reply.

Share this post

Link to post
Share on other sites

I have been spending the last few months writing a game using just XLib on a X11R6 (Irix 6.5) and doing colour in Xlib wasnt very obvious. Basically you will need to allocate a colour map from the xserver before you can switch to any colours.

By far the best way I have seen to do this is by asking the xserver for a colormap using the DefaultColormap() function. After doing this you will need to set the 'window' you are creating to use this colormap, I dont know if XCreateSimpleWindow() automatically uses the default colormap but I use XCreateWindow() which is a bit more drawn out code-wise but is great for doing all the manky bits in one hit :)
        Window 			win;
Display* display;
XVisualInfo visual_info;
int default_screen;
int default_depth;
Colormap default_cmap;
Visual *default_visual;
XSetWindowAttributes swa;

default_screen = DefaultScreen(display);
default_visual = DefaultVisual(display,default_screen);
default_depth = DefaultDepth(display,default_screen);
default_cmap = DefaultColormap(display,default_screen);

visual_info.visual = default_visual;

swa.background_pixel = BlackPixel(display,default_screen);
swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask | ButtonPressMask;
swa.colormap = default_cmap;

win = XCreateWindow(
RootWindow(display, visual_info.screen),
0, 0, /* Spawn window at x,y (the xserver knows best) */
0, /* Border Width (again the xserver knows best) */
InputOutput, /* Class (InputOnly/InputOutput) */
CWBackPixel|CWEventMask|CWColormap, /* Value Masks */
XMapWindow(display, win);
XSetForeground(display, gc, WhitePixel(display,visual_info.screen));


/*for setting an 'RGB string' colour you will need something like this */

XColor fontcolor;

if(XParseColor(display, default_cmap, "rgb:EE/00/66", &fontcolor)){
XAllocColor(display, default_cmap,&fontcolor);
XSetForeground(display, gc, fontcolor.pixel);
} else {
printf("failed to allocate colour\n");

Sorry the code is a bit spotty I just cut bits of it out and removed my error_handling code. This should at least give you an idea of how it works.

I got these two Xlib books and I havent needed to google since :)

XLIB Reference Manual: Release 5.0 v.1

XLIB Reference Manual: Release 5.0 v.2

Hope this helps.

Share this post

Link to post
Share on other sites
Thx for the reply :) .
But what do you think about this? :

Share this post

Link to post
Share on other sites
Original post by BBB
Thx for the reply :) .
But what do you think about this? :

I have seen that way of doing it before, seems to be a common way to allocate colours. I don't know why you would do that instead of using the systems default colour map and then allocating the colours you need.

I have a small Isometric engine I am working on that uses pixmaps for rendering the tiles and I dont neede to allocated all the colours, I just use the DefaultColormap() then when set XpmAttibutes.colormap to the default_colormap when I read in each xpm ( using XpmReadFileToPixmap() ).

Sorry I dont know the definitive answer to why, I am still learning Xlib myself :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!