# X11 true color

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

## 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,
"RGBi:0.0/0.0/1.0",
"RGBi:0.5/0.1/0.0");

// 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 on other sites
Hi BBB,

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(		display,		RootWindow(display, visual_info.screen),		0, 0,						/* Spawn window at x,y (the xserver knows best) */		WIN_WIDTH, WIN_HEIGHT,		0,						/* Border Width (again the xserver knows best) */		visual_info.depth,		InputOutput,				        /* Class (InputOnly/InputOutput) */		visual_info.visual,		CWBackPixel|CWEventMask|CWColormap,		/* Value Masks */		&swa	);        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 on other sites
Thx for the reply :) .

##### Share on other sites
Quote:

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 :)

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633420
• Total Posts
3011790
• ### Who's Online (See full list)

There are no registered users currently online

×