]> Pileus Git - ~andy/gtk/blob - gdk/gdkcolor.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #include "config.h"
26
27 #include "gdkcolor.h"
28
29 #include "gdkscreen.h"
30 #include "gdkinternals.h"
31
32 #include <time.h>
33
34 /**
35  * SECTION:colors
36  * @Short_description: Manipulation of colors
37  * @Title: Colors
38  *
39  * A #GdkColor represents a color.
40  *
41  * When working with cairo, it is often more convenient
42  * to use a #GdkRGBA instead.
43  */
44
45
46 /**
47  * gdk_color_copy:
48  * @color: a #GdkColor
49  *
50  * Makes a copy of a color structure.
51  *
52  * The result must be freed using gdk_color_free().
53  *
54  * Return value: a copy of @color
55  */
56 GdkColor*
57 gdk_color_copy (const GdkColor *color)
58 {
59   GdkColor *new_color;
60
61   g_return_val_if_fail (color != NULL, NULL);
62
63   new_color = g_slice_new (GdkColor);
64   *new_color = *color;
65   return new_color;
66 }
67
68 /**
69  * gdk_color_free:
70  * @color: a #GdkColor
71  *
72  * Frees a color structure created with gdk_color_copy().
73  */
74 void
75 gdk_color_free (GdkColor *color)
76 {
77   g_return_if_fail (color != NULL);
78
79   g_slice_free (GdkColor, color);
80 }
81
82 /**
83  * gdk_color_hash:
84  * @color: a #GdkColor
85  *
86  * A hash function suitable for using for a hash
87  * table that stores #GdkColors.
88  *
89  * Return value: The hash function applied to @color
90  */
91 guint
92 gdk_color_hash (const GdkColor *color)
93 {
94   return ((color->red) +
95           (color->green << 11) +
96           (color->blue << 22) +
97           (color->blue >> 6));
98 }
99
100 /**
101  * gdk_color_equal:
102  * @colora: a #GdkColor
103  * @colorb: another #GdkColor
104  *
105  * Compares two colors.
106  *
107  * Return value: %TRUE if the two colors compare equal
108  */
109 gboolean
110 gdk_color_equal (const GdkColor *colora,
111                  const GdkColor *colorb)
112 {
113   g_return_val_if_fail (colora != NULL, FALSE);
114   g_return_val_if_fail (colorb != NULL, FALSE);
115
116   return ((colora->red == colorb->red) &&
117           (colora->green == colorb->green) &&
118           (colora->blue == colorb->blue));
119 }
120
121 G_DEFINE_BOXED_TYPE (GdkColor, gdk_color,
122                      gdk_color_copy,
123                      gdk_color_free)
124
125 /**
126  * gdk_color_parse:
127  * @spec: the string specifying the color
128  * @color: (out): the #GdkColor to fill in
129  *
130  * Parses a textual specification of a color and fill in the
131  * <structfield>red</structfield>, <structfield>green</structfield>,
132  * and <structfield>blue</structfield> fields of a #GdkColor
133  * structure.
134  *
135  * The string can either one of a large set of standard names
136  * (taken from the X11 <filename>rgb.txt</filename> file), or
137  * it can be a hex value in the form '&num;rgb' '&num;rrggbb'
138  * '&num;rrrgggbbb' or '&num;rrrrggggbbbb' where 'r', 'g' and
139  * 'b' are hex digits of the red, green, and blue components
140  * of the color, respectively. (White in the four forms is
141  * '&num;fff', '&num;ffffff', '&num;fffffffff' and
142  * '&num;ffffffffffff').
143  *
144  * Return value: %TRUE if the parsing succeeded
145  */
146 gboolean
147 gdk_color_parse (const gchar *spec,
148                  GdkColor    *color)
149 {
150   PangoColor pango_color;
151
152   if (pango_color_parse (&pango_color, spec))
153     {
154       color->red = pango_color.red;
155       color->green = pango_color.green;
156       color->blue = pango_color.blue;
157
158       return TRUE;
159     }
160   else
161     return FALSE;
162 }
163
164 /**
165  * gdk_color_to_string:
166  * @color: a #GdkColor
167  *
168  * Returns a textual specification of @color in the hexadecimal form
169  * <literal>&num;rrrrggggbbbb</literal>, where <literal>r</literal>,
170  * <literal>g</literal> and <literal>b</literal> are hex digits
171  * representing the red, green and blue components respectively.
172  *
173  * The returned string can be parsed by gdk_color_parse().
174  *
175  * Return value: a newly-allocated text string
176  *
177  * Since: 2.12
178  */
179 gchar *
180 gdk_color_to_string (const GdkColor *color)
181 {
182   PangoColor pango_color;
183
184   g_return_val_if_fail (color != NULL, NULL);
185
186   pango_color.red = color->red;
187   pango_color.green = color->green;
188   pango_color.blue = color->blue;
189
190   return pango_color_to_string (&pango_color);
191 }