www.gibmonks.com




  Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 38.  Color, Images, and Cursors


Colormaps and Visuals

Computer screens can display only a fixed number of different colors at one time. The best monitors can display 24 million colors, but it is common to find 256 color displays. Really old VGA displays only display 16 colors. If you run several applications at once, it is possible that more colors are requested than can be displayed. The Windows and Macintosh platforms manage this scenario automatically. X provides lower-level facilities that Tk uses on UNIX to do the management. So, for the most part you don't have to worry. However, if you need more control, especially under X, then you need to understand colormaps and the different visual types.

Each pixel on the screen is represented by one or more bits of memory. There are a number of ways to map from a value stored at a pixel to the color that appears on the screen at that pixel. The mapping is a function of the number of bits at each pixel, which is called the depth of the display, and the style of interpretation, or visual class. The six visual classes defined by X are listed in the following table. Some of the visuals use a colormap that maps from the value stored at a pixel to a value used by the hardware to generate a color. A colormap enables a compact encoding for a much richer color. For example, a 256-entry colormap can be indexed with 8 bits, but it may contain 24 bits of color information. The UNIX xdpyinfo program reports the different visual classes supported by your display. Table 38-4 lists the visual classes:

Table 38-4. Visual classes for displays.
staticgreyGreyscale with a fixed colormap defined by the system.
greyscaleGreyscale with a writable colormap.
staticcolorColor with a fixed colormap defined by the system.
pseudocolorColor values determined by single writable colormap.
truecolorColor values determined by three colormaps defined by the system: one each for red, green, and blue.
directcolorColor values determined by three writable colormaps: one each for red, green, and blue.
bestUse the best visual for a given depth.

The frame and toplevel widgets support a colormap and visual attribute. You can query these attributes on all platforms. On Windows and Macintosh there is only one visual type at a time, and users may be able to change it for their whole system. On UNIX, the X server typically supports more than one visual class on the same display, and you can create frames and toplevels that use a particular visual class. The value of the visual attribute has two parts, a visual type and the desired depth of the display. The following example requests a greyscale visual with a depth of 4 bits per pixel:

toplevel .grey -visual "greyscale 4"

You can start wish with a -visual command line argument:

wish -visual "truecolor 24"

A visual is associated with a colormap. Windows and Macintosh have a single colormap that is shared by all applications. UNIX allows for private colormaps, which can be useful if you absolutely must have lots of colors. However, the drawback of a private colormap is that the display flashes as the mouse enters windows with their own colormap. This is because the monitor hardware really only has one colormap, so the X server must swap colormaps. Macintosh and Windows manage their colormap more gracefully, although if you use too many colors some flashing can occur. Tk can simulate private colormaps on Windows, but it is probably better to let the system manage the colormap. Tk on the Macintosh always uses a 24-bit truecolor visual, which is basically unlimited colors, and lets the operating system dither colors if necessary.

By default a widget inherits the colormap and visual from its parent widget. The value of the colormap attribute can be the keyword new, in which case the frame or toplevel gets a new private colormap, or it can be the name of another widget, in which case the frame or toplevel shares the colormap of that widget. When sharing colormaps, the other widget must be on the same screen and using the same visual class.


      Previous section   Next section
    Top