Re: [gtk-list] Colour allocation with gdk?



On Tue, 12 Jan 1999, Lyndon Drake wrote:

> I was wondering if anyone can point me to some sample code for handling
> colour allocation with gdk (other than the short tutorial example).  I

Do you mean just, for instance, displaying right color in gtk_drawing_area?
Then you can look through code that I've posed yesterday answering
letter about "color in Drawin Area" or something like this.

Just in case you won't find it (if you'll want to) I've attached another
source: gtk_drawing_area with changing color.
Good Luck,
_____
Alexander Kotelnikov
Saint-Petersburg, Russia
mailto:sacha@ak2614.spb.edu

#include <gtk/gtk.h>
#include <time.h>
#include <math.h>
#define EXPOSE
#define CONFIGURE
#define gradient 100
#define X 60
#define Y 60
#define COLOR

static GdkPixmap *pixmap = NULL;
static GdkColor *drcol= NULL;
static GdkGC *drgc;
static int j=0; 

#ifdef CONFIGURE
static gint configure_event (GtkWidget *widget, GdkEventConfigure *event)

{

    if (pixmap) gdk_pixmap_unref (pixmap);

    
    pixmap = gdk_pixmap_new (widget->window,

                widget->allocation.width,

                widget->allocation.height,

                -1);



    return TRUE;

}


#endif
#ifdef EXPOSE
static gint expose_event (GtkWidget *widget, GdkEventExpose *event)

{



    gdk_draw_pixmap (widget->window,

          widget->style->fg_gc[GTK_WIDGET_STATE (widget)],

          pixmap,

          event->area.x, event->area.y,

          event->area.x, event->area.y,

          event->area.width, event->area.height);



    return FALSE;

}

#endif
/***/
void quit(void)
{gtk_main_quit();}
/***/

gint anim(gpointer data)
{
GtkWidget* darea = (GtkWidget*) data;
GdkRectangle * update = (GdkRectangle*)g_malloc(sizeof(GdkRectangle));

if (j>gradient) j=0;
#ifndef COLOR
       drcol->red   = 0xffff*fabs(gradient-j)/gradient;
       drcol->green = 0xffff*fabs(gradient-j)/gradient;
       drcol->blue  = 0xffff*fabs(gradient-j)/gradient;
#else
if ( (j<=gradient/3.0)||(j>=2.0*gradient/3.0) )

       drcol->red   = 
           0xffff*fabs( (j-2*gradient/3) / (2.0*gradient/3) );
else   drcol->red   = 0;

if (j<= 2.0*gradient/3.0)
       drcol->green = 
           0xffff*(gradient/3.0-fabs(j-gradient/3.0))/(gradient/3.0);
else   drcol->green   = 0;

if (j>=gradient/3.0)
       drcol->blue  = 
           0xffff*(gradient/3.0-fabs(j-2*gradient/3.0))/(gradient/3.0);
else   drcol->blue   = 0;
#endif
gdk_color_alloc (gdk_colormap_get_system(), drcol);
gdk_gc_set_foreground(drgc,drcol);

gdk_draw_rectangle (pixmap,
                    drgc,
		    TRUE,
		    0,0,
		    darea->allocation.width,
		    darea->allocation.height);

update->x = 0; update->y = 0;
update->width  = darea->allocation.width; 
update->height = darea->allocation.height;

gtk_widget_draw(darea,update);

j++;
return TRUE;
}

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *darea;

    gtk_init (&argc, &argv);


/*****************************/
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
darea  = gtk_drawing_area_new();
gtk_drawing_area_size(GTK_DRAWING_AREA(darea),X, Y);
gtk_container_add(GTK_CONTAINER(window),darea);
gtk_widget_show (darea);
gtk_signal_connect (GTK_OBJECT(window), "destroy",
		    GTK_SIGNAL_FUNC (quit), NULL);



#ifdef EXPOSE
gtk_signal_connect (GTK_OBJECT(darea), "expose_event",
		    (GtkSignalFunc) expose_event, NULL);
#endif

#ifdef CONFIGURE
gtk_signal_connect (GTK_OBJECT(darea), "configure_event",
		    (GtkSignalFunc) configure_event, NULL);
#endif		    

gtk_widget_show (window);

drcol = (GdkColor*) g_malloc (sizeof(GdkColor));
drgc = gdk_gc_new(darea->window);
gdk_gc_copy(drgc,darea->style->white_gc);
//gdk_color_alloc (gdk_colormap_get_system(), drcol);

/*****************************/
gtk_timeout_add (5, anim, (gpointer) darea);

//while (gtk_events_pending()) gtk_main_iteration();

//anim ((gpointer)darea);		    

/*****************************/
    gtk_main ();

return 0;
}


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