Debian Codegraph @

demos/scripts/xcolors.tcl

raw ◀ ▶
1 Debian patch touch this file
•1 #
2 #!../bltwish
3 #
4 # Tk version of xcolors
5 #
6
7 package require BLT
8 # --------------------------------------------------------------------------
9 # Starting with Tcl 8.x, the BLT commands are stored in their own
10 # namespace called "blt". The idea is to prevent name clashes with
11 # Tcl commands and variables from other packages, such as a "table"
12 # command in two different packages.
13 #
14 # You can access the BLT commands in a couple of ways. You can prefix
15 # all the BLT commands with the namespace qualifier "blt::"
16 #
17 # blt::graph .g
18 # blt::table . .g -resize both
19 #
20 # or you can import all the command into the global namespace.
21 #
22 # namespace import blt::*
23 # graph .g
24 # table . .g -resize both
25 #
26 # --------------------------------------------------------------------------
27 if { $tcl_version >= 8.0 } {
28 namespace import blt::*
29 namespace import -force blt::tile::*
30 }
31 source scripts/demo.tcl
32 set numCols 0
33 set numRows 0
34 set maxCols 15
35 set cellWidth 40
36 set cellHeight 20
37 set numCells 0
38 set lastCount 0
39 set beginInput(0) 0
40 set map 0
41 set entryCount 0
42 set lastTagId {}
43
44 scrollbar .xscroll -command { .canvas xview } -orient horizontal
45 scrollbar .yscroll -command { .canvas yview }
46
47 label .sample \
48 -font -*-new*century*schoolbook*-bold-r-*-*-24-*-*-*-*-*-*-* \
49 -text {"Bisque is Beautiful".}
50
51 button .name -font -*-helvetica-medium-r-*-*-18-*-*-*-*-*-*-* \
52 -command "AddSelection name"
53 button .rgb -font -*-courier-medium-r-*-*-18-*-*-*-*-*-*-* \
54 -command "AddSelection rgb"
55
56 canvas .canvas \
57 -confine 1 \
58 -yscrollcommand { .yscroll set } \
59 -width [expr 16*$cellWidth] -height 400 \
60 -scrollregion [list 0 0 [expr 16*$cellWidth] 800]
61
62 frame .border -bd 2 -relief raised
63
64 label .status \
65 -anchor w \
66 -font -*-helvetica-medium-r-*-*-14-*-*-*-*-*-*-*
67
68 button .quit -text "Quit" -command "exit"
69 button .next -text "Next" -command "DisplayColors next"
70 button .prev -text "Previous" -command "DisplayColors last"
71
72 selection handle .name GetColor
73 selection handle .rgb GetValue
74
75 bind .name <Enter> {
76 .status config -text \
77 "Press button to write color name into primary selection"
78 }
79
80 bind .rgb <Enter> {
81 .status config -text \
82 "Press button to write RGB value into primary selection"
83 }
84 bind .name <Leave> {
85 .status config -text ""
86 }
87
88 bind .rgb <Leave> {
89 .status config -text ""
90 }
91
92 bind .canvas <Enter> {
93 .status config -text \
94 "Press button 1 to change background; Button 2 changes foreground"
95 }
96
97
98 table . \
99 .sample 0,0 -cspan 2 -fill both -reqheight 1i \
100 .name 1,0 -fill both -anchor w \
101 .rgb 1,1 -fill both -anchor w \
102 .canvas 2,0 -cspan 2 -fill both \
103 .yscroll 2,2 -fill y \
104 .border 3,0 -cspan 2 -fill x -reqheight 8 \
105 .status 4,0 -cspan 2 -fill both \
106 .quit 4,1 -anchor e -reqwidth 1i -fill y -padx 10 -pady 4 \
107 .prev 5,0 -anchor e -reqwidth 1i -fill y -padx 10 -pady 4 \
108 .next 5,1 -anchor e -reqwidth 1i -fill y -padx 10 -pady 4
109
110 proc AddSelection { what } {
111 selection own .$what
112 if {$what == "name" } {
113 set mesg "Color name written into primary selection"
114 } else {
115 set mesg "RGB value written into primary selection"
116 }
117 .status config -text $mesg
118 }
119
120 proc GetColor { args } {
121 return [lindex [.name config -text] 4]
122 }
123
124 proc GetValue { args } {
125 return [lindex [.rgb config -text] 4]
126 }
127
128 proc ShowInfo { tagId what info } {
129 global lastTagId
130
131 if { $lastTagId != {} } {
132 .canvas itemconfig $lastTagId -width 1
133 }
134 .canvas itemconfig $tagId -width 3
135 set lastTagId $tagId
136
137 set name [lindex $info 3]
138 .name config -text $name
139 set value [format "#%0.2x%0.2x%0.2x" \
140 [lindex $info 0] [lindex $info 1] [lindex $info 2]]
141 .rgb config -text $value
142 .sample config $what $name
143 .status config -bg $name
144 }
145
146
147 proc MakeCell { info } {
148 global numCols numRows maxCols cellWidth cellHeight numCells
149
150 set x [expr $numCols*$cellWidth]
151 set y [expr $numRows*$cellHeight]
152 set color [lindex $info 3]
153
154 if [catch {winfo rgb . $color}] {
155 return "ok"
156 }
157 # if { [tk colormodel .] != "color" } {
158 # bind . <Leave> {
159 # .status config -text "Color table full after $numCells entries."
160 # }
161 # .status config -text "Color table full after $numCells entries."
162 # return "out of colors"
163 # }
164 set id [.canvas create rectangle \
165 $x $y [expr $x+$cellWidth] [expr $y+$cellHeight] \
166 -fill $color -outline black]
167 if { $color == "white" } {
168 global whiteTagId
169 set whiteTagId $id
170 }
171
172 .canvas bind $id <1> [list ShowInfo $id -bg $info]
173 .canvas bind $id <2> [list ShowInfo $id -fg $info]
174
175 incr numCols
176 if { $numCols > $maxCols } {
177 set numCols 0
178 incr numRows
179 }
180 return "ok"
181 }
182
183 proc DisplayColors { how } {
184 global lastCount numCells cellHeight numRows numCols rgbText
185 global map beginInput
186
187 # tk colormodel . color
188 set initialized no
189
190 if { $how == "last" } {
191 if { $map == 0 } {
192 return
193 }
194 set map [expr $map-1]
195 } else {
196 incr map
197 if ![info exists beginInput($map)] {
198 set beginInput($map) $lastCount
199 }
200 }
201
202 set start $beginInput($map)
203
204 if { $numCells > 0 } {
205 .canvas delete all
206 set numRows 0
207 set numCols 0
208 set initialized yes
209 }
210
211 set input [lrange $rgbText $start end]
212 set lineCount $start
213 set entryCount 0
214 foreach i $input {
215 incr lineCount
216 if { [llength $i] == 4 } {
217 if { [MakeCell $i] == "out of colors" } {
218 break
219 }
220 incr entryCount
221 }
222 }
223 if { $entryCount == 0 } {
224 bind . <Leave> {
225 .status config -text "No more entries in RGB database"
226 }
227 .status config -text "No more entries in RGB database"
228 }
229 set lastCount $lineCount
230 proc tkerror {args} {
231 #dummy procedure
232 }
233
234 if { $initialized == "no" } {
235 global cellWidth
236
237 set height [expr $cellHeight*($numRows+1)]
238 .canvas config -scrollregion [list 0 0 [expr 16*$cellWidth] $height]
239 if { $height < 800 } {
240 .canvas config -height $height
241 }
242 global whiteTagId
243 if [info exists whiteTagId] {
244 ShowInfo $whiteTagId -bg {255 255 255 white}
245 }
246 }
247 update idletasks
248 update
249 rename tkerror {}
250 }
251
252 wm min . 0 0
253
254 foreach location {
255 /usr/X11R6
256 /util/X11R6
257 /usr/openwin
258 /usr/dt
259 } {
260 set file [file join $location lib X11 rgb.txt]
261 if { [file exists $file] } {
262 break
263 }
264 }
265 set in [open $file "r"]
266 set rgbText [read $in]
267 close $in
268 set rgbText [split $rgbText \n]
269 DisplayColors next
270 wm min . 0 0
271
272