]> Pileus Git - ~andy/gtk/blob - gdk/gdkcolor.c
ace91012e001b70ae058cfcd395bc97443289b61
[~andy/gtk] / gdk / gdkcolor.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include "gdkcolor.h"
30
31 #include "gdkscreen.h"
32 #include "gdkinternals.h"
33
34 #include <time.h>
35
36 /**
37  * SECTION:colors
38  * @Short_description: Manipulation of colors
39  * @Title: Colors
40  *
41  * A #GdkColor represents a color.
42  *
43  * When working with cairo, it is often more convenient
44  * to use a #GdkRGBA instead.
45  */
46
47
48 /**
49  * gdk_color_copy:
50  * @color: a #GdkColor
51  *
52  * Makes a copy of a color structure.
53  *
54  * The result must be freed using gdk_color_free().
55  *
56  * Return value: a copy of @color
57  */
58 GdkColor*
59 gdk_color_copy (const GdkColor *color)
60 {
61   GdkColor *new_color;
62
63   g_return_val_if_fail (color != NULL, NULL);
64
65   new_color = g_slice_new (GdkColor);
66   *new_color = *color;
67   return new_color;
68 }
69
70 /**
71  * gdk_color_free:
72  * @color: a #GdkColor
73  *
74  * Frees a color structure created with gdk_color_copy().
75  */
76 void
77 gdk_color_free (GdkColor *color)
78 {
79   g_return_if_fail (color != NULL);
80
81   g_slice_free (GdkColor, color);
82 }
83
84 /**
85  * gdk_color_hash:
86  * @color: a #GdkColor
87  *
88  * A hash function suitable for using for a hash
89  * table that stores #GdkColors.
90  *
91  * Return value: The hash function applied to @color
92  */
93 guint
94 gdk_color_hash (const GdkColor *color)
95 {
96   return ((color->red) +
97           (color->green << 11) +
98           (color->blue << 22) +
99           (color->blue >> 6));
100 }
101
102 /**
103  * gdk_color_equal:
104  * @colora: a #GdkColor
105  * @colorb: another #GdkColor
106  *
107  * Compares two colors.
108  *
109  * Return value: %TRUE if the two colors compare equal
110  */
111 gboolean
112 gdk_color_equal (const GdkColor *colora,
113                  const GdkColor *colorb)
114 {
115   g_return_val_if_fail (colora != NULL, FALSE);
116   g_return_val_if_fail (colorb != NULL, FALSE);
117
118   return ((colora->red == colorb->red) &&
119           (colora->green == colorb->green) &&
120           (colora->blue == colorb->blue));
121 }
122
123 G_DEFINE_BOXED_TYPE (GdkColor, gdk_color,
124                      gdk_color_copy,
125                      gdk_color_free)
126
127 /**
128  * gdk_color_parse:
129  * @spec: the string specifying the color
130  * @color: (out): the #GdkColor to fill in
131  *
132  * Parses a textual specification of a color and fill in the
133  * <structfield>red</structfield>, <structfield>green</structfield>,
134  * and <structfield>blue</structfield> fields of a #GdkColor
135  * structure.
136  *
137  * The string can either one of a large set of standard names
138  * (taken from the X11 <filename>rgb.txt</filename> file), or
139  * it can be a hex value in the form '&num;rgb' '&num;rrggbb'
140  * '&num;rrrgggbbb' or '&num;rrrrggggbbbb' where 'r', 'g' and
141  * 'b' are hex digits of the red, green, and blue components
142  * of the color, respectively. (White in the four forms is
143  * '&num;fff', '&num;ffffff', '&num;fffffffff' and
144  * '&num;ffffffffffff').
145  *
146  * Return value: %TRUE if the parsing succeeded
147  */
148 gboolean
149 gdk_color_parse (const gchar *spec,
150                  GdkColor    *color)
151 {
152   PangoColor pango_color;
153
154   if (pango_color_parse (&pango_color, spec))
155     {
156       color->red = pango_color.red;
157       color->green = pango_color.green;
158       color->blue = pango_color.blue;
159
160       return TRUE;
161     }
162   else
163     return FALSE;
164 }
165
166 /**
167  * gdk_color_to_string:
168  * @color: a #GdkColor
169  *
170  * Returns a textual specification of @color in the hexadecimal form
171  * <literal>&num;rrrrggggbbbb</literal>, where <literal>r</literal>,
172  * <literal>g</literal> and <literal>b</literal> are hex digits
173  * representing the red, green and blue components respectively.
174  *
175  * The returned string can be parsed by gdk_color_parse().
176  *
177  * Return value: a newly-allocated text string
178  *
179  * Since: 2.12
180  */
181 gchar *
182 gdk_color_to_string (const GdkColor *color)
183 {
184   PangoColor pango_color;
185
186   g_return_val_if_fail (color != NULL, NULL);
187
188   pango_color.red = color->red;
189   pango_color.green = color->green;
190   pango_color.blue = color->blue;
191
192   return pango_color_to_string (&pango_color);
193 }