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