]> Pileus Git - ~andy/gtk/blob - gdk/gdkrgba.c
gdk: Add section docs for GdkRGBA
[~andy/gtk] / gdk / gdkrgba.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 #include "gdkrgba.h"
29 #include <string.h>
30
31 /**
32  * SECTION:rgba_colors
33  * @Short_description: RGBA colors
34  * @Title: RGBA Colors
35  *
36  * The #GdkRGBA struct is a convenient way to pass rgba colors around.
37  * It's based on cairo's way to deal with colors and mirros its behavior.
38  * All values are in the range from 0.0 to 1.0 inclusive. So the color
39  * (0.0, 0.0, 0.0, 0.0) represents transparent black and
40  * (1.0, 1.0, 1.0, 1.0) is opaque white. Other values will be clamped
41  * to this range when drawing.
42  */
43
44 G_DEFINE_BOXED_TYPE (GdkRGBA, gdk_rgba,
45                      gdk_rgba_copy, gdk_rgba_free)
46
47 /**
48  * GdkRGBA:
49  * @red: The intensity of the red channel from 0.0 to 1.0 inclusive.
50  * @green: The intensity of the green channel from 0.0 to 1.0 inclusive.
51  * @blue: The intensity of the blue channel from 0.0 to 1.0 inclusive.
52  * @alpha: The opacity of the color from 0.0 for completely translucent to
53  *   1.0 for opaque.
54  *
55  * The GdkRGBA structure is used to pass around color data. When using it
56  * as struct members or on the stack, you want to use the struct directly
57  * and not allocate it.
58  */
59
60 /**
61  * gdk_rgba_copy:
62  * @rgba: a #GdkRGBA
63  *
64  * Makes a copy of a #GdkRGBA structure, the result must be freed
65  * through gdk_rgba_free().
66  *
67  * Returns: A newly allocated #GdkRGBA
68  *
69  * Since: 3.0
70  **/
71 GdkRGBA *
72 gdk_rgba_copy (const GdkRGBA *rgba)
73 {
74   GdkRGBA *copy;
75
76   copy = g_slice_new (GdkRGBA);
77   copy->red = rgba->red;
78   copy->green = rgba->green;
79   copy->blue = rgba->blue;
80   copy->alpha = rgba->alpha;
81
82   return copy;
83 }
84
85 /**
86  * gdk_rgba_free:
87  * @rgba: a #GdkRGBA
88  *
89  * Frees a #GdkRGBA struct created with gdk_rgba_copy()
90  *
91  * Since: 3.0
92  **/
93 void
94 gdk_rgba_free (GdkRGBA *rgba)
95 {
96   g_slice_free (GdkRGBA, rgba);
97 }
98
99 #define SKIP_WHITESPACES(s) while (*(s) == ' ') (s)++;
100
101 /* Parses a single color component from a rgb() or rgba() specification
102  * according to CSS3 rules. Compared to exact CSS3 parsing we are liberal
103  * in what we accept as follows:
104  *
105  *  - For non-percentage values, we accept floats in the range 0-255
106  *    not just [0-9]+ integers
107  *  - For percentage values we accept any float, not just
108  *     [ 0-9]+ | [0-9]* '.' [0-9]+
109  *  - We accept mixed percentages and non-percentages in a single
110  *    rgb() or rgba() specification.
111  */
112 static double
113 parse_rgb_value (const char  *str,
114                  char       **endp)
115 {
116   double number;
117   const char *p;
118
119   number = g_ascii_strtod (str, endp);
120
121   p = *endp;
122
123   SKIP_WHITESPACES (p);
124
125   if (*p == '%')
126     {
127       *endp = (char *)(p + 1);
128       return CLAMP(number / 100., 0., 1.);
129     }
130   else
131     {
132       return CLAMP(number / 255., 0., 1.);
133     }
134 }
135
136 /**
137  * gdk_rgba_parse:
138  * @rgba: the #GdkRGBA struct to fill in
139  * @spec: the string specifying the color
140  *
141  * Parses a textual representation of a color, filling in
142  * the <structfield>red</structfield>, <structfield>green</structfield>,
143  * <structfield>blue</structfield> and <structfield>alpha</structfield>
144  * fields of the @rgba struct.
145  *
146  * The string can be either one of:
147  * <itemizedlist>
148  * <listitem>
149  * A standard name (Taken from the X11 rgb.txt file).
150  * </listitem>
151  * <listitem>
152  * A hex value in the form '#rgb' '#rrggbb' '#rrrgggbbb' or '#rrrrggggbbbb'
153  * </listitem>
154  * <listitem>
155  * A RGB color in the form 'rgb(r,g,b)' (In this case the color will
156  * have full opacity)
157  * </listitem>
158  * <listitem>
159  * A RGBA color in the form 'rgba(r,g,b,a)'
160  * </listitem>
161  * </itemizedlist>
162  *
163  * Where 'r', 'g', 'b' and 'a' are respectively the red, green, blue and
164  * alpha color values. In the last two cases, r g and b are either integers
165  * in the range 0 to 255 or precentage values in the range 0% to 100%, and
166  * a is a floating point value in the range 0 to 1.
167  *
168  * Returns: %TRUE if the parsing succeeded
169  *
170  * Since: 3.0
171  **/
172 gboolean
173 gdk_rgba_parse (GdkRGBA     *rgba,
174                 const gchar *spec)
175 {
176   gboolean has_alpha;
177   gdouble r, g, b, a;
178   gchar *str = (gchar *) spec;
179
180   if (strncmp (str, "rgba", 4) == 0)
181     {
182       has_alpha = TRUE;
183       str += 4;
184     }
185   else if (strncmp (str, "rgb", 3) == 0)
186     {
187       has_alpha = FALSE;
188       a = 1;
189       str += 3;
190     }
191   else
192     {
193       PangoColor pango_color;
194
195       /* Resort on PangoColor for rgb.txt color
196        * map and '#' prefixed colors */
197       if (pango_color_parse (&pango_color, str))
198         {
199           if (rgba)
200             {
201               rgba->red = pango_color.red / 65535.;
202               rgba->green = pango_color.green / 65535.;
203               rgba->blue = pango_color.blue / 65535.;
204               rgba->alpha = 1;
205             }
206
207           return TRUE;
208         }
209       else
210         return FALSE;
211     }
212
213   SKIP_WHITESPACES (str);
214
215   if (*str != '(')
216     return FALSE;
217
218   str++;
219
220   /* Parse red */
221   SKIP_WHITESPACES (str);
222   r = parse_rgb_value (str, &str);
223   SKIP_WHITESPACES (str);
224
225   if (*str != ',')
226     return FALSE;
227
228   str++;
229
230   /* Parse green */
231   SKIP_WHITESPACES (str);
232   g = parse_rgb_value (str, &str);
233   SKIP_WHITESPACES (str);
234
235   if (*str != ',')
236     return FALSE;
237
238   str++;
239
240   /* Parse blue */
241   SKIP_WHITESPACES (str);
242   b = parse_rgb_value (str, &str);
243   SKIP_WHITESPACES (str);
244
245   if (has_alpha)
246     {
247       if (*str != ',')
248         return FALSE;
249
250       str++;
251
252       SKIP_WHITESPACES (str);
253       a = g_ascii_strtod (str, &str);
254       SKIP_WHITESPACES (str);
255     }
256
257   if (*str != ')')
258     return FALSE;
259
260   if (rgba)
261     {
262       rgba->red = CLAMP (r, 0, 1);
263       rgba->green = CLAMP (g, 0, 1);
264       rgba->blue = CLAMP (b, 0, 1);
265       rgba->alpha = CLAMP (a, 0, 1);
266     }
267
268   return TRUE;
269 }
270
271 #undef SKIP_WHITESPACES
272
273 /**
274  * gdk_rgba_hash:
275  * @p: a #GdkRGBA pointer.
276  *
277  * A hash function suitable for using for a hash
278  * table that stores #GdkRGBA<!-- -->s.
279  *
280  * Return value: The hash function applied to @p
281  *
282  * Since: 3.0
283  **/
284 guint
285 gdk_rgba_hash (gconstpointer p)
286 {
287   const GdkRGBA *rgba = p;
288
289   return ((guint) (rgba->red * 65535) +
290           ((guint) (rgba->green * 65535) << 11) +
291           ((guint) (rgba->blue * 65535) << 22) +
292           ((guint) (rgba->alpha * 65535) >> 6));
293 }
294
295 /**
296  * gdk_rgba_equal:
297  * @p1: a #GdkRGBA pointer.
298  * @p2: another #GdkRGBA pointer.
299  *
300  * Compares two RGBA colors.
301  *
302  * Return value: %TRUE if the two colors compare equal
303  *
304  * Since: 3.0
305  **/
306 gboolean
307 gdk_rgba_equal (gconstpointer p1,
308                 gconstpointer p2)
309 {
310   const GdkRGBA *rgba1, *rgba2;
311
312   rgba1 = p1;
313   rgba2 = p2;
314
315   if (rgba1->red == rgba2->red &&
316       rgba1->green == rgba2->green &&
317       rgba1->blue == rgba2->blue &&
318       rgba1->alpha == rgba2->alpha)
319     return TRUE;
320
321   return FALSE;
322 }
323
324 /**
325  * gdk_rgba_to_string:
326  * @rgba: a #GdkRGBA
327  *
328  * Returns a textual specification of @rgba in the form <literal>rgb
329  * (r, g, b)</literal> or <literal>rgba (r, g, b, a)</literal>,
330  * where 'r', 'g', 'b' and 'a' represent the red, green, blue and alpha values
331  * respectively. r, g, and b are integers in the range 0 to 255, and a
332  * is a floating point value in the range 0 to 1.
333  *
334  * (These string forms are string forms those supported by the CSS3 colors module)
335  *
336  * Returns: A newly allocated text string
337  *
338  * Since: 3.0
339  **/
340 gchar *
341 gdk_rgba_to_string (const GdkRGBA *rgba)
342 {
343   if (rgba->alpha > 0.999)
344     {
345       return g_strdup_printf ("rgb(%d,%d,%d)",
346                               (int)(0.5 + CLAMP (rgba->red, 0., 1.) * 255.),
347                               (int)(0.5 + CLAMP (rgba->green, 0., 1.) * 255.),
348                               (int)(0.5 + CLAMP (rgba->blue, 0., 1.) * 255.));
349     }
350   else
351     {
352       gchar alpha[G_ASCII_DTOSTR_BUF_SIZE];
353
354       g_ascii_dtostr (alpha, G_ASCII_DTOSTR_BUF_SIZE, CLAMP (rgba->alpha, 0, 1));
355
356       return g_strdup_printf ("rgba(%d,%d,%d,%s)",
357                               (int)(0.5 + CLAMP (rgba->red, 0., 1.) * 255.),
358                               (int)(0.5 + CLAMP (rgba->green, 0., 1.) * 255.),
359                               (int)(0.5 + CLAMP (rgba->blue, 0., 1.) * 255.),
360                               alpha);
361     }
362 }