GNOME Bugzilla – Bug 604790
Unable to set a GC's foreground colour
Last modified: 2009-12-27 09:19:16 UTC
I cannot set the foreground colour of a graphics context, nor modify any of its RGB values. This is corroborated in this Stack Overflow post: http://stackoverflow.com/questions/938921/pygtk-graphics-contexts-and-allocating-colors My code snippet to reproduce: grey = gtk.gdk.Color('gray') drawable.get_colormap().alloc_color(grey) gc = drawable.new_gc() print "Before", repr(gc.foreground) gc.foreground = grey print "Assignment", repr(grey) print "After", repr(gc.foreground) print "Blue value before", gc.foreground.blue gc.foreground.blue = 65535 print "Blue value after", gc.foreground.blue ## Produces: Before gtk.gdk.Color('#dff400a4dff4') Assignment gtk.gdk.Color('#bebebe') After gtk.gdk.Color('#04c000250001') Blue value before 1 Blue value after 1 Note that the colour changes, but not in such a way as to be useful. The properties are listed as read-write, and so I would expect them to by updated when I try to set them. The corresponding method set_foreground() has the same effect. Attempting to allocate the colour (as shown above) has no influence on the result.
Can you post a complete (minimal) working example showing the problem? thanks
Created attachment 150406 [details] Testcase for setting the foreground colour of a GC
This is not a bug. The basic problem with the test case is that it doesn't capture the allocated color from the call to self.window.get_colormap().alloc_color(grey) so when the unallocated color grey is used it of course doesn't fill the drawing area with the right color. The important attribute of an allocated color is the pixel value. If you assign a new value to any of the red, green or blue attributes of an allocated gtk.gdk.Color the color is not changed because the pixel value is unchanged. When a gtk.gdk.GC retrieves one of its color attributes (e.g. foreground) only the pixel attribute is set; the red, green and blue attributes are not set. To fix the test case assign the return value from self.window.get_colormap().alloc_color(grey) to 'grey'.