Re: setting a transparent color on a pixbuf for Canvas layering



On Tue, 01 Jul 2008 15:00:28 +0200
Quentin Sculo <squentin free fr> wrote:

On Mon, 2008-06-30 at 17:52 -0400, zentara wrote:

How do I take the non-transparent zzrgbwb.png , load it with Gtk2, and
make green transparent without resorting to GD?

Have you tried gdk_pixbuf_add_alpha ?
http://gtk2-perl.sourceforge.net/doc/pod/Gtk2/Gdk/Pixbuf.html#pixbuf_pixbuf_add_al
http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-util.html#gdk-pixbuf-add-alpha

Quentin

Thanks for the push in the right direction!  I did try gdk_pixbuf_add_alpha
but I was using it wrong. My first mistake wwas to use hex values for rgb, instead of
0..255. My second mistake was to assume gdk_pixbuf_add_alpha operated on the
pixbuf that called it, but it actually returns a new pixbuf.

So thanks, it works now.

#!/usr/bin/perl -w
use strict;
use warnings;
use Goo::Canvas;
use Gtk2 '-init';
use Glib qw(TRUE FALSE);

my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_default_size(640, 600);

my $swin = Gtk2::ScrolledWindow->new;
$swin->set_shadow_type('in');
$window->add($swin);

my $canvas = Goo::Canvas->new();
$canvas->set_size_request(600, 450);
$canvas->set_bounds(0, 0, 1000, 1000);
$swin->add($canvas);

my $root = $canvas->get_root_item();

# base file
my $im = Gtk2::Gdk::Pixbuf->new_from_file("MI.png");
my $w = $im->get_width;
my $h = $im->get_height;
my $image = Goo::Canvas::Image->new(
$root, $im, 0, 0,
'width' => $w,
'height' => $h);

# overlay file which needs transparency added
my $im1 = Gtk2::Gdk::Pixbuf->new_from_file("zzrgbwb.png");
my $im2 = $im1->add_alpha(1,0 ,255 , 0); # returns a new pixbuf
# makes green transparent

#$im1->render_pixmap_and_mask (44);
#gdk_pixbuf_add_alpha ()
#GdkPixbuf*          gdk_pixbuf_add_alpha (const GdkPixbuf *pixbuf,
#gboolean substitute_color,
#guchar r,
#guchar g,
#guchar b);

#Takes an existing pixbuf and adds an alpha channel to it. If the existing pixbuf already had an alpha 
channel, the channel values are copied from the original; otherwise, the alpha channel is initialized to 255 
(full opacity).
#If substitute_color is TRUE, then the color specified by (r, g, b) will be assigned zero opacity. That is, 
if you pass (255, 255, 255) for the substitute color, all white pixels will become fully transparent.

#pixbuf : A GdkPixbuf.
#substitute_color : Whether to set a color to zero opacity. If this is FALSE, then the (r, g, b) arguments 
will be ignored.
#r : Red value to substitute.
#g : Green value to substitute.
#b : Blue value to substitute.
#Returns : A newly-created pixbuf with a reference count of 1.

my $w1 = $im2->get_width;
my $h1 = $im2->get_height;
my $image1 = Goo::Canvas::Image->new(
$root, $im2, 50, 50,
'width' => $w,
'height' => $h);


$window->show_all();
Gtk2->main;
__END__


zentara

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



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