#!/usr/bin/perl ###################################################################### # An example of how to get rgb value in a motion callback of # the image shown in Gtk2::Image. # # Dov Grobgeld # 2005-08-28 # # This code is in the public domain. ###################################################################### use strict; use Gtk2 '-init'; sub get_rgb { my($img, $x,$y) = @_; return unpack("C3", $img->new_subpixbuf($x,$y,1,1)->get_pixels()); } sub cb_image_motion { my ($this, $event, $user_data) = @_; my ($label, $pb) = @$user_data; my $x = $event->x; my $y = $event->y; my $info = sprintf("Coord: (%3d,%3d) rgb: (%3d,%3d,%3d)",$x,$y, get_rgb($pb, $x,$y)); $label->set(label=>$info); return 1; } sub create_widgets { my $filename = shift || 'bridget-1.jpg'; my $mw = Gtk2::Window->new(); $mw->signal_connect (delete_event => sub {Gtk2->main_quit;}); # create a vbox for the image and a label my $vbox = Gtk2::VBox->new(); $mw->add($vbox); # Create an eventbox and put the image inside it my $event_box = Gtk2::EventBox->new(); my $image = Gtk2::Image->new(); $vbox->pack_start($event_box, 1,1,0); $event_box->add($image); # Load an image into a pixbuf. This gives more flexibility in # manipulating the image than using an Gdk::Pixmap. my $pb = new_from_file Gtk2::Gdk::Pixbuf($filename); die "Failed loading test image!\n" unless $pb; # create info label my $label = Gtk2::Label->new($filename); $vbox->pack_start($label, 0,0,0); # Load the image into the image widget and resize it so that # it fits the image $image->set_from_pixbuf($pb); $image->set_size_request($pb->get_width, $pb->get_height); # Make event box accept motion events $event_box->set_events(["pointer-motion-mask"]); # setup motion callback for the image widget $event_box->signal_connect( "motion-notify-event"=>\&cb_image_motion,[$label,$pb]); $mw->show_all(); } create_widgets(@ARGV); Gtk2->main();