Re: Colormaps in pixbufs which are in treeview



On Tue, 13 Feb 2007 23:17:41 +0000
Emmanuele Bassi <ebassi gmail com> wrote:

On Tue, 2007-02-13 at 17:35 -0500, muppet wrote:
You are making it waaaaaaay too complicated.  Try using this  
function, instead:

even this might be seen as "complicated", when there's a nice method for
Gtk2::Gdk::Pixbuf like fill():
Emmanuele.

For my own instruction and edification, and for future new users
looking for examples, I put the 2 methods shown by muppet and
Emmanuel side by side in a working script.

Emmanuel's seems more useful since it takes $alpha values too.
(but maybe muppet can improve his to take alpha :-)  )

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw(FALSE TRUE);
use Gtk2 -init;

# method1 by muppet
# Width and height are in pixels.
# Red, green, and blue are assumed to be [0, 255].
sub create_solid_color_pixbuf {
  my ($width, $height, $red, $green, $blue) = @_;
  my $data = pack "C*", ($red, $green, $blue) x ($width * $height);
  return Gtk2::Gdk::Pixbuf->new_from_data ($data, 'rgb', 
           FALSE, 8, $width, $height,  $width * 3)
}

# method2 by Emmanuel Bassi  (with alpha )
#even this might be seen as "complicated", when there's a nice method for
#Gtk2::Gdk::Pixbuf like fill():
sub create_solid_color_pixbuf1 {
    my ($width, $height, $red, $green, $blue,$alpha) = @_;
   # $red, $green, $blue and $alpha preconditions hold as above
  my $packed = $alpha
             | $blue  << 8
             | $green << 16
             | $red   << 24;
  my $pixbuf = Gtk2::Gdk::Pixbuf->new('rgb', TRUE, 8, $width, $height);
  $pixbuf->fill($packed);
  return $pixbuf;
}

my $window = Gtk2::Window->new;
$window->signal_connect (delete_event => sub {Gtk2->main_quit;});

my $pixbuf = create_solid_color_pixbuf(100,100,128,0,128);
my $image = Gtk2::Image->new_from_pixbuf ($pixbuf);

#with alpha layer
my $pixbuf1 = create_solid_color_pixbuf1(100,100,0,128,128,32);
my $image1 = Gtk2::Image->new_from_pixbuf($pixbuf1);

my $hbox = Gtk2::HBox->new;
$hbox->pack_start ($image, FALSE, FALSE, 6);
$hbox->pack_start ($image1, FALSE, FALSE, 6);

$window->add ($hbox);
$window->set_title ("Simple Color Pixbufs");
$window->show_all;

Gtk2->main;
__END__


zentara

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



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