Table 38-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. Table 38-1. Color attribute resource names.background | The normal background color. All widgets. | bg | Short for background. Command line only. | foreground | The normal foreground color. Widgets: button, checkbutton, entry, label, listbox, menu, menubutton, message, radiobutton, scale, and text. | fg | Short for foreground. Command line only. | activeBackground | The background when a mouse button will take an action. Widgets: button, checkbutton, menu, menubutton, radiobutton, scale, and scrollbar. | activeForeground | The foreground when the mouse is over an active widget. Widgets: button, checkbutton, menu, menubutton, and radiobutton. | disabledForeground | The foreground when a widget is disabled. Widgets: button, checkbutton, menu, menubutton, and radiobutton. | highlightBackground | The highlight color when widget does not have focus. All widgets. | highlightColor | The highlight color when the widget has focus. All widgets. | insertBackground | The color of the insert cursor. Widgets: canvas, entry, and text. | selectBackground | The background of selected text. Widgets: canvas, entry, listbox, and text. | selectColor | The color of the selector indicator. Widgets: checkbutton, and radiobutton. | selectForeground | The foreground of selected text. Widgets: canvas, entry, listbox, and text. | troughColor | The trough part of scales and scrollbars. | 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 38-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 38-1 Resources for reverse video. proc ReverseVideo {} { option add *Foreground white option add *Background black } Color Palettes The 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 Values Color 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. You can 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 38-2 lists the system colors on Windows. Several of these colors map to the same RGB value. Table 38-3 lists the system colors on Macintosh. Table 38-2. Windows system colors.system3dDarkShadow | Dark part of button 3D-relief. | system3dLight | Light part of button 3D-relief. | systemActiveBorder | Window border when activated. | systemActiveCaption | Caption (i.e., title bar) when activated. | systemAppWorkspace | Background for MDI workspaces. | systemBackground | Widget background. | systemButtonFace | Button background. | systemButtonHighlight | Lightest part of button 3D-relief. | systemButtonShadow | Darkest part of button 3D-relief. | systemButtonText | Button foreground. | systemCaptionText | Caption (i.e., title bar) text. | systemDisabledText | Text when disabled. | systemGrayText | Grey text color. | systemHighlight | Selection background. | systemHighlightText | Selection foreground. | systemInactiveBorder | Window border when not activated. | systemInactiveCaption | Caption background when not activated. | systemInactiveCaptionText | Caption text when not activated. | systemInfoBackground | Help pop-up background. | systemInfoText | Help pop-up text. | systemMenu | Menu background. | systemMenuText | Menu foreground. | systemScrollbar | Scrollbar background. | systemWindow | Text window background. | systemWindowFrame | Text window frame. | systemWindowText | Text window text color. | Table 38-3. Macintosh system colors.systemHighlight | Selection background. | systemHighlightText | Selection foreground. | systemButtonFace | Button background. | systemButtonFrame | Button frame. | systemButtonText | Button foreground. | systemWindowBody | Widget background. | systemMenuActive | Selected menu item background. | systemMenuActiveText | Selected menu item foreground. | systemMenu | Menu background. | systemMenuDisabled | Disabled menu item background. | systemMenuText | Menu foreground. | | Getting RGB values. |
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 38-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)]] } |