[pygtk] Questions about GdkColor, and how to pass an array of GdkColor as an argument

Gianni Moschini gianni.moschini at gmail.com
Tue May 26 05:54:58 WST 2009


Hello everyone

I'm trying to use a function from the metacity package, in PyGTK, but cannot
find what's wrong. I suppose it's related to the list of colors, and how to
send these as arguments (more below).

The function is metacity.gradient_create_multi

When typing pydoc metacity.gradient_create_multi, I don't get much help:

> Help on built-in function gradient_create_multi in metacity:
> metacity.gradient_create_multi = gradient_create_multi(...)

It's defined like this in gnome-python-desktop/metacity/metacity.defs :

> (define-function gradient_create_multi
>   (c-name "meta_gradient_create_multi")
>   (return-type "GdkPixbuf*")
>   (parameters
>     '("int" "width")
>     '("int" "height")
>     '("const-GdkColor*" "colors")
>     '("int" "n_colors")
>     '("MetaGradientType" "style")
>   )
> )


So, I took a look at the C function :

> GdkPixbuf*
> meta_gradient_create_multi (int              width,
> int              height,
> const GdkColor  *colors,
> int              n_colors,
> MetaGradientType style)

Which then calls meta_gradient_create_multi_vertical

> static GdkPixbuf*
> meta_gradient_create_multi_vertical (int width, int height,
>  GdkColor *colors,
>  count)

Here is the code in metacity/src/testgradient.c for reference (simplified)

> GdkColor colors[5];
>
> pixbuf = meta_gradient_create_multi (width, height, colors, 5, type);

Ok. That's nice. So this is just a pointer to an array of GdkColor. Right ?

How do I call this in python ? I suppose that using a list is _not_ the
solution, as the memory block should be continuous, right ? I tried
searching how to do this with pynum, but as far as I understand this is
_not_ the solution either (maybe I'm wrong here).

Here is how I do this right now.. (please, be rude, I know I'm wrong)

> colors = [
>             colormap.alloc_color( "yellow" ),
>             colormap.alloc_color( "green" ),
>             colormap.alloc_color( "white" )
>         ]
> metacity.gradient_create_multi(100, 100, colors[0], len(colors),
metacity.GRADIENT_VERTICAL)

This seems to pass, but (and that's the real problem) .. it seems like
colors are changing everytime I call this function. Sometimes it's just
fine, and sometimes colors are not those in passed in the list.

Note: there should be at least 3 colors for the gradient_create_multi_*
functions to be called. Otherwise there is a fallback on
gradient_create_simple, so don't bother using less than 3 colors.

Now I found a code where the colors seems to be stable between calls, but I
suppose if I add some colors to the list with colors.append( somecolor ), it
will probably shows weird colors again.

Maybe I'm doing something wrong with the color allocation here ?

The code is also attached in this e-mail (1,6Ko)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20090525/c4bcde85/attachment.htm 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: metacity_gradient-colors.tar.gz
Type: application/x-gzip
Size: 1602 bytes
Desc: not available
Url : http://www.daa.com.au/pipermail/pygtk/attachments/20090525/c4bcde85/attachment.bin 


More information about the pygtk mailing list