Re: [gtk-list] Problem with ColorContexts



>  I'm just trying to set the colors of some clist rows, but the
>  setting's being ignored.  I'm using Gtk 0.99.5, and here's how I
>  allocate the color:
[snip]

OK, OK, I know I should write some Color Context documentation :-)

In general, you should use gdk_color_context_get_pixels() for color
allocation, even if you will only be allocating a single color.  I
know this is confusing, and get_pixel should be made an internal
function of GdkColorContext.

The reason you should use get_pixels() is that it will try to remap
the color to the closest thing available in the colormap if the
initial allocation does not succeed.

Here is how to get a single color for a gc:

	GdkColorContext *cc;
	GdkColor color;
	gint n;

	cc = gdk_color_context_new (gtk_widget_get_visual (my_widget),
				    gtk_widget_get_colormap (my_widget));

	color.pixel = 0; /* this is important! */
	color.red   = 0x2020; /* or whatever values you want, in [0, 65535] */
	color.green = 0x4040;
	color.blue  = 0x6060;

	n = 0; /* this is important! */

	gdk_color_context_get_pixels (cc, &color.red, &color.green, &color.blue,
				      1, &color.pixel, &n);

	gdk_gc_set_foreground (my_gc, &color);

If this looks like too much trouble, I agree.  There should be a
simple wrapper function for getting a single pixel.

For "normal" applications, it is important that you initialize the
pixel values and the nallocated parameters to zero, otherwise you
won't get correct results.

This works like that because the color context get_pixels() function
was designed to work for the XmHTML widget's progressive image
loader.  The idea is that you can request colors incrementally with
the help of the color context.  Please look at the comments in gdkcc.c
and XmHTML's sources for the gritty details.

As a side note, maybe this blurb should be added to the FAQ under a
"How do I use the color context / how do I allocate colors?" question.

The Gnome sources have some examples of using GdkColorContext.  They
look mostly like the code above, but they are used in real
applications, so you may want to look at them.

Good luck,

  Quartic



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