clrpick.tcl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. # clrpick.tcl --
  2. #
  3. # Color selection dialog for platforms that do not support a
  4. # standard color selection dialog.
  5. #
  6. # Copyright (c) 1996 Sun Microsystems, Inc.
  7. #
  8. # See the file "license.terms" for information on usage and redistribution
  9. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10. #
  11. # ToDo:
  12. #
  13. # (1): Find out how many free colors are left in the colormap and
  14. # don't allocate too many colors.
  15. # (2): Implement HSV color selection.
  16. #
  17. # Make sure namespaces exist
  18. namespace eval ::tk {}
  19. namespace eval ::tk::dialog {}
  20. namespace eval ::tk::dialog::color {
  21. namespace import ::tk::msgcat::*
  22. }
  23. # ::tk::dialog::color:: --
  24. #
  25. # Create a color dialog and let the user choose a color. This function
  26. # should not be called directly. It is called by the tk_chooseColor
  27. # function when a native color selector widget does not exist
  28. #
  29. proc ::tk::dialog::color:: {args} {
  30. variable ::tk::Priv
  31. set dataName __tk__color
  32. upvar ::tk::dialog::color::$dataName data
  33. set w .$dataName
  34. # The lines variables track the start and end indices of the line
  35. # elements in the colorbar canvases.
  36. set data(lines,red,start) 0
  37. set data(lines,red,last) -1
  38. set data(lines,green,start) 0
  39. set data(lines,green,last) -1
  40. set data(lines,blue,start) 0
  41. set data(lines,blue,last) -1
  42. # This is the actual number of lines that are drawn in each color strip.
  43. # Note that the bars may be of any width.
  44. # However, NUM_COLORBARS must be a number that evenly divides 256.
  45. # Such as 256, 128, 64, etc.
  46. set data(NUM_COLORBARS) 16
  47. # BARS_WIDTH is the number of pixels wide the color bar portion of the
  48. # canvas is. This number must be a multiple of NUM_COLORBARS
  49. set data(BARS_WIDTH) 160
  50. # PLGN_WIDTH is the number of pixels wide of the triangular selection
  51. # polygon. This also results in the definition of the padding on the
  52. # left and right sides which is half of PLGN_WIDTH. Make this number even.
  53. set data(PLGN_HEIGHT) 10
  54. # PLGN_HEIGHT is the height of the selection polygon and the height of the
  55. # selection rectangle at the bottom of the color bar. No restrictions.
  56. set data(PLGN_WIDTH) 10
  57. Config $dataName $args
  58. InitValues $dataName
  59. set sc [winfo screen $data(-parent)]
  60. set winExists [winfo exists $w]
  61. if {!$winExists || $sc ne [winfo screen $w]} {
  62. if {$winExists} {
  63. destroy $w
  64. }
  65. toplevel $w -class TkColorDialog -screen $sc
  66. if {[tk windowingsystem] eq "x11"} {wm attributes $w -type dialog}
  67. BuildDialog $w
  68. }
  69. # Dialog boxes should be transient with respect to their parent,
  70. # so that they will always stay on top of their parent window. However,
  71. # some window managers will create the window as withdrawn if the parent
  72. # window is withdrawn or iconified. Combined with the grab we put on the
  73. # window, this can hang the entire application. Therefore we only make
  74. # the dialog transient if the parent is viewable.
  75. if {[winfo viewable [winfo toplevel $data(-parent)]] } {
  76. wm transient $w $data(-parent)
  77. }
  78. # 5. Withdraw the window, then update all the geometry information
  79. # so we know how big it wants to be, then center the window in the
  80. # display (Motif style) and de-iconify it.
  81. ::tk::PlaceWindow $w widget $data(-parent)
  82. wm title $w $data(-title)
  83. # 6. Set a grab and claim the focus too.
  84. ::tk::SetFocusGrab $w $data(okBtn)
  85. # 7. Wait for the user to respond, then restore the focus and
  86. # return the index of the selected button. Restore the focus
  87. # before deleting the window, since otherwise the window manager
  88. # may take the focus away so we can't redirect it. Finally,
  89. # restore any grab that was in effect.
  90. vwait ::tk::Priv(selectColor)
  91. set result $Priv(selectColor)
  92. ::tk::RestoreFocusGrab $w $data(okBtn)
  93. unset data
  94. return $result
  95. }
  96. # ::tk::dialog::color::InitValues --
  97. #
  98. # Get called during initialization or when user resets NUM_COLORBARS
  99. #
  100. proc ::tk::dialog::color::InitValues {dataName} {
  101. upvar ::tk::dialog::color::$dataName data
  102. # IntensityIncr is the difference in color intensity between a colorbar
  103. # and its neighbors.
  104. set data(intensityIncr) [expr {256 / $data(NUM_COLORBARS)}]
  105. # ColorbarWidth is the width of each colorbar
  106. set data(colorbarWidth) [expr {$data(BARS_WIDTH) / $data(NUM_COLORBARS)}]
  107. # Indent is the width of the space at the left and right side of the
  108. # colorbar. It is always half the selector polygon width, because the
  109. # polygon extends into the space.
  110. set data(indent) [expr {$data(PLGN_WIDTH) / 2}]
  111. set data(colorPad) 2
  112. set data(selPad) [expr {$data(PLGN_WIDTH) / 2}]
  113. #
  114. # minX is the x coordinate of the first colorbar
  115. #
  116. set data(minX) $data(indent)
  117. #
  118. # maxX is the x coordinate of the last colorbar
  119. #
  120. set data(maxX) [expr {$data(BARS_WIDTH) + $data(indent)-1}]
  121. #
  122. # canvasWidth is the width of the entire canvas, including the indents
  123. #
  124. set data(canvasWidth) [expr {$data(BARS_WIDTH) + $data(PLGN_WIDTH)}]
  125. # Set the initial color, specified by -initialcolor, or the
  126. # color chosen by the user the last time.
  127. set data(selection) $data(-initialcolor)
  128. set data(finalColor) $data(-initialcolor)
  129. set rgb [winfo rgb . $data(selection)]
  130. set data(red,intensity) [expr {[lindex $rgb 0]/0x100}]
  131. set data(green,intensity) [expr {[lindex $rgb 1]/0x100}]
  132. set data(blue,intensity) [expr {[lindex $rgb 2]/0x100}]
  133. }
  134. # ::tk::dialog::color::Config --
  135. #
  136. # Parses the command line arguments to tk_chooseColor
  137. #
  138. proc ::tk::dialog::color::Config {dataName argList} {
  139. variable ::tk::Priv
  140. upvar ::tk::dialog::color::$dataName data
  141. # 1: the configuration specs
  142. #
  143. if {[info exists Priv(selectColor)] && $Priv(selectColor) ne ""} {
  144. set defaultColor $Priv(selectColor)
  145. } else {
  146. set defaultColor [. cget -background]
  147. }
  148. set specs [list \
  149. [list -initialcolor "" "" $defaultColor] \
  150. [list -parent "" "" "."] \
  151. [list -title "" "" [mc "Color"]] \
  152. ]
  153. # 2: parse the arguments
  154. #
  155. tclParseConfigSpec ::tk::dialog::color::$dataName $specs "" $argList
  156. if {$data(-title) eq ""} {
  157. set data(-title) " "
  158. }
  159. if {[catch {winfo rgb . $data(-initialcolor)} err]} {
  160. error $err
  161. }
  162. if {![winfo exists $data(-parent)]} {
  163. error "bad window path name \"$data(-parent)\""
  164. }
  165. }
  166. # ::tk::dialog::color::BuildDialog --
  167. #
  168. # Build the dialog.
  169. #
  170. proc ::tk::dialog::color::BuildDialog {w} {
  171. upvar ::tk::dialog::color::[winfo name $w] data
  172. # TopFrame contains the color strips and the color selection
  173. #
  174. set topFrame [frame $w.top -relief raised -bd 1]
  175. # StripsFrame contains the colorstrips and the individual RGB entries
  176. set stripsFrame [frame $topFrame.colorStrip]
  177. set maxWidth [::tk::mcmaxamp &Red &Green &Blue]
  178. set maxWidth [expr {$maxWidth<6 ? 6 : $maxWidth}]
  179. set colorList {
  180. red "&Red"
  181. green "&Green"
  182. blue "&Blue"
  183. }
  184. foreach {color l} $colorList {
  185. # each f frame contains an [R|G|B] entry and the equiv. color strip.
  186. set f [frame $stripsFrame.$color]
  187. # The box frame contains the label and entry widget for an [R|G|B]
  188. set box [frame $f.box]
  189. ::tk::AmpWidget label $box.label -text "[mc $l]:" \
  190. -width $maxWidth -anchor ne
  191. bind $box.label <<AltUnderlined>> [list focus $box.entry]
  192. entry $box.entry -textvariable \
  193. ::tk::dialog::color::[winfo name $w]($color,intensity) \
  194. -width 4
  195. pack $box.label -side left -fill y -padx 2 -pady 3
  196. pack $box.entry -side left -anchor n -pady 0
  197. pack $box -side left -fill both
  198. set height [expr {
  199. [winfo reqheight $box.entry] -
  200. 2*([$box.entry cget -highlightthickness] + [$box.entry cget -bd])
  201. }]
  202. canvas $f.color -height $height \
  203. -width $data(BARS_WIDTH) -relief sunken -bd 2
  204. canvas $f.sel -height $data(PLGN_HEIGHT) \
  205. -width $data(canvasWidth) -highlightthickness 0
  206. pack $f.color -expand yes -fill both
  207. pack $f.sel -expand yes -fill both
  208. pack $f -side top -fill x -padx 0 -pady 2
  209. set data($color,entry) $box.entry
  210. set data($color,col) $f.color
  211. set data($color,sel) $f.sel
  212. bind $data($color,col) <Configure> \
  213. [list tk::dialog::color::DrawColorScale $w $color 1]
  214. bind $data($color,col) <Enter> \
  215. [list tk::dialog::color::EnterColorBar $w $color]
  216. bind $data($color,col) <Leave> \
  217. [list tk::dialog::color::LeaveColorBar $w $color]
  218. bind $data($color,sel) <Enter> \
  219. [list tk::dialog::color::EnterColorBar $w $color]
  220. bind $data($color,sel) <Leave> \
  221. [list tk::dialog::color::LeaveColorBar $w $color]
  222. bind $box.entry <Return> [list tk::dialog::color::HandleRGBEntry $w]
  223. }
  224. pack $stripsFrame -side left -fill both -padx 4 -pady 10
  225. # The selFrame contains a frame that demonstrates the currently
  226. # selected color
  227. #
  228. set selFrame [frame $topFrame.sel]
  229. set lab [::tk::AmpWidget label $selFrame.lab \
  230. -text [mc "&Selection:"] -anchor sw]
  231. set ent [entry $selFrame.ent \
  232. -textvariable ::tk::dialog::color::[winfo name $w](selection) \
  233. -width 16]
  234. set f1 [frame $selFrame.f1 -relief sunken -bd 2]
  235. set data(finalCanvas) [frame $f1.demo -bd 0 -width 100 -height 70]
  236. pack $lab $ent -side top -fill x -padx 4 -pady 2
  237. pack $f1 -expand yes -anchor nw -fill both -padx 6 -pady 10
  238. pack $data(finalCanvas) -expand yes -fill both
  239. bind $ent <Return> [list tk::dialog::color::HandleSelEntry $w]
  240. pack $selFrame -side left -fill none -anchor nw
  241. pack $topFrame -side top -expand yes -fill both -anchor nw
  242. # the botFrame frame contains the buttons
  243. #
  244. set botFrame [frame $w.bot -relief raised -bd 1]
  245. ::tk::AmpWidget button $botFrame.ok -text [mc "&OK"] \
  246. -command [list tk::dialog::color::OkCmd $w]
  247. ::tk::AmpWidget button $botFrame.cancel -text [mc "&Cancel"] \
  248. -command [list tk::dialog::color::CancelCmd $w]
  249. set data(okBtn) $botFrame.ok
  250. set data(cancelBtn) $botFrame.cancel
  251. grid x $botFrame.ok x $botFrame.cancel x -sticky ew
  252. grid configure $botFrame.ok $botFrame.cancel -padx 10 -pady 10
  253. grid columnconfigure $botFrame {0 4} -weight 1 -uniform space
  254. grid columnconfigure $botFrame {1 3} -weight 1 -uniform button
  255. grid columnconfigure $botFrame 2 -weight 2 -uniform space
  256. pack $botFrame -side bottom -fill x
  257. # Accelerator bindings
  258. bind $lab <<AltUnderlined>> [list focus $ent]
  259. bind $w <KeyPress-Escape> [list tk::ButtonInvoke $data(cancelBtn)]
  260. bind $w <Alt-Key> [list tk::AltKeyInDialog $w %A]
  261. wm protocol $w WM_DELETE_WINDOW [list tk::dialog::color::CancelCmd $w]
  262. bind $lab <Destroy> [list tk::dialog::color::CancelCmd $w]
  263. }
  264. # ::tk::dialog::color::SetRGBValue --
  265. #
  266. # Sets the current selection of the dialog box
  267. #
  268. proc ::tk::dialog::color::SetRGBValue {w color} {
  269. upvar ::tk::dialog::color::[winfo name $w] data
  270. set data(red,intensity) [lindex $color 0]
  271. set data(green,intensity) [lindex $color 1]
  272. set data(blue,intensity) [lindex $color 2]
  273. RedrawColorBars $w all
  274. # Now compute the new x value of each colorbars pointer polygon
  275. foreach color {red green blue} {
  276. set x [RgbToX $w $data($color,intensity)]
  277. MoveSelector $w $data($color,sel) $color $x 0
  278. }
  279. }
  280. # ::tk::dialog::color::XToRgb --
  281. #
  282. # Converts a screen coordinate to intensity
  283. #
  284. proc ::tk::dialog::color::XToRgb {w x} {
  285. upvar ::tk::dialog::color::[winfo name $w] data
  286. set x [expr {($x * $data(intensityIncr))/ $data(colorbarWidth)}]
  287. if {$x > 255} {
  288. set x 255
  289. }
  290. return $x
  291. }
  292. # ::tk::dialog::color::RgbToX
  293. #
  294. # Converts an intensity to screen coordinate.
  295. #
  296. proc ::tk::dialog::color::RgbToX {w color} {
  297. upvar ::tk::dialog::color::[winfo name $w] data
  298. return [expr {($color * $data(colorbarWidth)/ $data(intensityIncr))}]
  299. }
  300. # ::tk::dialog::color::DrawColorScale --
  301. #
  302. # Draw color scale is called whenever the size of one of the color
  303. # scale canvases is changed.
  304. #
  305. proc ::tk::dialog::color::DrawColorScale {w c {create 0}} {
  306. upvar ::tk::dialog::color::[winfo name $w] data
  307. # col: color bar canvas
  308. # sel: selector canvas
  309. set col $data($c,col)
  310. set sel $data($c,sel)
  311. # First handle the case that we are creating everything for the first time.
  312. if {$create} {
  313. # First remove all the lines that already exist.
  314. if { $data(lines,$c,last) > $data(lines,$c,start)} {
  315. for {set i $data(lines,$c,start)} \
  316. {$i <= $data(lines,$c,last)} {incr i} {
  317. $sel delete $i
  318. }
  319. }
  320. # Delete the selector if it exists
  321. if {[info exists data($c,index)]} {
  322. $sel delete $data($c,index)
  323. }
  324. # Draw the selection polygons
  325. CreateSelector $w $sel $c
  326. $sel bind $data($c,index) <ButtonPress-1> \
  327. [list tk::dialog::color::StartMove $w $sel $c %x $data(selPad) 1]
  328. $sel bind $data($c,index) <B1-Motion> \
  329. [list tk::dialog::color::MoveSelector $w $sel $c %x $data(selPad)]
  330. $sel bind $data($c,index) <ButtonRelease-1> \
  331. [list tk::dialog::color::ReleaseMouse $w $sel $c %x $data(selPad)]
  332. set height [winfo height $col]
  333. # Create an invisible region under the colorstrip to catch mouse clicks
  334. # that aren't on the selector.
  335. set data($c,clickRegion) [$sel create rectangle 0 0 \
  336. $data(canvasWidth) $height -fill {} -outline {}]
  337. bind $col <ButtonPress-1> \
  338. [list tk::dialog::color::StartMove $w $sel $c %x $data(colorPad)]
  339. bind $col <B1-Motion> \
  340. [list tk::dialog::color::MoveSelector $w $sel $c %x $data(colorPad)]
  341. bind $col <ButtonRelease-1> \
  342. [list tk::dialog::color::ReleaseMouse $w $sel $c %x $data(colorPad)]
  343. $sel bind $data($c,clickRegion) <ButtonPress-1> \
  344. [list tk::dialog::color::StartMove $w $sel $c %x $data(selPad)]
  345. $sel bind $data($c,clickRegion) <B1-Motion> \
  346. [list tk::dialog::color::MoveSelector $w $sel $c %x $data(selPad)]
  347. $sel bind $data($c,clickRegion) <ButtonRelease-1> \
  348. [list tk::dialog::color::ReleaseMouse $w $sel $c %x $data(selPad)]
  349. } else {
  350. # l is the canvas index of the first colorbar.
  351. set l $data(lines,$c,start)
  352. }
  353. # Draw the color bars.
  354. set highlightW [expr {[$col cget -highlightthickness] + [$col cget -bd]}]
  355. for {set i 0} { $i < $data(NUM_COLORBARS)} { incr i} {
  356. set intensity [expr {$i * $data(intensityIncr)}]
  357. set startx [expr {$i * $data(colorbarWidth) + $highlightW}]
  358. if {$c eq "red"} {
  359. set color [format "#%02x%02x%02x" \
  360. $intensity $data(green,intensity) $data(blue,intensity)]
  361. } elseif {$c eq "green"} {
  362. set color [format "#%02x%02x%02x" \
  363. $data(red,intensity) $intensity $data(blue,intensity)]
  364. } else {
  365. set color [format "#%02x%02x%02x" \
  366. $data(red,intensity) $data(green,intensity) $intensity]
  367. }
  368. if {$create} {
  369. set index [$col create rect $startx $highlightW \
  370. [expr {$startx +$data(colorbarWidth)}] \
  371. [expr {[winfo height $col] + $highlightW}] \
  372. -fill $color -outline $color]
  373. } else {
  374. $col itemconfigure $l -fill $color -outline $color
  375. incr l
  376. }
  377. }
  378. $sel raise $data($c,index)
  379. if {$create} {
  380. set data(lines,$c,last) $index
  381. set data(lines,$c,start) [expr {$index - $data(NUM_COLORBARS) + 1}]
  382. }
  383. RedrawFinalColor $w
  384. }
  385. # ::tk::dialog::color::CreateSelector --
  386. #
  387. # Creates and draws the selector polygon at the position
  388. # $data($c,intensity).
  389. #
  390. proc ::tk::dialog::color::CreateSelector {w sel c } {
  391. upvar ::tk::dialog::color::[winfo name $w] data
  392. set data($c,index) [$sel create polygon \
  393. 0 $data(PLGN_HEIGHT) \
  394. $data(PLGN_WIDTH) $data(PLGN_HEIGHT) \
  395. $data(indent) 0]
  396. set data($c,x) [RgbToX $w $data($c,intensity)]
  397. $sel move $data($c,index) $data($c,x) 0
  398. }
  399. # ::tk::dialog::color::RedrawFinalColor
  400. #
  401. # Combines the intensities of the three colors into the final color
  402. #
  403. proc ::tk::dialog::color::RedrawFinalColor {w} {
  404. upvar ::tk::dialog::color::[winfo name $w] data
  405. set color [format "#%02x%02x%02x" $data(red,intensity) \
  406. $data(green,intensity) $data(blue,intensity)]
  407. $data(finalCanvas) configure -bg $color
  408. set data(finalColor) $color
  409. set data(selection) $color
  410. set data(finalRGB) [list \
  411. $data(red,intensity) \
  412. $data(green,intensity) \
  413. $data(blue,intensity)]
  414. }
  415. # ::tk::dialog::color::RedrawColorBars --
  416. #
  417. # Only redraws the colors on the color strips that were not manipulated.
  418. # Params: color of colorstrip that changed. If color is not [red|green|blue]
  419. # Then all colorstrips will be updated
  420. #
  421. proc ::tk::dialog::color::RedrawColorBars {w colorChanged} {
  422. upvar ::tk::dialog::color::[winfo name $w] data
  423. switch $colorChanged {
  424. red {
  425. DrawColorScale $w green
  426. DrawColorScale $w blue
  427. }
  428. green {
  429. DrawColorScale $w red
  430. DrawColorScale $w blue
  431. }
  432. blue {
  433. DrawColorScale $w red
  434. DrawColorScale $w green
  435. }
  436. default {
  437. DrawColorScale $w red
  438. DrawColorScale $w green
  439. DrawColorScale $w blue
  440. }
  441. }
  442. RedrawFinalColor $w
  443. }
  444. #----------------------------------------------------------------------
  445. # Event handlers
  446. #----------------------------------------------------------------------
  447. # ::tk::dialog::color::StartMove --
  448. #
  449. # Handles a mousedown button event over the selector polygon.
  450. # Adds the bindings for moving the mouse while the button is
  451. # pressed. Sets the binding for the button-release event.
  452. #
  453. # Params: sel is the selector canvas window, color is the color of the strip.
  454. #
  455. proc ::tk::dialog::color::StartMove {w sel color x delta {dontMove 0}} {
  456. upvar ::tk::dialog::color::[winfo name $w] data
  457. if {!$dontMove} {
  458. MoveSelector $w $sel $color $x $delta
  459. }
  460. }
  461. # ::tk::dialog::color::MoveSelector --
  462. #
  463. # Moves the polygon selector so that its middle point has the same
  464. # x value as the specified x. If x is outside the bounds [0,255],
  465. # the selector is set to the closest endpoint.
  466. #
  467. # Params: sel is the selector canvas, c is [red|green|blue]
  468. # x is a x-coordinate.
  469. #
  470. proc ::tk::dialog::color::MoveSelector {w sel color x delta} {
  471. upvar ::tk::dialog::color::[winfo name $w] data
  472. incr x -$delta
  473. if { $x < 0 } {
  474. set x 0
  475. } elseif { $x > $data(BARS_WIDTH)} {
  476. set x $data(BARS_WIDTH)
  477. }
  478. set diff [expr {$x - $data($color,x)}]
  479. $sel move $data($color,index) $diff 0
  480. set data($color,x) [expr {$data($color,x) + $diff}]
  481. # Return the x value that it was actually set at
  482. return $x
  483. }
  484. # ::tk::dialog::color::ReleaseMouse
  485. #
  486. # Removes mouse tracking bindings, updates the colorbars.
  487. #
  488. # Params: sel is the selector canvas, color is the color of the strip,
  489. # x is the x-coord of the mouse.
  490. #
  491. proc ::tk::dialog::color::ReleaseMouse {w sel color x delta} {
  492. upvar ::tk::dialog::color::[winfo name $w] data
  493. set x [MoveSelector $w $sel $color $x $delta]
  494. # Determine exactly what color we are looking at.
  495. set data($color,intensity) [XToRgb $w $x]
  496. RedrawColorBars $w $color
  497. }
  498. # ::tk::dialog::color::ResizeColorbars --
  499. #
  500. # Completely redraws the colorbars, including resizing the
  501. # colorstrips
  502. #
  503. proc ::tk::dialog::color::ResizeColorBars {w} {
  504. upvar ::tk::dialog::color::[winfo name $w] data
  505. if {
  506. ($data(BARS_WIDTH) < $data(NUM_COLORBARS)) ||
  507. (($data(BARS_WIDTH) % $data(NUM_COLORBARS)) != 0)
  508. } then {
  509. set data(BARS_WIDTH) $data(NUM_COLORBARS)
  510. }
  511. InitValues [winfo name $w]
  512. foreach color {red green blue} {
  513. $data($color,col) configure -width $data(canvasWidth)
  514. DrawColorScale $w $color 1
  515. }
  516. }
  517. # ::tk::dialog::color::HandleSelEntry --
  518. #
  519. # Handles the return keypress event in the "Selection:" entry
  520. #
  521. proc ::tk::dialog::color::HandleSelEntry {w} {
  522. upvar ::tk::dialog::color::[winfo name $w] data
  523. set text [string trim $data(selection)]
  524. # Check to make sure that the color is valid
  525. if {[catch {set color [winfo rgb . $text]} ]} {
  526. set data(selection) $data(finalColor)
  527. return
  528. }
  529. set R [expr {[lindex $color 0]/0x100}]
  530. set G [expr {[lindex $color 1]/0x100}]
  531. set B [expr {[lindex $color 2]/0x100}]
  532. SetRGBValue $w "$R $G $B"
  533. set data(selection) $text
  534. }
  535. # ::tk::dialog::color::HandleRGBEntry --
  536. #
  537. # Handles the return keypress event in the R, G or B entry
  538. #
  539. proc ::tk::dialog::color::HandleRGBEntry {w} {
  540. upvar ::tk::dialog::color::[winfo name $w] data
  541. foreach c {red green blue} {
  542. if {[catch {
  543. set data($c,intensity) [expr {int($data($c,intensity))}]
  544. }]} {
  545. set data($c,intensity) 0
  546. }
  547. if {$data($c,intensity) < 0} {
  548. set data($c,intensity) 0
  549. }
  550. if {$data($c,intensity) > 255} {
  551. set data($c,intensity) 255
  552. }
  553. }
  554. SetRGBValue $w "$data(red,intensity) \
  555. $data(green,intensity) $data(blue,intensity)"
  556. }
  557. # mouse cursor enters a color bar
  558. #
  559. proc ::tk::dialog::color::EnterColorBar {w color} {
  560. upvar ::tk::dialog::color::[winfo name $w] data
  561. $data($color,sel) itemconfigure $data($color,index) -fill red
  562. }
  563. # mouse leaves enters a color bar
  564. #
  565. proc ::tk::dialog::color::LeaveColorBar {w color} {
  566. upvar ::tk::dialog::color::[winfo name $w] data
  567. $data($color,sel) itemconfigure $data($color,index) -fill black
  568. }
  569. # user hits OK button
  570. #
  571. proc ::tk::dialog::color::OkCmd {w} {
  572. variable ::tk::Priv
  573. upvar ::tk::dialog::color::[winfo name $w] data
  574. set Priv(selectColor) $data(finalColor)
  575. }
  576. # user hits Cancel button or destroys window
  577. #
  578. proc ::tk::dialog::color::CancelCmd {w} {
  579. variable ::tk::Priv
  580. set Priv(selectColor) ""
  581. }