# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
-# tk_setPalette --
+# ::tk_setPalette --
# Changes the default color scheme for a Tk application by setting
# default colors in the option database and by modifying all of the
# color options for existing widgets that have the default value.
# option names and values. The name for an option is the one used
# for the option database, such as activeForeground, not -activeforeground.
-proc tk_setPalette {args} {
+proc ::tk_setPalette {args} {
if {[winfo depth .] == 1} {
# Just return on monochrome displays, otherwise errors will occur
return
}
- global tkPalette
-
# Create an array that has the complete new palette. If some colors
# aren't specified, compute them from other colors that are specified.
if {![info exists new(background)]} {
error "must specify a background color"
}
+ set bg [winfo rgb . $new(background)]
if {![info exists new(foreground)]} {
- set new(foreground) black
+ # Note that the range of each value in the triple returned by
+ # [winfo rgb] is 0-65535, and your eyes are more sensitive to
+ # green than to red, and more to red than to blue.
+ foreach {r g b} $bg {break}
+ if {$r+1.5*$g+0.5*$b > 100000} {
+ set new(foreground) black
+ } else {
+ set new(foreground) white
+ }
}
- set bg [winfo rgb . $new(background)]
set fg [winfo rgb . $new(foreground)]
set darkerBg [format #%02x%02x%02x [expr {(9*[lindex $bg 0])/2560}] \
[expr {(9*[lindex $bg 1])/2560}] [expr {(9*[lindex $bg 2])/2560}]]
# defaults are currently for this platform.
toplevel .___tk_set_palette
wm withdraw .___tk_set_palette
- foreach q {button canvas checkbutton entry frame label listbox \
- menubutton menu message radiobutton scale scrollbar text} {
+ foreach q {
+ button canvas checkbutton entry frame label labelframe
+ listbox menubutton menu message radiobutton scale scrollbar
+ spinbox text
+ } {
$q .___tk_set_palette.$q
}
# Walk the widget hierarchy, recoloring all existing windows.
# The option database must be set according to what we do here,
# but it breaks things if we set things in the database while
- # we are changing colors...so, tkRecolorTree now returns the
+ # we are changing colors...so, ::tk::RecolorTree now returns the
# option database changes that need to be made, and they
# need to be evalled here to take effect.
# We have to walk the whole widget tree instead of just
# of widgets that we don't currently know about, so we'll
# walk the whole hierarchy just in case.
- eval [tkRecolorTree . new]
+ eval [tk::RecolorTree . new]
catch {destroy .___tk_set_palette}
option add *$option $new($option) widgetDefault
}
- # Save the options in the global variable tkPalette, for use the
+ # Save the options in the variable ::tk::Palette, for use the
# next time we change the options.
- array set tkPalette [array get new]
+ array set ::tk::Palette [array get new]
}
-# tkRecolorTree --
+# ::tk::RecolorTree --
# This procedure changes the colors in a window and all of its
# descendants, according to information provided by the colors
# argument. This looks at the defaults provided by the option
# is named after a widget configuration option, and
# each value is the value for that option.
-proc tkRecolorTree {w colors} {
- global tkPalette
+proc ::tk::RecolorTree {w colors} {
upvar $colors c
set result {}
+ set prototype .___tk_set_palette.[string tolower [winfo class $w]]
+ if {![winfo exists $prototype]} {
+ unset prototype
+ }
foreach dbOption [array names c] {
set option -[string tolower $dbOption]
+ set class [string replace $dbOption 0 0 [string toupper \
+ [string index $dbOption 0]]]
if {![catch {$w config $option} value]} {
# if the option database has a preference for this
# dbOption, then use it, otherwise use the defaults
# for the widget.
- set defaultcolor [option get $w $dbOption widgetDefault]
- if {[string match {} $defaultcolor]} {
+ set defaultcolor [option get $w $dbOption $class]
+ if {[string match {} $defaultcolor] || \
+ ([info exists prototype] && \
+ [$prototype cget $option] ne "$defaultcolor")} {
set defaultcolor [winfo rgb . [lindex $value 3]]
} else {
set defaultcolor [winfo rgb . $defaultcolor]
}
- if {[lindex $value 4] != {}} {
set chosencolor [winfo rgb . [lindex $value 4]]
if {[string match $defaultcolor $chosencolor]} {
# Change the option database so that future windows will get
*[winfo class $w].$dbOption $c($dbOption) 60]"
$w configure $option $c($dbOption)
}
- }
}
}
foreach child [winfo children $w] {
- append result ";\n[tkRecolorTree $child c]"
+ append result ";\n[::tk::RecolorTree $child c]"
}
return $result
}
-# tkDarken --
+# ::tk::Darken --
# Given a color name, computes a new color value that darkens (or
# brightens) the given color by a given percent.
#
# percent: 50 means darken by 50%, 110 means brighten
# by 10%.
-proc tkDarken {color percent} {
+proc ::tk::Darken {color percent} {
foreach {red green blue} [winfo rgb . $color] {
set red [expr {($red/256)*$percent/100}]
set green [expr {($green/256)*$percent/100}]
return [format "#%02x%02x%02x" $red $green $blue]
}
-# tk_bisque --
+# ::tk_bisque --
# Reset the Tk color palette to the old "bisque" colors.
#
# Arguments:
# None.
-proc tk_bisque {} {
+proc ::tk_bisque {} {
tk_setPalette activeBackground #e6ceb1 activeForeground black \
background #ffe4c4 disabledForeground #b0b0b0 foreground black \
highlightBackground #ffe4c4 highlightColor black \
selectBackground #e6ceb1 selectForeground black \
troughColor #cdb79e
}
-