Xlib tutorial part 3 -- Colour

by Alan at Mon 26th Jan 2009 1:00AM EST


The world is not very interesting if we are stuck in black and white. X has a sophisticated colour management system to be able to handle colour on displays that can only handle 16 colours at a time up to those that can handle millions or billions.

In our case, we just want three new colours. We're going to assume that the colours we name already exist on the server. For instance, on a true colour system they automatically will. X requires us to have a colormap so that we can map colours to integer values. So we'll put create some new variables. One to store the colormap and two to store the colours. Don't worry about the XColor structure right now. We'll get back to it later.

	Colormap cmap;
	XColor xc, xc2;


Then we'll stick in some code to do colour lookup after we get the screen number, but before we create the window. We'll also remove the three lines where we call BlackPixel or WhitePixel

	cmap = DefaultColormap(dpy, screen_num);
	XAllocNamedColor(dpy, cmap, "DarkGreen", &xc, &xc2);
	background = xc.pixel;
	XAllocNamedColor(dpy, cmap, "LightGreen", &xc, &xc2);
	border = xc.pixel;
	XAllocNamedColor(dpy, cmap, "Red", &xc, &xc2);
	values.foreground = xc.pixel;

So, nine new lines and three lines from the last section removed. We have to remove the lines to get the black or white pixel values for the default screen or they may overwrite our allocated colours.

Things to try:

Comments are closed.