[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: setting Gtk2::Gdk::Color pixel
- From: muppet <scott asofyet org>
- To: gtk2-perl List <gtk-perl-list gnome org>
- Subject: Re: setting Gtk2::Gdk::Color pixel
- Date: Fri, 16 Nov 2007 22:49:05 -0500
On Nov 16, 2007, at 6:40 PM, Kevin Ryde wrote:
Ie. for xors or other crazy GCFunction operations I think the GdkColor
is not a colour, just a way to pass a pixel value with the bits you
want
to flip or set or whatever.
(I imagine apart from xors most such things are only really useful
for a
pseudocolor where you've setup particular colours in particular pixels
of the colormap. Maybe on a truecolor you could OR in some extra
red on
top of other drawing, or something like that though.)
XOR is useful for reversible drawing. I swear, the white+xor trick
works. I just wrote the attached script, and used it to create the
attached screenshot. Load your favorite picture (watch out -- scaling
and scrolling are left as an exercise for you) and drag button 1 around.
Back in the olden days, fancy alpha compositing and such was simply
too expensive for interactive operations like rubber-band selection.
XOR allowed you to make a reversible change to the target very
cheaply. Doing and XOR bitwise operation on a 24-bit color value
flips all the bits and typically gives you the opposite RGB color.
This is nice because you can see what you're doing... mostly. Doesn't
work so great on grays.
#!/usr/bin/perl -w
use strict;
use Gtk2 -init;
my $image = Gtk2::Image->new_from_file ($ARGV[0]);
my $window = Gtk2::Window->new;
my $eventbox = Gtk2::EventBox->new;
$eventbox->signal_connect_after (realize => sub {
my ($widget) = @_;
warn "realize\n";
$widget->{drag_gc} = Gtk2::Gdk::GC->new ($widget->window);
$widget->{drag_gc}->set_function ('xor');
$widget->{drag_gc}->set_foreground ($widget->style->white);
});
$eventbox->signal_connect_after (unrealize => sub {
my ($widget) = @_;
delete $widget->{drag_gc};
});
sub min { $_[0] < $_[1] ? $_[0] : $_[1] }
sub draw_rubber_band_box {
my ($widget) = @_;
my $x = min ($widget->{draginfo}{x1}, $widget->{draginfo}{x2});
my $y = min ($widget->{draginfo}{y1}, $widget->{draginfo}{y2});
my $w = abs ($widget->{draginfo}{x2} - $widget->{draginfo}{x1});
my $h = abs ($widget->{draginfo}{y2} - $widget->{draginfo}{y1});
$widget->window->draw_rectangle
($widget->{drag_gc}, 1, $x, $y, $w, $h);
}
$eventbox->signal_connect (button_press_event => sub {
my ($widget, $event) = @_;
if ($event->button == 1) {
$widget->{draginfo} = {
x1 => $event->x,
y1 => $event->y,
x2 => $event->x,
y2 => $event->y,
};
# Draw...
draw_rubber_band_box ($widget);
}
return 0;
});
$eventbox->signal_connect (motion_notify_event => sub {
my ($widget, $event) = @_;
if ($widget->{draginfo}) {
# Erase...
draw_rubber_band_box ($widget);
$widget->{draginfo}{x2} = $event->x;
$widget->{draginfo}{y2} = $event->y;
# Draw new...
draw_rubber_band_box ($widget);
}
return 0;
});
$eventbox->signal_connect (button_release_event => sub {
my ($widget, $event) = @_;
if ($widget->{draginfo}) {
# Erase...
draw_rubber_band_box ($widget);
}
# Step out of rubber-band mode.
delete $widget->{draginfo};
return 0;
});
$eventbox->add ($image);
$window->add ($eventbox);
$window->show_all ();
$window->signal_connect (destroy => sub { Gtk2->main_quit () });
Gtk2->main ();

(For those who are curious, yes, those are my kids. The picture is
from around Halloween in '06.)
--
elysse: You dance better than some.
me: "Some" what?
elysse: Some asparagus.
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]