Colors

   

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

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


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.
backgroundThe normal background color. All widgets.
bgShort for background. Command line only.
foregroundThe normal foreground color. Widgets: button, checkbutton, entry, label, listbox, menu, menubutton, message, radiobutton, scale, and text.
fgShort for foreground. Command line only.
activeBackgroundThe background when a mouse button will take an action. Widgets: button, checkbutton, menu, menubutton, radiobutton, scale, and scrollbar.
activeForegroundThe foreground when the mouse is over an active widget. Widgets: button, checkbutton, menu, menubutton, and radiobutton.
disabledForegroundThe foreground when a widget is disabled. Widgets: button, checkbutton, menu, menubutton, and radiobutton.
highlightBackgroundThe highlight color when widget does not have focus. All widgets.
highlightColorThe highlight color when the widget has focus. All widgets.
insertBackgroundThe color of the insert cursor. Widgets: canvas, entry, and text.
selectBackgroundThe background of selected text. Widgets: canvas, entry, listbox, and text.
selectColorThe color of the selector indicator. Widgets: checkbutton, and radiobutton.
selectForegroundThe foreground of selected text. Widgets: canvas, entry, listbox, and text.
troughColorThe 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.
system3dDarkShadowDark part of button 3D-relief.
system3dLightLight part of button 3D-relief.
systemActiveBorderWindow border when activated.
systemActiveCaptionCaption (i.e., title bar) when activated.
systemAppWorkspaceBackground for MDI workspaces.
systemBackgroundWidget background.
systemButtonFaceButton background.
systemButtonHighlightLightest part of button 3D-relief.
systemButtonShadowDarkest part of button 3D-relief.
systemButtonTextButton foreground.
systemCaptionTextCaption (i.e., title bar) text.
systemDisabledTextText when disabled.
systemGrayTextGrey text color.
systemHighlightSelection background.
systemHighlightTextSelection foreground.
systemInactiveBorderWindow border when not activated.
systemInactiveCaptionCaption background when not activated.
systemInactiveCaptionTextCaption text when not activated.
systemInfoBackgroundHelp pop-up background.
systemInfoTextHelp pop-up text.
systemMenuMenu background.
systemMenuTextMenu foreground.
systemScrollbarScrollbar background.
systemWindowText window background.
systemWindowFrameText window frame.
systemWindowTextText window text color.

Table 38-3. Macintosh system colors.
systemHighlightSelection background.
systemHighlightTextSelection foreground.
systemButtonFaceButton background.
systemButtonFrameButton frame.
systemButtonTextButton foreground.
systemWindowBodyWidget background.
systemMenuActiveSelected menu item background.
systemMenuActiveTextSelected menu item foreground.
systemMenuMenu background.
systemMenuDisabledDisabled menu item background.
systemMenuTextMenu foreground.

graphics/tip_icon.gif

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)]] } 

       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net