Sujet : Re: Named colours in Tk
De : gregor.ebbing (at) *nospam* gmx.de (greg)
Groupes : comp.lang.tclDate : 20. May 2025, 16:00:07
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <100i5dn$29lhf$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
Am 19.05.25 um 16:05 schrieb Alan Grunwald:
Tk recognises a lot of colour names, as documented at, say, https:// www.tcl-lang.org/man/tcl9.0/TkCmd/colors.html Is it possible to get a list of them?
I guess I could hack it out of one of the source files, but I'd hope to be able to get it in a cleaner fashion.
I need a list of color names.
Linux Debian:
/usr/share/X11/rgb.txt
or
https://gitlab.freedesktop.org/xorg/app/rgb/raw/master/rgb.txtor
in the demo directory is a file with color name
file join $::tk_library demos colors.tcl
Then use winfo to check.
https://www.tcl-lang.org/man/tcl8.6/TkCmd/winfo.htm#M30}
package require Tk
#the proc can only read rgb.txt
proc readRgbFile {rgbFile} {
set fh [open $rgbFile r]
set db {}
while {[gets $fh line] >= 0} {
if {[string match {!*} $line]} continue
set name [lassign $line r g b]
set hex [format "#%02X%02X%02X" $r $g $b]
# Use {-1 -1 -1} if in list, or continue if not recognized
set winfo [expr {[catch {winfo rgb . $name} msg] ? [continue] : $msg}]
#set winfo [expr {[catch {winfo rgb . $name} msg] ? {-1 -1 -1} : $msg}]
dict set db $name [dict create r $r g $g b $b hex $hex winfo $winfo]
}
close $fh
return $db
}
set namergb [readRgbFile rgb.txt]
#set namergb [readRgbFile /usr/share/X11/rgb.txt]
foreach sc {red green lime blue white black yellow "DebianRed"} {
if {[dict exists $namergb $sc]} {
puts "searchColor $sc: [dict get $namergb $sc]"
} else {
puts "searchColor $sc: Not found"
}
}
# or just a proc for checking
proc colorExists {color} {
return [expr {![catch {winfo rgb . $color}]}]
}
if {0} {
#Output:
./nn-example.tcl
searchColor red: r 255 g 0 b 0 hex #FF0000 winfo {65535 0 0}
searchColor green: r 0 g 255 b 0 hex #00FF00 winfo {0 32896 0}
searchColor lime: r 0 g 255 b 0 hex #00FF00 winfo {0 65535 0}
searchColor blue: r 0 g 0 b 255 hex #0000FF winfo {0 0 65535}
searchColor white: r 255 g 255 b 255 hex #FFFFFF winfo {65535 65535 65535}
searchColor black: r 0 g 0 b 0 hex #000000 winfo {0 0 0}
searchColor yellow: r 255 g 255 b 0 hex #FFFF00 winfo {65535 65535 0}
searchColor debianred: Not found
}
best regards
Gregor