|
List Info
Thread: Colormaps in pixbufs which are in treeview
|
|
| Colormaps in pixbufs which are in
treeview |
  Finland |
2007-02-08 05:33:11 |
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:
Gdk-WARNING **: gdk_gc_set_rgb_fg_color() and
gdk_gc_set_rgb_bg_color() can
only be used on GC's with a colormap. A GC will have a
colormap
if it is created for a drawable with a colormap, or if a
colormap has been set explicitly with gdk_gc_set_colormap.
Gdk-WARNING **: ../../../gtk+/gdk/gdkpixbuf-drawable.c:1249:
Source
drawable has
no colormap; either pass in a colormap, or set the colormap
on the
drawable wit
h gdk_drawable_set_colormap()
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?
Best regards,
Ari
--
Prof. Ari Jolma
Geoinformaatio- ja paikannustekniikka
Geoinformation and positioning technology
Teknillinen Korkeakoulu / Helsinki University of Technology
POBox 1200, 02015 TKK, Finland
Email: ari.jolma at tkk.fi URL: http://www.tkk.fi/~jolma
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
|
|
| Re: Colormaps in pixbufs which are in
treeview |
  United States |
2007-02-13 07:08:07 |
On Thu, 08 Feb 2007 13:33:11 +0200
Ari Jolma <ari.jolma TKK.FI> 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:
>
>Gdk-WARNING **: gdk_gc_set_rgb_fg_color() and
gdk_gc_set_rgb_bg_color() can
>only be used on GC's with a colormap. A GC will have a
colormap
>if it is created for a drawable with a colormap, or if
a
>colormap has been set explicitly with
gdk_gc_set_colormap.
>Gdk-WARNING **:
../../../gtk+/gdk/gdkpixbuf-drawable.c:1249: Source
>drawable has
> no colormap; either pass in a colormap, or set the
colormap on the
>drawable wit
>h gdk_drawable_set_colormap()
>
>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?
Hi, I don't think you test for it, you just always satisfy
it by always
providing a colormap and gc derived from the Gdk window.
sub draw_poly{
my($widget,$points,$color) = _;
# see Gdk::Gdk::Window, Gtk2::Gdk: rawable,
Gtk2::Gdk::GC
my $colormap = $widget->window->get_colormap;
my $gc = $widget-> || new Gtk2::Gdk::GC
$widget->window;
$gc->set_foreground(get_color($colormap, $color));
$widget->window->draw_polygon($gc,1, $points);
}
It seemed that always doing this for each call was
redundent,
so I used something like this, gleaned from
some examples from the module. What you do is keep a hash
of the colormaps, and gc's.
my %gc;
my %colormap;
my %allocated_colors;
my $linecolor = 'red';
my $axiscolor = 'black';
my %background =( 'm' => 'white',
't' => 'cornsilk',
'l' => 'lightcyan',
'c' => 'blue', );
# sample
my $color = Gtk2::Gdk::Color->new ($r,$g,$b);
&mydraw_points('m',[$x1,$y1,
$x1,$y1-1,
$x1,$y1+1,
], $color );
sub mydraw_points{
my ( $id, $coords, $color ) = _;
$colormap{ $id }->alloc_color( $color, TRUE, TRUE );
$gc{$id}->set_foreground( $color );
$pixmap{ $id }->draw_points( $gc{ $id }, $coords
);
$area{ $id }->queue_draw;
return 0; }
sub get_color{
my ( $name, $id ) = _;
$id ||='m';
my $ret; if ( $ret = $allocated_colors{ $name } ) { return
$ret }
my $color =
Gtk2::Gdk::Color->parse( $name ); $colormap{ $id
}->alloc_color( $color, TRUE, TRUE );
$allocated_colors{ $name } = $color;
return $color;
}
##########################################################
Since this all has a bunch of interactions which are hard to
follow,
I've attached an example. There is a server and client.
Start the
server, then the client. You can look at the server code to
see
how I created and saved the hashes.
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.
html
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
|
|
|
|
| Re: Colormaps in pixbufs which are in
treeview |
  United States |
2007-02-13 16:35:40 |
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
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
|
|
| Re: Colormaps in pixbufs which are in
treeview |
  United Kingdom |
2007-02-13 17:17:41 |
hi;
On Tue, 2007-02-13 at 17:35 -0500, muppet wrote:
> > 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)
> }
even this might be seen as "complicated", when
there's a nice method for
Gtk2::Gdk::Pixbuf like fill():
# $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);
well, packing bits might prove to be complicated - but maybe
it's just
me that had to check.
ciao,
Emmanuele.
--
Emmanuele Bassi, E: ebassi gmail.com
W: http://www.emmanuelebas
si.net
B: http://log.emmanuelebas
si.net
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
|
|
| Re: Colormaps in pixbufs which are in
treeview |
  United States |
2007-02-17 09:30:26 |
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
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
|
|
| Re: Colormaps in pixbufs which are in
treeview |
  United States |
2007-02-17 17:30:32 |
On Feb 17, 2007, at 10:30 AM, zentara wrote:
> Emmanuel's seems more useful since it takes $alpha
values too.
> (but maybe muppet can improve his to take alpha )
Here you go. Not very different. Note that the row stride
is now
$width * 4 instead of $width * 3.
sub create_solid_color_pixbuf_with_alpha {
my ($width, $height, $red, $green, $blue, $alpha) =
_;
my $data = pack "C*", ($red, $green, $blue,
$alpha) x ($width *
$height);
return Gtk2::Gdk::Pixbuf->new_from_data ($data,
'rgb', TRUE, 8,
$width,
$height, $width
* 4);
}
However, now that Emmanuel has gently reminded me of the
existence of
fill(), i'd probably use that, because all of the work
happens under
the hood.
Still, now that you know how to use pack() with the repeat
operator
to fill with a color, it's just a short conceptual jump to
manipulating your image data in perl. For example:
sub create_solid_color_pixbuf_with_alpha_gradient {
my ($width, $height, $red, $green, $blue) = _;
my $data = pack "C*",
map { ($red, $green, $blue, ($_ * 255.0 / ($height
- 1))) x
$width }
0 .. ($height-1);
return Gtk2::Gdk::Pixbuf->new_from_data ($data,
'rgb', TRUE, 8,
$width, $height, $width * 4);
}
Now I feel like i deserve an award for egregious disregard
for memory
efficiency.
--
If the monkey could type one keystroke every nanosecond, the
expected
waiting time until the monkey types out Hamlet is so long
that the
estimated age of the universe is insignificant by comparison
... this
is not a practical method for writing plays.
-- Gian-Carlo Rota
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
|
|
[1-6]
|
|