An Introduction To Embedded Tk (page 27 of 32)

[Previous Page][Next Page][Table of Contents]

12.1 The Color Chooser

There is a color chooser tool for X11 called color. The sources to color are in the files color.c and color.tcl. A screen image of the program is shown in figure 12.1.


**Image**

Figure 12.1: Typical appearance of the color program


The X11 Window System supports displays with over 280 quadrillion distinct colors (48 bits per pixel). But from this vast number, a few hundred colors are assigned English names like ``blue'' or ``turquoise'' or ``peachpuff''. All the rest are given arcane hexadecimal designations like ``#b22cd8517f32''. It is best to use colors with English names whenever possible.

The purpose of the color program it to help select colors with English names. At the top of the application is large swatch showing one of the 280 quadrillion X11 colors, together with either its English name (if it has one) or its hexadecimal value. Sliders on the lower left side of the window allow the user to vary the color of the swatch by changing various color components. On the lower right side of the window are six smaller swatches that show colors with English names that are similar to the color in the main swatch. Moving any of the six color component sliders causes the colors in all swatches, and the other sliders, to update in real time. Clicking on any of the smaller swatches transfers its color to the main swatch, updating all of the sliders and swatches appropriately.

In theory, there is nothing to prevent the color program from being coded in pure Tcl/Tk, but in practice, such an implementation would be much too slow. For this reason, two key routines are coded in C. The ET_PROC command ChangeComponent is called whenever one of the color component sliders is moved. This routine moves the other sliders, changes the color of the main swatch, then computes close colors for the smaller swatches. Another ET_PROC command named ChangeColor is called whenever the user clicks on one of the smaller swatches. This routine changes the color of the main swatch, then updates the sliders and the smaller swatches accordingly.

[Next Page][Table of Contents]