Re: setting Gtk2::Gdk::Color pixel




On Nov 17, 2007, at 6:28 PM, Kevin Ryde wrote:

But like I say I've got in mind flipping between particular colours, so
as to maybe get orange lasso on white background, or red on grey or
something like that, having some knowledge of the background colour. So
for that I'd like to explicitly set the pixel field value in a
Gtk2::Gdk::Color to go in the GC, if that could be possible ... somehow
...

At last i realize that i misunderstood your original message. D'oh. In my experience, you don't try to take that kind of knowledge of the background; either do the plain XOR or expect to clear and redraw the background.

The value in GdkColor.pixel is assigned by gdk when the color is allocated, and its value is dependent on the current visual and all that fun stuff. On my truecolor system, the pixel value is simply a packed rgb value. This code

    my ($r, $g, $b) = (127, 195, 255);
    my $c = Gtk2::Gdk::Color->new ($r<<8, $g<<8, $b<<8);
    $widget->get_colormap->alloc_color ($c, 1, 1);
printf "r,g,b = %02x,%02x,%02x pixel=%08x\n", $r, $g, $b, $c- >pixel;
    $widget->{drag_gc}->set_foreground ($c);

gives me

r,g,b = 7f,c3,ff  pixel=007fc3ff


This may give you what you want.  Or, you can investigate Cairo...


Back in the olden days, fancy alpha compositing and such was simply
too expensive for interactive operations like rubber-band selection.

Yes, that's about my level, I don't really know anything about what
people do with partial transparency and whatnot these days.  It seems
like a great way to slow down your computer and stop 8-bit screens
working :).


Doing a selection rectangle with transparency pretty much requires using Cairo. This code isn't very good, but gets the job done....



#!/usr/bin/perl -w
use strict;
use Gtk2 -init;
use Cairo;
my $image = Gtk2::Image->new_from_file ($ARGV[0]);
my $window = Gtk2::Window->new;
my $eventbox = Gtk2::EventBox->new;

sub min { $_[0] < $_[1] ? $_[0] : $_[1] }
sub max { $_[0] > $_[1] ? $_[0] : $_[1] }

$eventbox->signal_connect_after (expose_event => sub {
    my ($widget, $event) = @_;
    if ($widget->{rubberband}) {
        my ($widget) = @_;
        my $x = $widget->{rubberband}{x};
        my $y = $widget->{rubberband}{y};
        my $w = $widget->{rubberband}{width};
        my $h = $widget->{rubberband}{height};

        my ($r, $g, $b, $a) = (0.9, 0.9, 0.9, 0.5);

        my $cr = Gtk2::Gdk::Cairo::Context->create ($widget->window);
        $cr->save;

        # Fill with transparency
        $cr->set_source_rgba ($r, $g, $b, $a);
        $cr->rectangle ($x, $y, $w, $h);
        $cr->clip;
        $cr->paint;

        # Outline without transparency
        $cr->set_source_rgb ($r, $g, $b);
        $cr->rectangle ($x + 0.5, $y + 0.5, $w - 1, $h - 1);
        $cr->stroke;

        $cr->restore;
    }
    return 0;
});

sub update {
    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});

    my $oldx = $widget->{rubberband}{x} || $x;
    my $oldy = $widget->{rubberband}{y} || $y;
    my $oldw = $widget->{rubberband}{width} || $w;
    my $oldh = $widget->{rubberband}{height} || $h;

    $widget->{rubberband}{x} = $x;
    $widget->{rubberband}{y} = $y;
    $widget->{rubberband}{width} = $w;
    $widget->{rubberband}{height} = $h;

    $widget->queue_draw_area (min ($oldx, $x), min ($oldy, $y),
                              max ($oldw, $w), max ($oldh, $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,
        };
        update ($widget);
    }

    return 0;
});

$eventbox->signal_connect (motion_notify_event => sub {
    my ($widget, $event) = @_;

    if ($widget->{draginfo}) {
        $widget->{draginfo}{x2} = $event->x;
        $widget->{draginfo}{y2} = $event->y;
        update ($widget);
    }

    return 0;
});

$eventbox->signal_connect (button_release_event => sub {
    my ($widget, $event) = @_;

    delete $widget->{draginfo};
    # Step out of rubber-band mode.
    delete $widget->{rubberband};

    $widget->queue_draw ();

    return 0;
});

$eventbox->add ($image);
#$window->set_default_size (400, 400);
$window->add ($eventbox);
$window->show_all ();
$window->signal_connect (destroy => sub { Gtk2->main_quit () });
Gtk2->main ();



--
One, two, free, four, five, six, sebben, eight, nine, ten, elebben, twull, fourteen, sickteen, sebbenteen, eightteen, elebbenteen, fiffeen, elebbenteen!
  -- Zella, aged three, counting to twenty.





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]