clrpick.tcl
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:21k
源码类别:

通讯编程

开发平台:

Visual C++

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