1 | # palette.tcl -- |
---|
2 | # |
---|
3 | # This file contains procedures that change the color palette used |
---|
4 | # by Tk. |
---|
5 | # |
---|
6 | # RCS: @(#) $Id: palette.tcl,v 1.8 2001/11/29 10:54:21 dkf Exp $ |
---|
7 | # |
---|
8 | # Copyright (c) 1995-1997 Sun Microsystems, Inc. |
---|
9 | # |
---|
10 | # See the file "license.terms" for information on usage and redistribution |
---|
11 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
12 | # |
---|
13 | |
---|
14 | # ::tk_setPalette -- |
---|
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. |
---|
18 | # |
---|
19 | # Arguments: |
---|
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. |
---|
25 | |
---|
26 | proc ::tk_setPalette {args} { |
---|
27 | if {[winfo depth .] == 1} { |
---|
28 | # Just return on monochrome displays, otherwise errors will occur |
---|
29 | return |
---|
30 | } |
---|
31 | |
---|
32 | # Create an array that has the complete new palette. If some colors |
---|
33 | # aren't specified, compute them from other colors that are specified. |
---|
34 | |
---|
35 | if {[llength $args] == 1} { |
---|
36 | set new(background) [lindex $args 0] |
---|
37 | } else { |
---|
38 | array set new $args |
---|
39 | } |
---|
40 | if {![info exists new(background)]} { |
---|
41 | error "must specify a background color" |
---|
42 | } |
---|
43 | set bg [winfo rgb . $new(background)] |
---|
44 | if {![info exists new(foreground)]} { |
---|
45 | # Note that the range of each value in the triple returned by |
---|
46 | # [winfo rgb] is 0-65535, and your eyes are more sensitive to |
---|
47 | # green than to red, and more to red than to blue. |
---|
48 | foreach {r g b} $bg {break} |
---|
49 | if {$r+1.5*$g+0.5*$b > 100000} { |
---|
50 | set new(foreground) black |
---|
51 | } else { |
---|
52 | set new(foreground) white |
---|
53 | } |
---|
54 | } |
---|
55 | set fg [winfo rgb . $new(foreground)] |
---|
56 | set darkerBg [format #%02x%02x%02x [expr {(9*[lindex $bg 0])/2560}] \ |
---|
57 | [expr {(9*[lindex $bg 1])/2560}] [expr {(9*[lindex $bg 2])/2560}]] |
---|
58 | foreach i {activeForeground insertBackground selectForeground \ |
---|
59 | highlightColor} { |
---|
60 | if {![info exists new($i)]} { |
---|
61 | set new($i) $new(foreground) |
---|
62 | } |
---|
63 | } |
---|
64 | if {![info exists new(disabledForeground)]} { |
---|
65 | set new(disabledForeground) [format #%02x%02x%02x \ |
---|
66 | [expr {(3*[lindex $bg 0] + [lindex $fg 0])/1024}] \ |
---|
67 | [expr {(3*[lindex $bg 1] + [lindex $fg 1])/1024}] \ |
---|
68 | [expr {(3*[lindex $bg 2] + [lindex $fg 2])/1024}]] |
---|
69 | } |
---|
70 | if {![info exists new(highlightBackground)]} { |
---|
71 | set new(highlightBackground) $new(background) |
---|
72 | } |
---|
73 | if {![info exists new(activeBackground)]} { |
---|
74 | # Pick a default active background that islighter than the |
---|
75 | # normal background. To do this, round each color component |
---|
76 | # up by 15% or 1/3 of the way to full white, whichever is |
---|
77 | # greater. |
---|
78 | |
---|
79 | foreach i {0 1 2} { |
---|
80 | set light($i) [expr {[lindex $bg $i]/256}] |
---|
81 | set inc1 [expr {($light($i)*15)/100}] |
---|
82 | set inc2 [expr {(255-$light($i))/3}] |
---|
83 | if {$inc1 > $inc2} { |
---|
84 | incr light($i) $inc1 |
---|
85 | } else { |
---|
86 | incr light($i) $inc2 |
---|
87 | } |
---|
88 | if {$light($i) > 255} { |
---|
89 | set light($i) 255 |
---|
90 | } |
---|
91 | } |
---|
92 | set new(activeBackground) [format #%02x%02x%02x $light(0) \ |
---|
93 | $light(1) $light(2)] |
---|
94 | } |
---|
95 | if {![info exists new(selectBackground)]} { |
---|
96 | set new(selectBackground) $darkerBg |
---|
97 | } |
---|
98 | if {![info exists new(troughColor)]} { |
---|
99 | set new(troughColor) $darkerBg |
---|
100 | } |
---|
101 | if {![info exists new(selectColor)]} { |
---|
102 | set new(selectColor) #b03060 |
---|
103 | } |
---|
104 | |
---|
105 | # let's make one of each of the widgets so we know what the |
---|
106 | # defaults are currently for this platform. |
---|
107 | toplevel .___tk_set_palette |
---|
108 | wm withdraw .___tk_set_palette |
---|
109 | foreach q { |
---|
110 | button canvas checkbutton entry frame label labelframe |
---|
111 | listbox menubutton menu message radiobutton scale scrollbar |
---|
112 | spinbox text |
---|
113 | } { |
---|
114 | $q .___tk_set_palette.$q |
---|
115 | } |
---|
116 | |
---|
117 | # Walk the widget hierarchy, recoloring all existing windows. |
---|
118 | # The option database must be set according to what we do here, |
---|
119 | # but it breaks things if we set things in the database while |
---|
120 | # we are changing colors...so, ::tk::RecolorTree now returns the |
---|
121 | # option database changes that need to be made, and they |
---|
122 | # need to be evalled here to take effect. |
---|
123 | # We have to walk the whole widget tree instead of just |
---|
124 | # relying on the widgets we've created above to do the work |
---|
125 | # because different extensions may provide other kinds |
---|
126 | # of widgets that we don't currently know about, so we'll |
---|
127 | # walk the whole hierarchy just in case. |
---|
128 | |
---|
129 | eval [tk::RecolorTree . new] |
---|
130 | |
---|
131 | catch {destroy .___tk_set_palette} |
---|
132 | |
---|
133 | # Change the option database so that future windows will get the |
---|
134 | # same colors. |
---|
135 | |
---|
136 | foreach option [array names new] { |
---|
137 | option add *$option $new($option) widgetDefault |
---|
138 | } |
---|
139 | |
---|
140 | # Save the options in the variable ::tk::Palette, for use the |
---|
141 | # next time we change the options. |
---|
142 | |
---|
143 | array set ::tk::Palette [array get new] |
---|
144 | } |
---|
145 | |
---|
146 | # ::tk::RecolorTree -- |
---|
147 | # This procedure changes the colors in a window and all of its |
---|
148 | # descendants, according to information provided by the colors |
---|
149 | # argument. This looks at the defaults provided by the option |
---|
150 | # database, if it exists, and if not, then it looks at the default |
---|
151 | # value of the widget itself. |
---|
152 | # |
---|
153 | # Arguments: |
---|
154 | # w - The name of a window. This window and all its |
---|
155 | # descendants are recolored. |
---|
156 | # colors - The name of an array variable in the caller, |
---|
157 | # which contains color information. Each element |
---|
158 | # is named after a widget configuration option, and |
---|
159 | # each value is the value for that option. |
---|
160 | |
---|
161 | proc ::tk::RecolorTree {w colors} { |
---|
162 | upvar $colors c |
---|
163 | set result {} |
---|
164 | set prototype .___tk_set_palette.[string tolower [winfo class $w]] |
---|
165 | if {![winfo exists $prototype]} { |
---|
166 | unset prototype |
---|
167 | } |
---|
168 | foreach dbOption [array names c] { |
---|
169 | set option -[string tolower $dbOption] |
---|
170 | set class [string replace $dbOption 0 0 [string toupper \ |
---|
171 | [string index $dbOption 0]]] |
---|
172 | if {![catch {$w config $option} value]} { |
---|
173 | # if the option database has a preference for this |
---|
174 | # dbOption, then use it, otherwise use the defaults |
---|
175 | # for the widget. |
---|
176 | set defaultcolor [option get $w $dbOption $class] |
---|
177 | if {[string match {} $defaultcolor] || \ |
---|
178 | ([info exists prototype] && \ |
---|
179 | [$prototype cget $option] ne "$defaultcolor")} { |
---|
180 | set defaultcolor [winfo rgb . [lindex $value 3]] |
---|
181 | } else { |
---|
182 | set defaultcolor [winfo rgb . $defaultcolor] |
---|
183 | } |
---|
184 | set chosencolor [winfo rgb . [lindex $value 4]] |
---|
185 | if {[string match $defaultcolor $chosencolor]} { |
---|
186 | # Change the option database so that future windows will get |
---|
187 | # the same colors. |
---|
188 | append result ";\noption add [list \ |
---|
189 | *[winfo class $w].$dbOption $c($dbOption) 60]" |
---|
190 | $w configure $option $c($dbOption) |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | foreach child [winfo children $w] { |
---|
195 | append result ";\n[::tk::RecolorTree $child c]" |
---|
196 | } |
---|
197 | return $result |
---|
198 | } |
---|
199 | |
---|
200 | # ::tk::Darken -- |
---|
201 | # Given a color name, computes a new color value that darkens (or |
---|
202 | # brightens) the given color by a given percent. |
---|
203 | # |
---|
204 | # Arguments: |
---|
205 | # color - Name of starting color. |
---|
206 | # perecent - Integer telling how much to brighten or darken as a |
---|
207 | # percent: 50 means darken by 50%, 110 means brighten |
---|
208 | # by 10%. |
---|
209 | |
---|
210 | proc ::tk::Darken {color percent} { |
---|
211 | foreach {red green blue} [winfo rgb . $color] { |
---|
212 | set red [expr {($red/256)*$percent/100}] |
---|
213 | set green [expr {($green/256)*$percent/100}] |
---|
214 | set blue [expr {($blue/256)*$percent/100}] |
---|
215 | break |
---|
216 | } |
---|
217 | if {$red > 255} { |
---|
218 | set red 255 |
---|
219 | } |
---|
220 | if {$green > 255} { |
---|
221 | set green 255 |
---|
222 | } |
---|
223 | if {$blue > 255} { |
---|
224 | set blue 255 |
---|
225 | } |
---|
226 | return [format "#%02x%02x%02x" $red $green $blue] |
---|
227 | } |
---|
228 | |
---|
229 | # ::tk_bisque -- |
---|
230 | # Reset the Tk color palette to the old "bisque" colors. |
---|
231 | # |
---|
232 | # Arguments: |
---|
233 | # None. |
---|
234 | |
---|
235 | proc ::tk_bisque {} { |
---|
236 | tk_setPalette activeBackground #e6ceb1 activeForeground black \ |
---|
237 | background #ffe4c4 disabledForeground #b0b0b0 foreground black \ |
---|
238 | highlightBackground #ffe4c4 highlightColor black \ |
---|
239 | insertBackground black selectColor #b03060 \ |
---|
240 | selectBackground #e6ceb1 selectForeground black \ |
---|
241 | troughColor #cdb79e |
---|
242 | } |
---|