Color Confusion



I've been trying to develop a small Gtk application which needs to use
the gdk_draw routines to generate an image in a pixmap, and then copy
this pixmap into a previously created drawing_area.  I'm having a
great deal of trouble figuring out what to pass as the gc parameter
that all the gdk_draw routines use.

The examples that I've found all seem to either just use one of the
widget->window->gcs, or else involve so much code that I can't follow
them.  After spending most of a weekend plowing through colors, color
maps, visuals, color contexts, graphics contexts, styles, and much
else, it seems like time to ask for help.

The little test program below is intended to paint a blue square on a
white background.  When run, however, the center square shows up in
black rather than the intended blue.  The print statement seems to
indicate that blue has RGB values of 20480,20480,63488, even though
blue is listed as 0,0,255 in my rgb.txt file.  This however seems like
a minor problem.

I'd be most appreciative if someone could take a look at the exposure
handler in the attached test program and give me a hint as to what
I've done wrong here, or what general approach should be used in
getting a properly set up GC from a symbolic color name.  TIA.

-- John Kodis.


#include <stdio.h>
#include <stdlib.h>
#include "gtk/gtk.h"

static gint expose_handler(GtkWidget *widget, GdkEventExpose *event)
{
  GdkPixmap *pixmap;
  GdkColor blue = {0};
  GdkGC *blu_gc = NULL;
  int w = widget->allocation.width, h = widget->allocation.height;

  gdk_color_parse("blue", &blue);
  printf("blu=%d,%d,%d\n", blue.red, blue.green, blue.blue);

  blu_gc = gdk_gc_new(widget->window);
  gdk_gc_set_foreground(blu_gc, &blue);
  gdk_gc_set_background(blu_gc, &blue);

  pixmap = gdk_pixmap_new(widget->window, w, h, -1);
  gdk_draw_rectangle(pixmap, widget->style->white_gc, TRUE, 0,0, w,h);
  gdk_draw_rectangle(pixmap, blu_gc, TRUE, w/4,h/4, w/2,h/2);
  gdk_draw_pixmap(
    widget->window, widget->style->white_gc,
    pixmap, 0,0, 0,0, w,h);

  return 0;
}

int main(int argc, char **argv)
{
  GtkWidget *window, *drawing;

  gtk_init (&argc, &argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  drawing = gtk_drawing_area_new();
  gtk_container_add(GTK_CONTAINER(window), drawing);
  gtk_signal_connect(
    GTK_OBJECT(drawing), "expose_event", 
    (GtkSignalFunc)expose_handler, NULL);
  gtk_widget_set_events(drawing, GDK_EXPOSURE_MASK);
  gtk_widget_show(drawing);

  gtk_widget_show(window);
  gtk_main();

  return EXIT_SUCCESS;
}



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