Next: About this document ... Up: Colour in X/Motif Previous: Colour Database

Explicit Colour Coding

Here we program the RGB values directly.

Basically we do this by setting the RGB values of a given XColour directly and then set this an entry in the colourmap.

The function XAllocColor(Display, Colormap, XColour) will set the Colormap RGB entries for a given index from the pixel, red, green, blue values in the XColor structure.

It is good practice to free the colormap entry first using XFreeColors(Display, Colormap, unsigned long pixel_index, int num_pixels, unsigned long planes_to_be_freed) before assigning new values.


Let us look at an example program colour.c that illustrates this point.

colour.c creates a Label widget which can have its colours changed via 3 Scale widgets - one for each RGB.

The program only changes on colourmap value (index 1) as this is the colourmap index assigned to the Label.

The program creates a Form widget which has contained within it the Label and a RowColumn widget. The RowColumn widget contains the 3 Scale widgets (Fig. 27).

Fig. 27 Output of colour.c

The same Callback function is used for the 3 Scales. The Client data DoRed etc. being used to distinguish which Scale called the function. The Scale callbacks are activated on a drag or ValueChanged event.

The value of the callback structure is then used to set the new RGB value.

The functions XAllocColor() and XFreeColors() set up the colormap values for a DefaultColorMaoOfScreen() colormap, cmap.

Finally we reset the background colour pixel of the Label widget with XtVaSetValues().
Tue May 24 16:52:56 BST 1994