4 * An OpenGL based 'interactive canvas' library.
6 * Authored By Matthew Allum <mallum@openedhand.com>
8 * Copyright (C) 2006 OpenedHand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
27 * SECTION:clutter-color
28 * @short_description: Color management and manipulation.
30 * #ClutterColor is a simple type for representing colors in Clutter.
32 * A #ClutterColor is expressed as a 4-tuple of values ranging from
33 * zero to 255, one for each color channel plus one for the alpha.
40 #include <pango/pango-attributes.h>
41 #include <gobject/gvaluecollector.h>
43 #include "clutter-main.h"
44 #include "clutter-color.h"
45 #include "clutter-private.h"
46 #include "clutter-debug.h"
52 * @result: (out): return location for the result
54 * Adds @a to @b and saves the resulting color inside @result.
56 * The alpha channel of @result is set as as the maximum value
57 * between the alpha channels of @a and @b.
60 clutter_color_add (const ClutterColor *a,
61 const ClutterColor *b,
64 g_return_if_fail (a != NULL);
65 g_return_if_fail (b != NULL);
66 g_return_if_fail (result != NULL);
68 result->red = CLAMP (a->red + b->red, 0, 255);
69 result->green = CLAMP (a->green + b->green, 0, 255);
70 result->blue = CLAMP (a->blue + b->blue, 0, 255);
72 result->alpha = MAX (a->alpha, b->alpha);
76 * clutter_color_subtract:
79 * @result: (out): return location for the result
81 * Subtracts @b from @a and saves the resulting color inside @result.
83 * This function assumes that the components of @a are greater than the
84 * components of @b; the result is, otherwise, undefined.
86 * The alpha channel of @result is set as the minimum value
87 * between the alpha channels of @a and @b.
90 clutter_color_subtract (const ClutterColor *a,
91 const ClutterColor *b,
94 g_return_if_fail (a != NULL);
95 g_return_if_fail (b != NULL);
96 g_return_if_fail (result != NULL);
98 result->red = CLAMP (a->red - b->red, 0, 255);
99 result->green = CLAMP (a->green - b->green, 0, 255);
100 result->blue = CLAMP (a->blue - b->blue, 0, 255);
102 result->alpha = MIN (a->alpha, b->alpha);
106 * clutter_color_lighten:
107 * @color: a #ClutterColor
108 * @result: (out): return location for the lighter color
110 * Lightens @color by a fixed amount, and saves the changed color
114 clutter_color_lighten (const ClutterColor *color,
115 ClutterColor *result)
117 clutter_color_shade (color, 1.3, result);
121 * clutter_color_darken:
122 * @color: a #ClutterColor
123 * @result: (out): return location for the darker color
125 * Darkens @color by a fixed amount, and saves the changed color
129 clutter_color_darken (const ClutterColor *color,
130 ClutterColor *result)
132 clutter_color_shade (color, 0.7, result);
136 * clutter_color_to_hls:
137 * @color: a #ClutterColor
138 * @hue: return location for the hue value or %NULL
139 * @luminance: return location for the luminance value or %NULL
140 * @saturation: return location for the saturation value or %NULL
142 * Converts @color to the HLS format.
144 * The @hue value is in the 0 .. 360 range. The @luminance and
145 * @saturation values are in the 0 .. 1 range.
148 clutter_color_to_hls (const ClutterColor *color,
153 float red, green, blue;
154 float min, max, delta;
157 g_return_if_fail (color != NULL);
159 red = color->red / 255.0;
160 green = color->green / 255.0;
161 blue = color->blue / 255.0;
195 s = (max - min) / (max + min);
197 s = (max - min) / (2.0 - max - min);
202 h = (green - blue) / delta;
203 else if (green == max)
204 h = 2.0 + (blue - red) / delta;
205 else if (blue == max)
206 h = 4.0 + (red - green) / delta;
225 * clutter_color_from_hls:
226 * @color: (out): return location for a #ClutterColor
227 * @hue: hue value, in the 0 .. 360 range
228 * @luminance: luminance value, in the 0 .. 1 range
229 * @saturation: saturation value, in the 0 .. 1 range
231 * Converts a color expressed in HLS (hue, luminance and saturation)
232 * values into a #ClutterColor.
235 clutter_color_from_hls (ClutterColor *color,
249 color->red = color->green = color->blue = (luminance * 255);
254 if (luminance <= 0.5)
255 tmp2 = luminance * (1.0 + saturation);
257 tmp2 = luminance + saturation - (luminance * saturation);
259 tmp1 = 2.0 * luminance - tmp2;
261 tmp3[0] = hue + 1.0 / 3.0;
263 tmp3[2] = hue - 1.0 / 3.0;
265 for (i = 0; i < 3; i++)
273 if (6.0 * tmp3[i] < 1.0)
274 clr[i] = tmp1 + (tmp2 - tmp1) * tmp3[i] * 6.0;
275 else if (2.0 * tmp3[i] < 1.0)
277 else if (3.0 * tmp3[i] < 2.0)
278 clr[i] = (tmp1 + (tmp2 - tmp1) * ((2.0 / 3.0) - tmp3[i]) * 6.0);
283 color->red = clr[0] * 255.0;
284 color->green = clr[1] * 255.0;
285 color->blue = clr[2] * 255.0;
289 * clutter_color_shade:
290 * @color: a #ClutterColor
291 * @factor: the shade factor to apply
292 * @result: (out): return location for the shaded color
294 * Shades @color by @factor and saves the modified color into @result.
297 clutter_color_shade (const ClutterColor *color,
299 ClutterColor *result)
303 g_return_if_fail (color != NULL);
304 g_return_if_fail (result != NULL);
306 clutter_color_to_hls (color, &h, &l, &s);
320 clutter_color_from_hls (result, h, l, s);
322 result->alpha = color->alpha;
326 * clutter_color_to_pixel:
327 * @color: a #ClutterColor
329 * Converts @color into a packed 32 bit integer, containing
330 * all the four 8 bit channels used by #ClutterColor.
332 * Return value: a packed color
335 clutter_color_to_pixel (const ClutterColor *color)
337 g_return_val_if_fail (color != NULL, 0);
339 return (color->alpha |
346 * clutter_color_from_pixel:
347 * @color: (out): return location for a #ClutterColor
348 * @pixel: a 32 bit packed integer containing a color
350 * Converts @pixel from the packed representation of a four 8 bit channel
351 * color to a #ClutterColor.
354 clutter_color_from_pixel (ClutterColor *color,
357 g_return_if_fail (color != NULL);
359 color->red = pixel >> 24;
360 color->green = (pixel >> 16) & 0xff;
361 color->blue = (pixel >> 8) & 0xff;
362 color->alpha = pixel & 0xff;
366 * clutter_color_from_string:
367 * @color: (out): return location for a #ClutterColor
368 * @str: a string specifiying a color (named color or #RRGGBBAA)
370 * Parses a string definition of a color, filling the
371 * <structfield>red</structfield>, <structfield>green</structfield>,
372 * <structfield>blue</structfield> and <structfield>alpha</structfield>
373 * channels of @color. If alpha is not specified it will be set full opaque.
375 * The @color is not allocated.
377 * The color may be defined by any of the formats understood by
378 * pango_color_from_string(); these include literal color names, like
379 * <literal>Red</literal> or <literal>DarkSlateGray</literal>, or
380 * hexadecimal specifications like <literal>#3050b2</literal> or
381 * <literal>#333</literal>.
383 * Return value: %TRUE if parsing succeeded.
388 clutter_color_from_string (ClutterColor *color,
391 PangoColor pango_color = { 0, };
393 g_return_val_if_fail (color != NULL, FALSE);
394 g_return_val_if_fail (str != NULL, FALSE);
396 /* if the string contains a color encoded using the hexadecimal
397 * notations (#rrggbbaa or #rgba) we attempt a rough pass at
398 * parsing the color ourselves, as we need the alpha channel that
399 * Pango can't retrieve.
405 if (sscanf (str + 1, "%x", &result))
407 if (strlen (str) == 9)
410 color->red = (result >> 24) & 0xff;
411 color->green = (result >> 16) & 0xff;
412 color->blue = (result >> 8) & 0xff;
414 color->alpha = result & 0xff;
418 else if (strlen (str) == 5)
421 color->red = ((result >> 12) & 0xf);
422 color->green = ((result >> 8) & 0xf);
423 color->blue = ((result >> 4) & 0xf);
424 color->alpha = result & 0xf;
426 color->red = (color->red << 4) | color->red;
427 color->green = (color->green << 4) | color->green;
428 color->blue = (color->blue << 4) | color->blue;
429 color->alpha = (color->alpha << 4) | color->alpha;
436 /* Fall back to pango for named colors */
437 if (pango_color_parse (&pango_color, str))
439 color->red = pango_color.red;
440 color->green = pango_color.green;
441 color->blue = pango_color.blue;
452 * clutter_color_to_string:
453 * @color: a #ClutterColor
455 * Returns a textual specification of @color in the hexadecimal form
456 * <literal>#rrggbbaa</literal>, where <literal>r</literal>,
457 * <literal>g</literal>, <literal>b</literal> and <literal>a</literal> are
458 * hex digits representing the red, green, blue and alpha components
461 * Return value: a newly-allocated text string
466 clutter_color_to_string (const ClutterColor *color)
468 g_return_val_if_fail (color != NULL, NULL);
470 return g_strdup_printf ("#%02x%02x%02x%02x",
478 * clutter_color_equal:
479 * @v1: a #ClutterColor
480 * @v2: a #ClutterColor
482 * Compares two #ClutterColor<!-- -->s and checks if they are the same.
484 * This function can be passed to g_hash_table_new() as the @key_equal_func
485 * parameter, when using #ClutterColor<!-- -->s as keys in a #GHashTable.
487 * Return value: %TRUE if the two colors are the same.
492 clutter_color_equal (gconstpointer v1,
495 const ClutterColor *a, *b;
497 g_return_val_if_fail (v1 != NULL, FALSE);
498 g_return_val_if_fail (v2 != NULL, FALSE);
506 return (a->red == b->red &&
507 a->green == b->green &&
508 a->blue == b->blue &&
509 a->alpha == b->alpha);
513 * clutter_color_hash:
514 * @v: a #ClutterColor
516 * Converts a #ClutterColor to a hash value.
518 * This function can be passed to g_hash_table_new() as the @hash_func
519 * parameter, when using #ClutterColor<!-- -->s as keys in a #GHashTable.
521 * Return value: a hash value corresponding to the color
526 clutter_color_hash (gconstpointer v)
528 return clutter_color_to_pixel ((const ClutterColor *) v);
532 * clutter_color_copy:
533 * @color: a #ClutterColor
535 * Makes a copy of the color structure. The result must be
536 * freed using clutter_color_free().
538 * Return value: an allocated copy of @color.
543 clutter_color_copy (const ClutterColor *color)
545 if (G_LIKELY (color != NULL))
546 return g_slice_dup (ClutterColor, color);
552 * clutter_color_free:
553 * @color: a #ClutterColor
555 * Frees a color structure created with clutter_color_copy().
560 clutter_color_free (ClutterColor *color)
562 if (G_LIKELY (color != NULL))
563 g_slice_free (ClutterColor, color);
568 * @red: red component of the color, between 0 and 255
569 * @green: green component of the color, between 0 and 255
570 * @blue: blue component of the color, between 0 and 255
571 * @alpha: alpha component of the color, between 0 and 255
573 * Creates a new #ClutterColor with the given values.
575 * Return value: the newly allocated color. Use clutter_color_free()
581 clutter_color_new (guint8 red,
588 color = g_slice_new (ClutterColor);
591 color->green = green;
593 color->alpha = alpha;
599 clutter_value_transform_color_string (const GValue *src,
602 gchar *string = clutter_color_to_string (src->data[0].v_pointer);
604 g_value_take_string (dest, string);
608 clutter_value_transform_string_color (const GValue *src,
611 ClutterColor color = { 0, };
613 clutter_color_from_string (&color, g_value_get_string (src));
615 clutter_value_set_color (dest, &color);
619 clutter_color_get_type (void)
621 static GType _clutter_color_type = 0;
623 if (G_UNLIKELY (_clutter_color_type == 0))
625 _clutter_color_type =
626 g_boxed_type_register_static (I_("ClutterColor"),
627 (GBoxedCopyFunc) clutter_color_copy,
628 (GBoxedFreeFunc) clutter_color_free);
630 g_value_register_transform_func (_clutter_color_type, G_TYPE_STRING,
631 clutter_value_transform_color_string);
632 g_value_register_transform_func (G_TYPE_STRING, _clutter_color_type,
633 clutter_value_transform_string_color);
636 return _clutter_color_type;
640 clutter_value_init_color (GValue *value)
642 value->data[0].v_pointer = NULL;
646 clutter_value_free_color (GValue *value)
648 if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
649 clutter_color_free (value->data[0].v_pointer);
653 clutter_value_copy_color (const GValue *src,
656 dest->data[0].v_pointer = clutter_color_copy (src->data[0].v_pointer);
660 clutter_value_peek_color (const GValue *value)
662 return value->data[0].v_pointer;
666 clutter_value_collect_color (GValue *value,
667 guint n_collect_values,
668 GTypeCValue *collect_values,
671 if (!collect_values[0].v_pointer)
672 value->data[0].v_pointer = NULL;
675 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
677 value->data[0].v_pointer = collect_values[0].v_pointer;
678 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
682 value->data[0].v_pointer =
683 clutter_color_copy (collect_values[0].v_pointer);
691 clutter_value_lcopy_color (const GValue *value,
692 guint n_collect_values,
693 GTypeCValue *collect_values,
696 ClutterColor **color_p = collect_values[0].v_pointer;
699 return g_strdup_printf ("value location for '%s' passed as NULL",
700 G_VALUE_TYPE_NAME (value));
702 if (!value->data[0].v_pointer)
706 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
707 *color_p = value->data[0].v_pointer;
709 *color_p = clutter_color_copy (value->data[0].v_pointer);
716 * clutter_value_set_color:
717 * @value: a #GValue initialized to #CLUTTER_TYPE_COLOR
718 * @color: the color to set
720 * Sets @value to @color.
725 clutter_value_set_color (GValue *value,
726 const ClutterColor *color)
728 g_return_if_fail (CLUTTER_VALUE_HOLDS_COLOR (value));
730 value->data[0].v_pointer = clutter_color_copy (color);
734 * clutter_value_get_color:
735 * @value: a #GValue initialized to #CLUTTER_TYPE_COLOR
737 * Gets the #ClutterColor contained in @value.
739 * Return value: the colors inside the passed #GValue
743 G_CONST_RETURN ClutterColor *
744 clutter_value_get_color (const GValue *value)
746 g_return_val_if_fail (CLUTTER_VALUE_HOLDS_COLOR (value), NULL);
748 return value->data[0].v_pointer;
752 param_color_init (GParamSpec *pspec)
754 ClutterParamSpecColor *cspec = CLUTTER_PARAM_SPEC_COLOR (pspec);
756 cspec->default_value = NULL;
760 param_color_finalize (GParamSpec *pspec)
762 ClutterParamSpecColor *cspec = CLUTTER_PARAM_SPEC_COLOR (pspec);
764 clutter_color_free (cspec->default_value);
768 param_color_set_default (GParamSpec *pspec,
771 value->data[0].v_pointer = CLUTTER_PARAM_SPEC_COLOR (pspec)->default_value;
772 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
776 param_color_values_cmp (GParamSpec *pspec,
777 const GValue *value1,
778 const GValue *value2)
780 guint32 color1, color2;
782 color1 = clutter_color_to_pixel (value1->data[0].v_pointer);
783 color2 = clutter_color_to_pixel (value2->data[0].v_pointer);
787 else if (color1 == color2)
793 static const GTypeValueTable _clutter_color_value_table = {
794 clutter_value_init_color,
795 clutter_value_free_color,
796 clutter_value_copy_color,
797 clutter_value_peek_color,
799 clutter_value_collect_color,
801 clutter_value_lcopy_color
805 clutter_param_color_get_type (void)
807 static GType pspec_type = 0;
809 if (G_UNLIKELY (pspec_type == 0))
811 const GParamSpecTypeInfo pspec_info = {
812 sizeof (ClutterParamSpecColor),
816 param_color_finalize,
817 param_color_set_default,
819 param_color_values_cmp,
822 pspec_type = g_param_type_register_static (I_("ClutterParamSpecColor"),
830 * clutter_param_spec_color:
831 * @name: name of the property
833 * @blurb: description (can be translatable)
834 * @default_value: default value
835 * @flags: flags for the param spec
837 * Creates a #GParamSpec for properties using #ClutterColor.
839 * Return value: the newly created #GParamSpec
844 clutter_param_spec_color (const gchar *name,
847 const ClutterColor *default_value,
850 ClutterParamSpecColor *cspec;
852 cspec = g_param_spec_internal (CLUTTER_TYPE_PARAM_COLOR,
853 name, nick, blurb, flags);
855 cspec->default_value = clutter_color_copy (default_value);
857 return G_PARAM_SPEC (cspec);