Version 10 of Get the color of the pixel under the cursor

Updated 2006-01-25 18:17:27

HJG I have some images on a canvas, and I want to get the color of the pixel under the cursor.

Right now, this works only for an image placed on the canvas at 0,0 :


  package require Img

  proc int x { expr int($x) }

  proc ShowPixel {x y i} {
    set c "- - -"
    catch { set c [$::img1 get $x $y] }
    wm title . "$x $y #$i : $c"
  }

  proc ShowColor {w x y} {
  # idea: RS
    set color ""
    set id [$w find withtag current]
    if {$id>0} {
      set xx $x
      set yy $y
      foreach {x0 y0 x1 y1} [$w bbox $id] break
      incr xx -$x0
      incr yy -$x0
     #wm title . "$x $y #$id : $x0 $y0  $x1 $y1 : $xx $yy"
     #return

      switch -- [$w type $id] {
        image {set color [[$w itemcget $id -image] get $xx $yy]}
        line - polygon - rectangle - oval {
            set color [$w itemcget $id -fill]
        }
      }
      wm title . "$x $y #$id $xx $yy = $color"
    } else {
      wm title . "$x $y"
    }

    #return $color
  }

  set fn gosmall.gif
  set img1 [image create photo -file $fn]

  pack [canvas .c]
  .c create image  0  0 -image $img1 -anchor nw -tag Copy1
  .c create image 80 80 -image $img1 -anchor nw -tag Copy2

  .c create rect 125  25  145  45 -fill red 
  .c create oval  25 125   45 145 -fill green
  .c create line  15 100   70 120 -fill blue
  .c create poly 100  65  140  65  100  20 -fill cyan -outline black

 #bind .c <Motion> { ShowPixel    [int [%W canvasx %x]] [int [%W canvasy %y]] [%W find withtag current] }
  bind .c <Motion> { ShowColor %W [int [%W canvasx %x]] [int [%W canvasy %y]] }

Using proc ShowPixel, the RGB-values of the pixel under the cursor are shown only for the first copy of the image...

I can query the coordinates of an item/image on the canvas with bbox (to adjust coordinates within the image vs. the screen-coordinates given by the cursor), but how do I pass the item (as opposed to its id) to the proc ShowPixel ?

RS: Pass in the widget name. For images you can retrieve the name as -image attribute.

HJG: Ok - proc ShowColor now does mostly what I wanted, but gives an error "coordinates out of range" at the edge of the images.

And for rect / oval etc. it would be nice to also get the RGB-values...


Category Graphics - Category Image Processing