clrpick.tcl 21 KB

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