📄 sec-colorselection.html
字号:
<HTML><HEAD><TITLE>Color Selection</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="UP"TITLE="Miscellaneous Widgets"HREF="ch-miscwidgets.html"><LINKREL="PREVIOUS"TITLE="Calendar"HREF="sec-calendar.html"><LINKREL="NEXT"TITLE="File Selections"HREF="sec-fileselections.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%"> <tr> <td bgcolor="#FFFFFF" valign=top nowrap> <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A> <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR> </td> <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sec-calendar.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 9. Miscellaneous Widgets</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-fileselections.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-COLORSELECTION">9.13. Color Selection</A></H1><P>The color selection widget is, not surprisingly, a widget forinteractive selection of colors. This composite widget lets the userselect a color by manipulating RGB (Red, Green, Blue) and HSV (Hue,Saturation, Value) triples. This is done either by adjusting singlevalues with sliders or entries, or by picking the desired color from ahue-saturation wheel/value bar. Optionally, the opacity of the colorcan also be set.</P><P>The color selection widget currently emits only one signal,"color_changed", which is emitted whenever the current color in thewidget changes, either when the user changes it or if it's setexplicitly through gtk_color_selection_set_color().</P><P>Lets have a look at what the color selection widget has to offerus. The widget comes in two flavours: gtk_color_selection andgtk_color_selection_dialog.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget *gtk_color_selection_new( void );</PRE></TD></TR></TABLE><P>You'll probably not be using this constructor directly. It creates anorphan ColorSelection widget which you'll have to parentyourself. The ColorSelection widget inherits from the VBoxwidget.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget *gtk_color_selection_dialog_new( const gchar *title );</PRE></TD></TR></TABLE><P>This is the most common color selection constructor. It creates aColorSelectionDialog. It consists of a Frame containing aColorSelection widget, an HSeparator and an HBox with three buttons,"Ok", "Cancel" and "Help". You can reach these buttons by accessingthe "ok_button", "cancel_button" and "help_button" widgets in theColorSelectionDialog structure,(i.e., <TTCLASS="LITERAL">GTK_COLOR_SELECTION_DIALOG(colorseldialog)->ok_button</TT>)).</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_color_selection_set_update_policy( GtkColorSelection *colorsel, GtkUpdateType policy );</PRE></TD></TR></TABLE><P>This function sets the update policy. The default policy is<TTCLASS="LITERAL">GTK_UPDATE_CONTINUOUS</TT> which means that the current color isupdated continuously when the user drags the sliders or presses themouse and drags in the hue-saturation wheel or value bar. If youexperience performance problems, you may want to set the policy to<TTCLASS="LITERAL">GTK_UPDATE_DISCONTINUOUS</TT> or <TTCLASS="LITERAL">GTK_UPDATE_DELAYED</TT>.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_color_selection_set_opacity( GtkColorSelection *colorsel, gint use_opacity );</PRE></TD></TR></TABLE><P>The color selection widget supports adjusting the opacity of a color(also known as the alpha channel). This is disabled bydefault. Calling this function with use_opacity set to TRUE enablesopacity. Likewise, use_opacity set to FALSE will disable opacity.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_color_selection_set_color( GtkColorSelection *colorsel, gdouble *color );</PRE></TD></TR></TABLE><P>You can set the current color explicitly by calling this function witha pointer to an array of colors (gdouble). The length of the arraydepends on whether opacity is enabled or not. Position 0 contains thered component, 1 is green, 2 is blue and opacity is at position 3(only if opacity is enabled, seegtk_color_selection_set_opacity()). All values are between 0.0 and1.0.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_color_selection_get_color( GtkColorSelection *colorsel, gdouble *color );</PRE></TD></TR></TABLE><P>When you need to query the current color, typically when you'vereceived a "color_changed" signal, you use this function. Color is apointer to the array of colors to fill in. See thegtk_color_selection_set_color() function for the description of thisarray.</P><P></P><P>Here's a simple example demonstrating the use of theColorSelectionDialog. The program displays a window containing adrawing area. Clicking on it opens a color selection dialog, andchanging the color in the color selection dialog changes thebackground color.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start colorsel colorsel.c */#include <glib.h>#include <gdk/gdk.h>#include <gtk/gtk.h>GtkWidget *colorseldlg = NULL;GtkWidget *drawingarea = NULL;/* Color changed handler */void color_changed_cb( GtkWidget *widget, GtkColorSelection *colorsel ){ gdouble color[3]; GdkColor gdk_color; GdkColormap *colormap; /* Get drawingarea colormap */ colormap = gdk_window_get_colormap (drawingarea->window); /* Get current color */ gtk_color_selection_get_color (colorsel,color); /* Fit to a unsigned 16 bit integer (0..65535) and * insert into the GdkColor structure */ gdk_color.red = (guint16)(color[0]*65535.0); gdk_color.green = (guint16)(color[1]*65535.0); gdk_color.blue = (guint16)(color[2]*65535.0); /* Allocate color */ gdk_color_alloc (colormap, &gdk_color); /* Set window background color */ gdk_window_set_background (drawingarea->window, &gdk_color); /* Clear window */ gdk_window_clear (drawingarea->window);}/* Drawingarea event handler */gint area_event( GtkWidget *widget, GdkEvent *event, gpointer client_data ){ gint handled = FALSE; GtkWidget *colorsel; /* Check if we've received a button pressed event */ if (event->type == GDK_BUTTON_PRESS && colorseldlg == NULL) { /* Yes, we have an event and there's no colorseldlg yet! */ handled = TRUE; /* Create color selection dialog */ colorseldlg = gtk_color_selection_dialog_new("Select background color"); /* Get the ColorSelection widget */ colorsel = GTK_COLOR_SELECTION_DIALOG(colorseldlg)->colorsel; /* Connect to the "color_changed" signal, set the client-data * to the colorsel widget */ gtk_signal_connect(GTK_OBJECT(colorsel), "color_changed", (GtkSignalFunc)color_changed_cb, (gpointer)colorsel); /* Show the dialog */ gtk_widget_show(colorseldlg); } return handled;}/* Close down and exit handler */gint destroy_window( GtkWidget *widget, GdkEvent *event, gpointer client_data ){ gtk_main_quit (); return(TRUE);}/* Main */gint main( gint argc, gchar *argv[] ){ GtkWidget *window; /* Initialize the toolkit, remove gtk-related commandline stuff */ gtk_init (&argc,&argv); /* Create toplevel window, set title and policies */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW(window), "Color selection test"); gtk_window_set_policy (GTK_WINDOW(window), TRUE, TRUE, TRUE); /* Attach to the "delete" and "destroy" events so we can exit */ gtk_signal_connect (GTK_OBJECT(window), "delete_event", (GtkSignalFunc)destroy_window, (gpointer)window); /* Create drawingarea, set size and catch button events */ drawingarea = gtk_drawing_area_new (); gtk_drawing_area_size (GTK_DRAWING_AREA(drawingarea), 200, 200); gtk_widget_set_events (drawingarea, GDK_BUTTON_PRESS_MASK); gtk_signal_connect (GTK_OBJECT(drawingarea), "event", (GtkSignalFunc)area_event, (gpointer)drawingarea); /* Add drawingarea to window, then show them both */ gtk_container_add (GTK_CONTAINER(window), drawingarea); gtk_widget_show (drawingarea); gtk_widget_show (window); /* Enter the gtk main loop (this never returns) */ gtk_main (); /* Satisfy grumpy compilers */ return(0);}/* example-end */</PRE></TD></TR></TABLE></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-calendar.html"><<< Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-fileselections.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Calendar</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-miscwidgets.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">File Selections</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -