<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >How do I use color allocation?</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="GTK+ FAQ" HREF="book1.html"><LINK REL="UP" TITLE="About GDK" HREF="c857.html"><LINK REL="PREVIOUS" TITLE="About GDK" HREF="c857.html"><LINK REL="NEXT" TITLE="About GLib" HREF="c866.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >GTK+ FAQ</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="c857.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >About GDK</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="c866.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN862" >How do I use color allocation?</A ></H1 ><P >One of the nice things about GDK is that it's based on top of Xlib; this is also a problem, especially in the area of color management. If you want to use color in your program (drawing a rectangle or such, your code should look something like this:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >{ GdkColor *color; int width, height; GtkWidget *widget; GdkGC *gc; ... /* first, create a GC to draw on */ gc = gdk_gc_new(widget->window); /* find proper dimensions for rectangle */ gdk_window_get_size(widget->window, &width, &height); /* the color we want to use */ color = (GdkColor *)malloc(sizeof(GdkColor)); /* red, green, and blue are passed values, indicating the RGB triple * of the color we want to draw. Note that the values of the RGB components * within the GdkColor are taken from 0 to 65535, not 0 to 255. */ color->red = red * (65535/255); color->green = green * (65535/255); color->blue = blue * (65535/255); /* the pixel value indicates the index in the colormap of the color. * it is simply a combination of the RGB values we set earlier */ color->pixel = (gulong)(red*65536 + green*256 + blue); /* However, the pixel valule is only truly valid on 24-bit (TrueColor) * displays. Therefore, this call is required so that GDK and X can * give us the closest color available in the colormap */ gdk_color_alloc(gtk_widget_get_colormap(widget), color); /* set the foreground to our color */ gdk_gc_set_foreground(gc, color); /* draw the rectangle */ gdk_draw_rectangle(widget->window, gc, 1, 0, 0, width, height); ... }</PRE ></TD ></TR ></TABLE ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="c857.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="book1.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="c866.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >About GDK</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c857.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >About GLib</TD ></TR ></TABLE ></DIV ></BODY ></HTML >