3
# This file contains procedures that change the color palette used
6
# RCS: @(#) $Id: palette.tcl,v 1.3 1998/09/14 18:23:24 stanton Exp $
8
# Copyright (c) 1995-1997 Sun Microsystems, Inc.
10
# See the file "license.terms" for information on usage and redistribution
11
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15
# Changes the default color scheme for a Tk application by setting
16
# default colors in the option database and by modifying all of the
17
# color options for existing widgets that have the default value.
20
# The arguments consist of either a single color name, which
21
# will be used as the new background color (all other colors will
22
# be computed from this) or an even number of values consisting of
23
# option names and values. The name for an option is the one used
24
# for the option database, such as activeForeground, not -activeforeground.
26
proc tk_setPalette {args} {
29
# Create an array that has the complete new palette. If some colors
30
# aren't specified, compute them from other colors that are specified.
32
if {[llength $args] == 1} {
33
set new(background) [lindex $args 0]
37
if {![info exists new(background)]} {
38
error "must specify a background color"
40
if {![info exists new(foreground)]} {
41
set new(foreground) black
43
set bg [winfo rgb . $new(background)]
44
set fg [winfo rgb . $new(foreground)]
45
set darkerBg [format #%02x%02x%02x [expr {(9*[lindex $bg 0])/2560}] \
46
[expr {(9*[lindex $bg 1])/2560}] [expr {(9*[lindex $bg 2])/2560}]]
47
foreach i {activeForeground insertBackground selectForeground \
49
if {![info exists new($i)]} {
50
set new($i) $new(foreground)
53
if {![info exists new(disabledForeground)]} {
54
set new(disabledForeground) [format #%02x%02x%02x \
55
[expr {(3*[lindex $bg 0] + [lindex $fg 0])/1024}] \
56
[expr {(3*[lindex $bg 1] + [lindex $fg 1])/1024}] \
57
[expr {(3*[lindex $bg 2] + [lindex $fg 2])/1024}]]
59
if {![info exists new(highlightBackground)]} {
60
set new(highlightBackground) $new(background)
62
if {![info exists new(activeBackground)]} {
63
# Pick a default active background that islighter than the
64
# normal background. To do this, round each color component
65
# up by 15% or 1/3 of the way to full white, whichever is
69
set light($i) [expr {[lindex $bg $i]/256}]
70
set inc1 [expr {($light($i)*15)/100}]
71
set inc2 [expr {(255-$light($i))/3}]
77
if {$light($i) > 255} {
81
set new(activeBackground) [format #%02x%02x%02x $light(0) \
84
if {![info exists new(selectBackground)]} {
85
set new(selectBackground) $darkerBg
87
if {![info exists new(troughColor)]} {
88
set new(troughColor) $darkerBg
90
if {![info exists new(selectColor)]} {
91
set new(selectColor) #b03060
94
# let's make one of each of the widgets so we know what the
95
# defaults are currently for this platform.
96
toplevel .___tk_set_palette
97
wm withdraw .___tk_set_palette
98
foreach q {button canvas checkbutton entry frame label listbox menubutton menu message \
99
radiobutton scale scrollbar text} {
100
$q .___tk_set_palette.$q
103
# Walk the widget hierarchy, recoloring all existing windows.
104
# The option database must be set according to what we do here,
105
# but it breaks things if we set things in the database while
106
# we are changing colors...so, tkRecolorTree now returns the
107
# option database changes that need to be made, and they
108
# need to be evalled here to take effect.
109
# We have to walk the whole widget tree instead of just
110
# relying on the widgets we've created above to do the work
111
# because different extensions may provide other kinds
112
# of widgets that we don't currently know about, so we'll
113
# walk the whole hierarchy just in case.
115
eval [tkRecolorTree . new]
117
catch {destroy .___tk_set_palette}
119
# Change the option database so that future windows will get the
122
foreach option [array names new] {
123
option add *$option $new($option) widgetDefault
126
# Save the options in the global variable tkPalette, for use the
127
# next time we change the options.
129
array set tkPalette [array get new]
133
# This procedure changes the colors in a window and all of its
134
# descendants, according to information provided by the colors
135
# argument. This looks at the defaults provided by the option
136
# database, if it exists, and if not, then it looks at the default
137
# value of the widget itself.
140
# w - The name of a window. This window and all its
141
# descendants are recolored.
142
# colors - The name of an array variable in the caller,
143
# which contains color information. Each element
144
# is named after a widget configuration option, and
145
# each value is the value for that option.
147
proc tkRecolorTree {w colors} {
151
foreach dbOption [array names c] {
152
set option -[string tolower $dbOption]
153
if {![catch {$w config $option} value]} {
154
# if the option database has a preference for this
155
# dbOption, then use it, otherwise use the defaults
157
set defaultcolor [option get $w $dbOption widgetDefault]
158
if {[string match {} $defaultcolor]} {
159
set defaultcolor [winfo rgb . [lindex $value 3]]
161
set defaultcolor [winfo rgb . $defaultcolor]
163
set chosencolor [winfo rgb . [lindex $value 4]]
164
if {[string match $defaultcolor $chosencolor]} {
165
# Change the option database so that future windows will get
167
append result ";\noption add [list \
168
*[winfo class $w].$dbOption $c($dbOption) 60]"
169
$w configure $option $c($dbOption)
173
foreach child [winfo children $w] {
174
append result ";\n[tkRecolorTree $child c]"
180
# Given a color name, computes a new color value that darkens (or
181
# brightens) the given color by a given percent.
184
# color - Name of starting color.
185
# perecent - Integer telling how much to brighten or darken as a
186
# percent: 50 means darken by 50%, 110 means brighten
189
proc tkDarken {color percent} {
190
set l [winfo rgb . $color]
191
set red [expr {[lindex $l 0]/256}]
192
set green [expr {[lindex $l 1]/256}]
193
set blue [expr {[lindex $l 2]/256}]
194
set red [expr {($red*$percent)/100}]
198
set green [expr {($green*$percent)/100}]
202
set blue [expr {($blue*$percent)/100}]
206
format #%02x%02x%02x $red $green $blue
210
# Reset the Tk color palette to the old "bisque" colors.
216
tk_setPalette activeBackground #e6ceb1 activeForeground black \
217
background #ffe4c4 disabledForeground #b0b0b0 foreground black \
218
highlightBackground #ffe4c4 highlightColor black \
219
insertBackground black selectColor #b03060 \
220
selectBackground #e6ceb1 selectForeground black \