Re: Colormaps in pixbufs which are in treeview




On Feb 8, 2007, at 6:33 AM, Ari Jolma wrote:

I have this piece of code:

$pm = Gtk2::Gdk::Pixmap->new(undef,10,10,24);
$gc = new Gtk2::Gdk::GC $pm;
$gc->set_rgb_fg_color(Gtk2::Gdk::Color->new(0,0,0));
$pm->draw_rectangle($gc,1,0,0,10,10);
$pb = Gtk2::Gdk::Pixbuf->get_from_drawable($pm,undef,0,0,0,0,10,10);

The idea is to create small (10x10) pixbufs with varying colors for
inserting them into the model of a treeview. The code works in Linux and
Windows - usually, but in a specific Windows environment the following
error occurs:

[snip]

It looks like I need to call $gc->set_colormap($color_map), and
$color_map = Gtk2::Gdk::Colormap->new ($visual, $allocate) before that,
but what should I use for $visual and $allocate?

How do I test those cases where I need to do that?

You are making it waaaaaaay too complicated. Try using this function, instead:


    #
    # 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)
    }



For example:

#!/usr/bin/perl -w
use strict;
use Gtk2 -init;
use Glib ':constants';


#
# 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)
}



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

my $list = Gtk2::ListStore->new ('Glib::String', 'Gtk2::Gdk::Pixbuf');

foreach my $i (0..8) {
    foreach my $j (0..8) {
        foreach my $k (0..8) {
            my $r = $i * 255 / 8.0;
            my $g = $j * 255 / 8.0;
            my $b = $k * 255 / 8.0;
            $list->set ($list->append,
                        0, sprintf ("#%.2x%.2x%.2x", $r, $g, $b),
1, create_solid_color_pixbuf (12, 12, $r, $g, $b));
        }
    }
}

my $treeview = Gtk2::TreeView->new_with_model ($list);
$treeview->insert_column_with_attributes (-1, '#RGB',
                                          Gtk2::CellRendererText->new,
                                          text => 0);
$treeview->insert_column_with_attributes (-1, "Swatch",
Gtk2::CellRendererPixbuf- >new,
                                          pixbuf => 1);

my $scroller = Gtk2::ScrolledWindow->new;
$scroller->set_policy ('never', 'always');
$scroller->add ($treeview);
$window->add ($scroller);

$window->show_all;
Gtk2->main;

__END__



--
Doing a good job around here is like wetting your pants in a dark suit; you get a warm feeling, but no one notices.
  -- unknown





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