| [ Team LiB ] |
|
ColorsTable 41-1 lists the resource names for color attributes. The table indicates what widgets use the different color attributes. Remember to use all lowercase and a leading dash when specifying attributes in a Tcl command.
The foreground color is used to draw an element, while the background color is used for the blank area behind the element. Text, for example, is painted with the foreground color. There are several variations on foreground and background that reflect different states for widgets or items they are displaying. Each attribute also has a resource class. This is most useful for the variations on foreground and background colors. For example, Tk does not have a reverse video mode. However, with a couple of resource specifications you can convert a monochrome display into reverse video. The definitions are given in Example 41-1. The Foreground and Background resource class names are used, and the various foreground and background colors (e.g., activeBackground) have the correct resource class so these settings work. You have to set these resources before you create any widgets: Example 41-1 Resources for reverse video
proc ReverseVideo {} {
option add *Foreground white
option add *Background black
}
Color PalettesThe tk_setPalette command changes colors of existing widgets and installs resource values so new widgets have matching colors. If you give it a single argument, it treats this as the background and then computes new values for the other color resources. For example, if you do not like the standard Tk grey, you can lighten your spirits with a cool blue background: tk_setPalette #0088cc If you liked the light brown color scheme of Tk 3.6, you can restore that palette with the tk_bisque command: tk_bisque The tk_setPalette command can be used to change any of the color attributes. You can specify a set of name-value pairs, where the names are color resource names and the values are new color values: tk_setPalette activeBackground red activeForeground white Color ValuesColor values are specified in two ways: symbolically (e.g., red), or by hexadecimal numbers (e.g., #ff0000). The leading # distinguishes the hexadecimal representation from the symbolic one. The number is divided into three equal-sized fields that give the red, green, and blue values, respectively. The fields can specify 4, 8, 12, or 16 bits of a color: #RGB 4 bits per color #RRGGBB 8 bits per color #RRRGGGBBB 12 bits per color #RRRRGGGGBBBB 16 bits per color If you specify more resolution than is supported by the display, the low-order bits of each field are discarded. The different display types supported by Tk are described in the next section. Each field ranges from 0, which means no color, to a maximum, which is all ones in binary, or all f in hex, that means full color saturation. For example, pure red can be specified four ways: #f00 #ff0000 #fff000000 #ffff00000000 There is a large collection of symbolic color names like "red," "blue," "green," "thistle," "medium sea green," and "yellow4." These names originate from X and UNIX, and Tk supports these colors on all platforms. As of Tk 8.3.2, these color names are documented in the colors online reference page. Prior to that, you could find the list in the Tk sources in the xlib/xcolor.c file. Or, run the xcolors program that comes with the standard X distribution. The Windows and Macintosh platforms have a small set of colors that are guaranteed to exist, and Tk defines names for these. The advantage of using these colors is that they are shared by all applications, so the system can manage colors efficiently. Table 41-2 lists the system colors on Windows. Several of these colors map to the same RGB value. Table 41-3 lists the system colors on Macintosh.
The winfo rgb command maps from a color name (or value) to three numbers that are its red, green, and blue values. You can use this to compute variations on a color. The ColorDarken procedure shown below uses the winfo rgb command to get the red, green, and blue components of the input color. It reduces these amounts by 5 percent, and reconstructs the color specification using the format command. Example 41-2 Computing a darker color
proc ColorDarken { win color } {
set rgb [winfo rgb $win $color]
return [format "#%03x%03x%03x" \
[expr round([lindex $rgb 0] * 0.95)] \
[expr round([lindex $rgb 1] * 0.95)] \
[expr round([lindex $rgb 2] * 0.95)]]
}
|
| [ Team LiB ] |
|