home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tk42r2x.zip / TclTk / lib / tk4.2 / clrpick.tcl < prev    next >
Text File  |  1999-07-27  |  20KB  |  692 lines

  1. # clrpick.tcl --
  2. #
  3. #    Color selection dialog for platforms that do not support a
  4. #    standard color selection dialog.
  5. #
  6. # SCCS: @(#) clrpick.tcl 1.3 96/09/05 09:59:24
  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.  
  20. # tkColorDialog --
  21. #
  22. #    Create a color dialog and let the user choose a color. This function
  23. #    should not be called directly. It is called by the tk_chooseColor
  24. #    function when a native color selector widget does not exist
  25. #
  26. proc tkColorDialog {args} {
  27.     global tkPriv
  28.     set w .__tk__color
  29.     upvar #0 $w data
  30.  
  31.     # The lines variables track the start and end indices of the line
  32.     # elements in the colorbar canvases.
  33.     set data(lines,red,start)   0
  34.     set data(lines,red,last)   -1
  35.     set data(lines,green,start) 0
  36.     set data(lines,green,last) -1
  37.     set data(lines,blue,start)  0
  38.     set data(lines,blue,last)  -1
  39.  
  40.     # This is the actual number of lines that are drawn in each color strip.
  41.     # Note that the bars may be of any width.
  42.     # However, NUM_COLORBARS must be a number that evenly divides 256.
  43.     # Such as 256, 128, 64, etc.
  44.     set data(NUM_COLORBARS) 8
  45.  
  46.     # BARS_WIDTH is the number of pixels wide the color bar portion of the
  47.     # canvas is. This number must be a multiple of NUM_COLORBARS
  48.     set data(BARS_WIDTH) 128
  49.  
  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.  
  55.     # PLGN_HEIGHT is the height of the selection polygon and the height of the 
  56.     # selection rectangle at the bottom of the color bar. No restrictions.
  57.     set data(PLGN_WIDTH) 10
  58.  
  59.     tkColorDialog_Config $w $args
  60.     tkColorDialog_InitValues $w
  61.  
  62.     if ![winfo exists $w] {
  63.     toplevel $w -class tkColorDialog
  64.     tkColorDialog_BuildDialog $w
  65.     }
  66.     wm transient $w $data(-parent)
  67.  
  68.  
  69.     # 5. Withdraw the window, then update all the geometry information
  70.     # so we know how big it wants to be, then center the window in the
  71.     # display and de-iconify it.
  72.  
  73.     wm withdraw $w
  74.     update idletasks
  75.     set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
  76.     - [winfo vrootx [winfo parent $w]]]
  77.     set y [expr [winfo screenheight $w]/2 - [winfo reqheight $w]/2 \
  78.     - [winfo vrooty [winfo parent $w]]]
  79.     wm geom $w +$x+$y
  80.     wm deiconify $w
  81.     wm title $w $data(-title)
  82.  
  83.     # 6. Set a grab and claim the focus too.
  84.  
  85.     set oldFocus [focus]
  86.     set oldGrab [grab current $w]
  87.     if {$oldGrab != ""} {
  88.     set grabStatus [grab status $oldGrab]
  89.     }
  90.     grab $w
  91.     focus $data(okBtn)
  92.  
  93.     # 7. Wait for the user to respond, then restore the focus and
  94.     # return the index of the selected button.  Restore the focus
  95.     # before deleting the window, since otherwise the window manager
  96.     # may take the focus away so we can't redirect it.  Finally,
  97.     # restore any grab that was in effect.
  98.  
  99.     tkwait variable tkPriv(selectColor)
  100.     catch {focus $oldFocus}
  101.     grab release $w
  102.     destroy $w
  103.     unset data
  104.     if {$oldGrab != ""} {
  105.     if {$grabStatus == "global"} {
  106.         grab -global $oldGrab
  107.     } else {
  108.         grab $oldGrab
  109.     }
  110.     }
  111.     return $tkPriv(selectColor)
  112. }
  113.  
  114. # tkColorDialog_InitValues --
  115. #
  116. #    Get called during initialization or when user resets NUM_COLORBARS
  117. #
  118. proc tkColorDialog_InitValues {w} {
  119.     upvar #0 $w data
  120.  
  121.     # IntensityIncr is the difference in color intensity between a colorbar
  122.     # and its neighbors.
  123.     set data(intensityIncr) [expr 256 / $data(NUM_COLORBARS)]
  124.  
  125.     # ColorbarWidth is the width of each colorbar
  126.     set data(colorbarWidth) \
  127.     [expr $data(BARS_WIDTH) / $data(NUM_COLORBARS)]
  128.  
  129.     # Indent is the width of the space at the left and right side of the
  130.     # colorbar. It is always half the selector polygon width, because the
  131.     # polygon extends into the space.
  132.     set data(indent) [expr $data(PLGN_WIDTH) / 2]
  133.  
  134.     set data(colorPad) 2
  135.     set data(selPad)   [expr $data(PLGN_WIDTH) / 2]
  136.  
  137.     #
  138.     # minX is the x coordinate of the first colorbar
  139.     #
  140.     set data(minX) $data(indent)
  141.  
  142.     #
  143.     # maxX is the x coordinate of the last colorbar
  144.     #
  145.     set data(maxX) [expr $data(BARS_WIDTH) + $data(indent)-1]
  146.  
  147.     #
  148.     # canvasWidth is the width of the entire canvas, including the indents
  149.     #
  150.     set data(canvasWidth) [expr $data(BARS_WIDTH) + \
  151.     $data(PLGN_WIDTH)]
  152.  
  153.     # Set the initial color, specified by -initialcolor, or the
  154.     # color chosen by the user the last time.
  155.     set data(selection) $data(-initialcolor)
  156.     set data(finalColor)  $data(-initialcolor)
  157.     set rgb [winfo rgb . $data(selection)]
  158.  
  159.     set data(red,intensity)   [expr [lindex $rgb 0]/0x100]
  160.     set data(green,intensity) [expr [lindex $rgb 1]/0x100]
  161.     set data(blue,intensity)  [expr [lindex $rgb 2]/0x100]
  162. }
  163.  
  164. # tkColorDialog_Config  --
  165. #
  166. #    Parses the command line arguments to tk_chooseColor
  167. #
  168. proc tkColorDialog_Config {w argList} {
  169.     global tkPriv
  170.     upvar #0 $w data
  171.  
  172.     # 1: the configuration specs
  173.     #
  174.     set specs {
  175.     {-initialcolor "" "" ""}
  176.     {-parent "" "" "."}
  177.     {-title "" "" "Color"}
  178.     }
  179.  
  180.     # 2: parse the arguments
  181.     #
  182.     tclParseConfigSpec $w $specs "" $argList
  183.  
  184.     if ![string compare $data(-title) ""] {
  185.     set data(-title) " "
  186.     }
  187.     if ![string compare $data(-initialcolor) ""] {
  188.     if {[info exists tkPriv(selectColor)] && \
  189.         [string compare $tkPriv(selectColor) ""]} {
  190.         set data(-initialcolor) $tkPriv(selectColor)
  191.     } else {
  192.         set data(-initialcolor) [. cget -background]
  193.     }
  194.     } else {
  195.     if [catch {winfo rgb . $data(-initialcolor)} err] {
  196.         error $err
  197.     }
  198.     }
  199.  
  200.     if ![winfo exists $data(-parent)] {
  201.     error "bad window path name \"$data(-parent)\""
  202.     }
  203. }
  204.  
  205. # tkColorDialog_BuildDialog --
  206. #
  207. #    Build the dialog.
  208. #
  209. proc tkColorDialog_BuildDialog {w} {
  210.     upvar #0 $w data
  211.  
  212.     # TopFrame contains the color strips and the color selection
  213.     #
  214.     set topFrame [frame $w.top -relief raised -bd 1]
  215.  
  216.     # StripsFrame contains the colorstrips and the individual RGB entries
  217.     set stripsFrame [frame $topFrame.colorStrip]
  218.  
  219.     foreach c { Red Green Blue } {
  220.     set color [string tolower $c]
  221.  
  222.     # each f frame contains an [R|G|B] entry and the equiv. color strip.
  223.     set f [frame $stripsFrame.$color]
  224.  
  225.     # The box frame contains the label and entry widget for an [R|G|B]
  226.     set box [frame $f.box]
  227.  
  228.     label $box.label -text $c: -width 6 -under 0 -anchor ne
  229.     entry $box.entry -textvariable [format %s $w]($color,intensity) \
  230.         -width 4
  231.     pack $box.label -side left -fill y -padx 2 -pady 3
  232.     pack $box.entry -side left -anchor n -pady 0
  233.     pack $box -side left -fill both
  234.  
  235.     set height [expr \
  236.         [winfo reqheight $box.entry] - \
  237.         2*([$box.entry cget -highlightthickness] + [$box.entry cget -bd])]
  238.  
  239.     canvas $f.color -height $height\
  240.         -width $data(BARS_WIDTH) -relief sunken -bd 2
  241.     canvas $f.sel -height $data(PLGN_HEIGHT) \
  242.         -width $data(canvasWidth) -highlightthickness 0
  243.     pack $f.color -expand yes -fill both
  244.     pack $f.sel -expand yes -fill both
  245.  
  246.     pack $f -side top -fill x -padx 0 -pady 2
  247.  
  248.     set data($color,entry) $box.entry
  249.     set data($color,col) $f.color
  250.     set data($color,sel) $f.sel
  251.  
  252.     bind $data($color,col) <Configure> \
  253.         "tkColorDialog_DrawColorScale $w $color 1"
  254.     bind $data($color,col) <Enter> \
  255.         "tkColorDialog_EnterColorBar $w $color"
  256.     bind $data($color,col) <Leave> \
  257.         "tkColorDialog_LeaveColorBar $w $color"
  258.  
  259.     bind $data($color,sel) <Enter> \
  260.         "tkColorDialog_EnterColorBar $w $color"
  261.     bind $data($color,sel) <Leave> \
  262.         "tkColorDialog_LeaveColorBar $w $color"
  263.     
  264.     bind $box.entry <Return> "tkColorDialog_HandleRGBEntry $w"
  265.     }
  266.  
  267.     pack $stripsFrame -side left -fill both -padx 4 -pady 10
  268.  
  269.     # The selFrame contains a frame that demonstrates the currently
  270.     # selected color
  271.     #
  272.     set selFrame [frame $topFrame.sel]
  273.     set lab [label $selFrame.lab -text "Selection:" -under 0 -anchor sw]
  274.     set ent [entry $selFrame.ent -textvariable [format %s $w](selection) \
  275.     -width 16]
  276.     set f1  [frame $selFrame.f1 -relief sunken -bd 2]
  277.     set data(finalCanvas) [frame $f1.demo -bd 0 -width 100 -height 70]
  278.  
  279.     pack $lab $ent -side top -fill x -padx 4 -pady 2
  280.     pack $f1 -expand yes -anchor nw -fill both -padx 6 -pady 10
  281.     pack $data(finalCanvas) -expand yes -fill both
  282.  
  283.     bind $ent <Return> "tkColorDialog_HandleSelEntry $w"
  284.  
  285.     pack $selFrame -side left -fill none -anchor nw
  286.     pack $topFrame -side top -expand yes -fill both -anchor nw
  287.  
  288.     # the botFrame frame contains the buttons
  289.     #
  290.     set botFrame [frame $w.bot -relief raised -bd 1]
  291.     button $botFrame.ok     -text OK            -width 8 -under 0 \
  292.     -command "tkColorDialog_OkCmd $w"
  293.     button $botFrame.cancel -text Cancel        -width 8 -under 0 \
  294.     -command "tkColorDialog_CancelCmd $w"
  295.  
  296.     set data(okBtn)      $botFrame.ok
  297.     set data(cancelBtn)  $botFrame.cancel
  298.  
  299.     pack $botFrame.ok $botFrame.cancel \
  300.     -padx 10 -pady 10 -expand yes -side left
  301.     pack $botFrame -side bottom -fill x
  302.  
  303.  
  304.     # Accelerator bindings
  305.  
  306.     bind $w <Alt-r> "focus $data(red,entry)"
  307.     bind $w <Alt-g> "focus $data(green,entry)"
  308.     bind $w <Alt-b> "focus $data(blue,entry)"
  309.     bind $w <Alt-s> "focus $ent"
  310.     bind $w <KeyPress-Escape> "tkButtonInvoke $data(cancelBtn)"
  311.     bind $w <Alt-c> "tkButtonInvoke $data(cancelBtn)"
  312.     bind $w <Alt-o> "tkButtonInvoke $data(okBtn)"
  313.  
  314.     wm protocol $w WM_DELETE_WINDOW "tkColorDialog_CancelCmd $w"
  315. }
  316.  
  317. # tkColorDialog_SetRGBValue --
  318. #
  319. #    Sets the current selection of the dialog box
  320. #
  321. proc tkColorDialog_SetRGBValue {w color} {
  322.     upvar #0 $w data 
  323.  
  324.     set data(red,intensity)   [lindex $color 0]
  325.     set data(green,intensity) [lindex $color 1]
  326.     set data(blue,intensity)  [lindex $color 2]
  327.     
  328.     tkColorDialog_RedrawColorBars $w all
  329.  
  330.     # Now compute the new x value of each colorbars pointer polygon
  331.     foreach color { red green blue } {
  332.     set x [tkColorDialog_RgbToX $w $data($color,intensity)]
  333.     tkColorDialog_MoveSelector $w $data($color,sel) $color $x 0
  334.     }
  335. }
  336.  
  337. # tkColorDialog_XToRgb --
  338. #
  339. #    Converts a screen coordinate to intensity
  340. #
  341. proc tkColorDialog_XToRgb {w x} {
  342.     upvar #0 $w data
  343.     
  344.     return [expr ($x * $data(intensityIncr))/ $data(colorbarWidth)]
  345. }
  346.  
  347. # tkColorDialog_RgbToX
  348. #
  349. #    Converts an intensity to screen coordinate.
  350. #
  351. proc tkColorDialog_RgbToX {w color} {
  352.     upvar #0 $w data
  353.     
  354.     return [expr ($color * $data(colorbarWidth)/ $data(intensityIncr))]
  355. }
  356.  
  357.  
  358. # tkColorDialog_DrawColorScale --
  359. #    Draw color scale is called whenever the size of one of the color
  360. #    scale canvases is changed.
  361. #
  362. proc tkColorDialog_DrawColorScale {w c {create 0}} {
  363.     global lines
  364.     upvar #0 $w data
  365.  
  366.     # col: color bar canvas
  367.     # sel: selector canvas
  368.     set col $data($c,col)
  369.     set sel $data($c,sel)
  370.  
  371.     # First handle the case that we are creating everything for the first time.
  372.     if $create {
  373.     # First remove all the lines that already exist.
  374.     if { $data(lines,$c,last) > $data(lines,$c,start)} {
  375.         for {set i $data(lines,$c,start)} \
  376.         {$i <= $data(lines,$c,last)} { incr i} {
  377.         $sel delete $i
  378.         }
  379.     }
  380.     # Delete the selector if it exists
  381.     if [info exists data($c,index)] {
  382.         $sel delete $data($c,index)
  383.     }
  384.     
  385.     # Draw the selection polygons
  386.     tkColorDialog_CreateSelector $w $sel $c
  387.     $sel bind $data($c,index) <ButtonPress-1> \
  388.         "tkColorDialog_StartMove $w $sel $c %x $data(selPad) 1"
  389.     $sel bind $data($c,index) <B1-Motion> \
  390.         "tkColorDialog_MoveSelector $w $sel $c %x $data(selPad)"
  391.     $sel bind $data($c,index) <ButtonRelease-1> \
  392.         "tkColorDialog_ReleaseMouse $w $sel $c %x $data(selPad)"
  393.  
  394.     set height [winfo height $col]
  395.     # Create an invisible region under the colorstrip to catch mouse clicks
  396.     # that aren't on the selector.
  397.     set data($c,clickRegion) [$sel create rectangle 0 0 \
  398.         $data(canvasWidth) $height -fill {} -outline {}]
  399.  
  400.     bind $col <ButtonPress-1> \
  401.         "tkColorDialog_StartMove $w $sel $c %x $data(colorPad)"
  402.     bind $col <B1-Motion> \
  403.         "tkColorDialog_MoveSelector $w $sel $c %x $data(colorPad)"
  404.     bind $col <ButtonRelease-1> \
  405.         "tkColorDialog_ReleaseMouse $w $sel $c %x $data(colorPad)"
  406.  
  407.     $sel bind $data($c,clickRegion) <ButtonPress-1> \
  408.         "tkColorDialog_StartMove $w $sel $c %x $data(selPad)"
  409.     $sel bind $data($c,clickRegion) <B1-Motion> \
  410.         "tkColorDialog_MoveSelector $w $sel $c %x $data(selPad)"
  411.     $sel bind $data($c,clickRegion) <ButtonRelease-1> \
  412.         "tkColorDialog_ReleaseMouse $w $sel $c %x $data(selPad)"
  413.     } else {
  414.     # l is the canvas index of the first colorbar.
  415.     set l $data(lines,$c,start)
  416.     }
  417.     
  418.     # Draw the color bars.
  419.     set highlightW [expr \
  420.     [$col cget -highlightthickness] + [$col cget -bd]]
  421.     for {set i 0} { $i < $data(NUM_COLORBARS)} { incr i} {
  422.     set intensity [expr $i * $data(intensityIncr)]
  423.     set startx [expr $i * $data(colorbarWidth) + $highlightW]
  424.     if { $c == "red" } {
  425.         set color [format "#%02x%02x%02x" \
  426.                $intensity \
  427.                $data(green,intensity) \
  428.                $data(blue,intensity)]
  429.     } elseif { $c == "green" } {
  430.         set color [format "#%02x%02x%02x" \
  431.                $data(red,intensity) \
  432.                $intensity \
  433.                $data(blue,intensity)]
  434.     } else {
  435.         set color [format "#%02x%02x%02x" \
  436.                $data(red,intensity) \
  437.                $data(green,intensity) \
  438.                $intensity]
  439.     }
  440.  
  441.     if $create {
  442.         set index [$col create rect $startx $highlightW \
  443.         [expr $startx +$data(colorbarWidth)] \
  444.         [expr [winfo height $col] + $highlightW]\
  445.             -fill $color -outline $color]
  446.     } else {
  447.         $col itemconf $l -fill $color -outline $color
  448.         incr l
  449.     }
  450.     }
  451.     $sel raise $data($c,index)
  452.  
  453.     if $create {
  454.     set data(lines,$c,last) $index
  455.     set data(lines,$c,start) [expr $index - $data(NUM_COLORBARS) + 1 ]
  456.     }
  457.  
  458.     tkColorDialog_RedrawFinalColor $w
  459. }
  460.  
  461. # tkColorDialog_CreateSelector --
  462. #
  463. #    Creates and draws the selector polygon at the position
  464. #    $data($c,intensity).
  465. #
  466. proc tkColorDialog_CreateSelector {w sel c } {
  467.     upvar #0 $w data
  468.     set data($c,index) [$sel create polygon \
  469.     0 $data(PLGN_HEIGHT) \
  470.     $data(PLGN_WIDTH) $data(PLGN_HEIGHT) \
  471.     $data(indent) 0]
  472.     set data($c,x) [tkColorDialog_RgbToX $w $data($c,intensity)]
  473.     $sel move $data($c,index) $data($c,x) 0
  474. }
  475.  
  476. # tkColorDialog_RedrawFinalColor
  477. #
  478. #    Combines the intensities of the three colors into the final color
  479. #
  480. proc tkColorDialog_RedrawFinalColor {w} {
  481.     upvar #0 $w data
  482.  
  483.     set color [format "#%02x%02x%02x" $data(red,intensity) \
  484.     $data(green,intensity) $data(blue,intensity)]
  485.     
  486.     $data(finalCanvas) conf -bg $color
  487.     set data(finalColor) $color
  488.     set data(selection) $color
  489.     set data(finalRGB) [list \
  490.     $data(red,intensity) \
  491.     $data(green,intensity) \
  492.     $data(blue,intensity)]
  493. }
  494.  
  495. # tkColorDialog_RedrawColorBars --
  496. #
  497. # Only redraws the colors on the color strips that were not manipulated.
  498. # Params: color of colorstrip that changed. If color is not [red|green|blue]
  499. #         Then all colorstrips will be updated
  500. #
  501. proc tkColorDialog_RedrawColorBars {w colorChanged} {
  502.     upvar #0 $w data
  503.  
  504.     switch $colorChanged {
  505.     red { 
  506.         tkColorDialog_DrawColorScale $w green
  507.         tkColorDialog_DrawColorScale $w blue
  508.     }
  509.     green {
  510.         tkColorDialog_DrawColorScale $w red
  511.         tkColorDialog_DrawColorScale $w blue
  512.     }
  513.     blue {
  514.         tkColorDialog_DrawColorScale $w red
  515.         tkColorDialog_DrawColorScale $w green
  516.     }
  517.     default {
  518.         tkColorDialog_DrawColorScale $w red
  519.         tkColorDialog_DrawColorScale $w green
  520.         tkColorDialog_DrawColorScale $w blue
  521.     }
  522.     }
  523.     tkColorDialog_RedrawFinalColor $w
  524. }
  525.  
  526. #----------------------------------------------------------------------
  527. #            Event handlers
  528. #----------------------------------------------------------------------
  529.  
  530. # tkColorDialog_StartMove --
  531. #
  532. #    Handles a mousedown button event over the selector polygon.
  533. #    Adds the bindings for moving the mouse while the button is
  534. #    pressed.  Sets the binding for the button-release event.
  535. # Params: sel is the selector canvas window, color is the color of the strip.
  536. #
  537. proc tkColorDialog_StartMove {w sel color x delta {dontMove 0}} {
  538.     upvar #0 $w data
  539.  
  540.     if !$dontMove {
  541.     tkColorDialog_MoveSelector $w $sel $color $x $delta
  542.     }
  543. }
  544.  
  545. # tkColorDialog_MoveSelector --
  546. # Moves the polygon selector so that its middle point has the same
  547. # x value as the specified x. If x is outside the bounds [0,255],
  548. # the selector is set to the closest endpoint.
  549. #
  550. # Params: sel is the selector canvas, c is [red|green|blue]
  551. #         x is a x-coordinate.
  552. #
  553. proc tkColorDialog_MoveSelector {w sel color x delta} {
  554.     upvar #0 $w data
  555.  
  556.     incr x -$delta
  557.  
  558.     if { $x < 0 } {
  559.     set x 0
  560.     } elseif { $x >= $data(BARS_WIDTH)} {
  561.     set x [expr $data(BARS_WIDTH) - 1]
  562.     }
  563.     set diff [expr  $x - $data($color,x)]
  564.     $sel move $data($color,index) $diff 0
  565.     set data($color,x) [expr $data($color,x) + $diff]
  566.     
  567.     # Return the x value that it was actually set at
  568.     return $x
  569. }
  570.  
  571. # tkColorDialog_ReleaseMouse
  572. #
  573. # Removes mouse tracking bindings, updates the colorbars.
  574. #
  575. # Params: sel is the selector canvas, color is the color of the strip,
  576. #         x is the x-coord of the mouse.
  577. #
  578. proc tkColorDialog_ReleaseMouse {w sel color x delta} {
  579.     upvar #0 $w data 
  580.  
  581.     set x [tkColorDialog_MoveSelector $w $sel $color $x $delta]
  582.     
  583.     # Determine exactly what color we are looking at.
  584.     set data($color,intensity) [tkColorDialog_XToRgb $w $x]
  585.  
  586.     tkColorDialog_RedrawColorBars $w $color
  587. }
  588.  
  589. # tkColorDialog_ResizeColorbars --
  590. #
  591. #    Completely redraws the colorbars, including resizing the
  592. #    colorstrips
  593. #
  594. proc tkColorDialog_ResizeColorBars {w} {
  595.     upvar #0 $w data
  596.     
  597.     if { ($data(BARS_WIDTH) < $data(NUM_COLORBARS)) || 
  598.      (($data(BARS_WIDTH) % $data(NUM_COLORBARS)) != 0)} {
  599.     set data(BARS_WIDTH) $data(NUM_COLORBARS)
  600.     }
  601.     tkColorDialog_InitValues $w
  602.     foreach color { red green blue } {
  603.     $data($color,col) conf -width $data(canvasWidth)
  604.     tkColorDialog_DrawColorScale $w $color 1
  605.     }
  606. }
  607.  
  608. # tkColorDialog_HandleSelEntry --
  609. #
  610. #    Handles the return keypress event in the "Selection:" entry
  611. #
  612. proc tkColorDialog_HandleSelEntry {w} {
  613.     upvar #0 $w data
  614.  
  615.     set text [string trim $data(selection)]
  616.     # Check to make sure that the color is valid
  617.     if [catch {set color [winfo rgb . $text]} ] {
  618.     set data(selection) $data(finalColor)
  619.     return
  620.     }
  621.     
  622.     set R [expr [lindex $color 0]/0x100]
  623.     set G [expr [lindex $color 1]/0x100]
  624.     set B [expr [lindex $color 2]/0x100]
  625.  
  626.     tkColorDialog_SetRGBValue $w "$R $G $B"
  627.     set data(selection) $text
  628. }
  629.  
  630. # tkColorDialog_HandleRGBEntry --
  631. #
  632. #    Handles the return keypress event in the R, G or B entry
  633. #
  634. proc tkColorDialog_HandleRGBEntry {w} {
  635.     upvar #0 $w data
  636.  
  637.     foreach c {red green blue} {
  638.     if [catch {
  639.         set data($c,intensity) [expr int($data($c,intensity))]
  640.     }] {
  641.         set data($c,intensity) 0
  642.     }
  643.  
  644.     if {$data($c,intensity) < 0} {
  645.         set data($c,intensity) 0
  646.     }
  647.     if {$data($c,intensity) > 255} {
  648.         set data($c,intensity) 255
  649.     }
  650.     }
  651.  
  652.     tkColorDialog_SetRGBValue $w "$data(red,intensity) $data(green,intensity) \
  653.     $data(blue,intensity)"
  654. }    
  655.  
  656. # mouse cursor enters a color bar
  657. #
  658. proc tkColorDialog_EnterColorBar {w color} {
  659.     upvar #0 $w data
  660.  
  661.     $data($color,sel) itemconfig $data($color,index) -fill red
  662. }
  663.  
  664. # mouse leaves enters a color bar
  665. #
  666. proc tkColorDialog_LeaveColorBar {w color} {
  667.     upvar #0 $w data
  668.  
  669.     $data($color,sel) itemconfig $data($color,index) -fill black
  670. }
  671.  
  672. # user hits OK button
  673. #
  674. proc tkColorDialog_OkCmd {w} {
  675.     global tkPriv
  676.     upvar #0 $w data
  677.  
  678.     set tkPriv(selectColor) $data(finalColor)
  679. }
  680.  
  681. # user hits Cancel button
  682. #
  683. proc tkColorDialog_CancelCmd {w} {
  684.     global tkPriv
  685.  
  686.     set tkPriv(selectColor) ""
  687. }
  688.  
  689.