Re: Alloc'ing and free'ing colors.




johannes@nada.kth.se (Johannes Keukelaar) writes:

> Hello All,
> 
> I'm working on an application that dynamically creates colors by mixing some 
> colors that the user can change at runtime. My problem is, each time the user 
> changes the colors, I have to gdk_color_alloc new mixed colors. This works, 
> until my colormap fills up; then, I start getting weird almost-matches. 
> (Obviously, I don't have a 24bit screen, I have to 'make do' with a 256 color 
> one.)
> 
> I understand the problem (the colormap fills up), and see a possible solution 
> in gdk_colors_free. However, from looking at the code for a while it seems that 
> gdk_color_alloc puts the newly allocated color in the colormap, whereas 
> gdk_colors_free doesn't remove them (in fact, that may not be correct, as they 
> may have been color_alloc'ed from somewhere else in the application, too).
> 
> In other words, if I understand things correctly, gdk_color_alloc and 
> gdk_colors_free can't really be used together in the way I want them to be. Is 
> this true? (Please, tell me I'm wrong. :) Can it be fixed? Is there another 
> solution?

colormap->colors[] keeps track of the color that has been assigned to
each pixel value, but is not used to keep track of whether a color has
been allocated or not.

I believe gdk_colors_free works (at least approximately) correctly. It
calls XFreeColors; this is correct because XAllocColor keeps a
reference count so that if gdk_color_alloc was called multiple times
for the same color, it can safely be freed multiple times.

[ I say "approximately" because I'm not quite sure about the case
  of a private colormap. GDK initially allocates all the cells in
  the colormap read/write, but still will free them with
  gdk_colors_free, so that might cause problems ]

If you are only allocating a few colors, but changing them frequently,
a more efficient solution may be to allocate them read/write
using gdk_colors_alloc, and then change them using gdk_color_change().

Something like:

 gulong pixels[10];
 GdkColor mycolor;

 gdk_colors_alloc (gdk_colormap_get_system(), FALSE, NULL, 0, 10,
                   &pixels, 10);


 [...]

 mycolor.pixel = pixels[5];
 mycolor.red = 0xFFFF;
 mycolor.green = 0x0;
 mycolor.red = 0x0;

 gdk_color_change (gdk_colormap_get_system, &mycolor);

However, this is specific to a PseudoColor display, and won't work
if you get a 24bit display.

Regards,
                                        Owen



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