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_hlsx:
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. Returned hue is in degrees (0 .. 360),
143 * luminance and saturation from interval <0 .. 1>.
145 * The implementation is in fixed point because we don't particularly
146 * care about precision. It can be moved to floating point at any later
150 clutter_color_to_hlsx (const ClutterColor *color,
152 CoglFixed *luminance,
153 CoglFixed *saturation)
155 CoglFixed red, green, blue;
156 CoglFixed min, max, delta;
159 g_return_if_fail (color != NULL);
161 red = COGL_FIXED_FAST_DIV (color->red, COGL_FIXED_255);
162 green = COGL_FIXED_FAST_DIV (color->green, COGL_FIXED_255);
163 blue = COGL_FIXED_FAST_DIV (color->blue, COGL_FIXED_255);
196 if (l <= COGL_FIXED_0_5)
197 s = COGL_FIXED_DIV ((max - min), (max + min));
199 s = COGL_FIXED_DIV ((max - min),
200 (COGL_FIXED_FROM_INT (2) - max - min));
205 h = COGL_FIXED_DIV ((green - blue), delta);
206 else if (green == max)
207 h = COGL_FIXED_FROM_INT (2) + COGL_FIXED_DIV ((blue - red), delta);
208 else if (blue == max)
209 h = COGL_FIXED_FROM_INT (4) + COGL_FIXED_DIV ((red - green), delta);
228 * clutter_color_from_hlsx:
229 * @dest: (out): return location for a #ClutterColor
230 * @hue: hue value (0 .. 360)
231 * @luminance: luminance value (0 .. 1)
232 * @saturation: saturation value (0 .. 1)
234 * Converts a color expressed in HLS (hue, luminance and saturation)
235 * values into a #ClutterColor.
238 clutter_color_from_hlsx (ClutterColor *color,
241 CoglFixed saturation)
246 g_return_if_fail (color != NULL);
251 if (l <= COGL_FIXED_0_5)
252 m2 = COGL_FIXED_MUL (l, (COGL_FIXED_1 + s));
254 m2 = l + s - COGL_FIXED_MUL (l, s);
260 color->red = (guint8) (COGL_FIXED_TO_INT (l) * 255);
261 color->green = (guint8) (COGL_FIXED_TO_INT (l) * 255);
262 color->blue = (guint8) (COGL_FIXED_TO_INT (l) * 255);
266 h = hue + COGL_FIXED_120;
268 while (h > COGL_FIXED_360)
274 if (h < COGL_FIXED_60)
278 tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1), h, COGL_FIXED_60));
279 color->red = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
281 else if (h < COGL_FIXED_180)
282 color->red = (guint8) (COGL_FIXED_TO_INT (m2) * 255);
283 else if (h < COGL_FIXED_240)
287 tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1),
288 (COGL_FIXED_240 - h),
290 color->red = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
293 color->red = (guint8) (COGL_FIXED_TO_INT (m1) * 255);
296 while (h > COGL_FIXED_360)
301 if (h < COGL_FIXED_60)
305 tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1), h, COGL_FIXED_60));
306 color->green = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
308 else if (h < COGL_FIXED_180)
309 color->green = (guint8) (COGL_FIXED_TO_INT (m2) * 255);
310 else if (h < COGL_FIXED_240)
314 tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1),
315 (COGL_FIXED_240 - h),
317 color->green = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
320 color->green = (guint8) (COGL_FIXED_TO_INT (m1) * 255);
322 h = hue - COGL_FIXED_120;
324 while (h > COGL_FIXED_360)
330 if (h < COGL_FIXED_60)
334 tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1), h, COGL_FIXED_60));
335 color->blue = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
337 else if (h < COGL_FIXED_180)
338 color->blue = (guint8) (COGL_FIXED_TO_INT (m2) * 255);
339 else if (h < COGL_FIXED_240)
343 tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1),
344 (COGL_FIXED_240 - h),
346 color->blue = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
349 color->blue = (guint8) (COGL_FIXED_TO_INT (m1) * 255);
354 * clutter_color_to_hls:
355 * @color: a #ClutterColor
356 * @hue: return location for the hue value or %NULL
357 * @luminance: return location for the luminance value or %NULL
358 * @saturation: return location for the saturation value or %NULL
360 * Converts @color to the HLS format.
362 * The @hue value is in the 0 .. 360 range. The @luminance and
363 * @saturation values are in the 0 .. 1 range.
366 clutter_color_to_hls (const ClutterColor *color,
373 clutter_color_to_hlsx (color, &h, &l, &s);
376 *hue = COGL_FIXED_TO_FLOAT (h);
379 *luminance = COGL_FIXED_TO_FLOAT (l);
382 *saturation = COGL_FIXED_TO_FLOAT (s);
386 * clutter_color_from_hls:
387 * @color: (out): return location for a #ClutterColor
388 * @hue: hue value, in the 0 .. 360 range
389 * @luminance: luminance value, in the 0 .. 1 range
390 * @saturation: saturation value, in the 0 .. 1 range
392 * Converts a color expressed in HLS (hue, luminance and saturation)
393 * values into a #ClutterColor.
396 clutter_color_from_hls (ClutterColor *color,
403 h = COGL_FIXED_FROM_FLOAT (hue);
404 l = COGL_FIXED_FROM_FLOAT (luminance);
405 s = COGL_FIXED_FROM_FLOAT (saturation);
407 clutter_color_from_hlsx (color, h, l, s);
411 * clutter_color_shadex:
412 * @color: a #ClutterColor
413 * @factor: the shade factor to apply, as a fixed point value
414 * @result: (out): return location for the shaded color
416 * Shades @color by @factor and saves the modified color into @result.
419 clutter_color_shadex (const ClutterColor *color,
421 ClutterColor *result)
425 g_return_if_fail (color != NULL);
426 g_return_if_fail (result != NULL);
428 clutter_color_to_hlsx (color, &h, &l, &s);
430 l = COGL_FIXED_MUL (l, factor);
431 if (l > COGL_FIXED_1)
436 s = COGL_FIXED_MUL (s, factor);
437 if (s > COGL_FIXED_1)
442 clutter_color_from_hlsx (result, h, l, s);
444 result->alpha = color->alpha;
448 * clutter_color_shade:
449 * @color: a #ClutterColor
450 * @factor: the shade factor to apply
451 * @result: (out): return location for the shaded color
453 * Shades @color by @factor and saves the modified color into @result.
456 clutter_color_shade (const ClutterColor *color,
458 ClutterColor *result)
460 clutter_color_shadex (color,
461 COGL_FIXED_FROM_FLOAT (factor),
466 * clutter_color_to_pixel:
467 * @color: a #ClutterColor
469 * Converts @color into a packed 32 bit integer, containing
470 * all the four 8 bit channels used by #ClutterColor.
472 * Return value: a packed color
475 clutter_color_to_pixel (const ClutterColor *color)
477 g_return_val_if_fail (color != NULL, 0);
479 return (color->alpha |
486 * clutter_color_from_pixel:
487 * @color: (out): return location for a #ClutterColor
488 * @pixel: a 32 bit packed integer containing a color
490 * Converts @pixel from the packed representation of a four 8 bit channel
491 * color to a #ClutterColor.
494 clutter_color_from_pixel (ClutterColor *color,
497 g_return_if_fail (color != NULL);
499 color->red = pixel >> 24;
500 color->green = (pixel >> 16) & 0xff;
501 color->blue = (pixel >> 8) & 0xff;
502 color->alpha = pixel & 0xff;
506 * clutter_color_from_string:
507 * @color: (out): return location for a #ClutterColor
508 * @str: a string specifiying a color (named color or #RRGGBBAA)
510 * Parses a string definition of a color, filling the
511 * <structfield>red</structfield>, <structfield>green</structfield>,
512 * <structfield>blue</structfield> and <structfield>alpha</structfield>
513 * channels of @color. If alpha is not specified it will be set full opaque.
515 * The @color is not allocated.
517 * The color may be defined by any of the formats understood by
518 * pango_color_from_string(); these include literal color names, like
519 * <literal>Red</literal> or <literal>DarkSlateGray</literal>, or
520 * hexadecimal specifications like <literal>#3050b2</literal> or
521 * <literal>#333</literal>.
523 * Return value: %TRUE if parsing succeeded.
528 clutter_color_from_string (ClutterColor *color,
531 PangoColor pango_color = { 0, };
533 g_return_val_if_fail (color != NULL, FALSE);
534 g_return_val_if_fail (str != NULL, FALSE);
536 /* if the string contains a color encoded using the hexadecimal
537 * notations (#rrggbbaa or #rrggbb) we attempt a rough pass at
538 * parsing the color ourselves, as we need the alpha channel that
539 * Pango can't retrieve.
545 if (sscanf (str + 1, "%x", &result))
547 if (strlen (str) == 9)
550 color->red = (result >> 24) & 0xff;
551 color->green = (result >> 16) & 0xff;
552 color->blue = (result >> 8) & 0xff;
554 color->alpha = result & 0xff;
558 else if (strlen (str) == 7)
561 color->red = (result >> 16) & 0xff;
562 color->green = (result >> 8) & 0xff;
563 color->blue = result & 0xff;
571 /* XXX - should we return FALSE here? it's not like
572 * Pango is endowed with mystical parsing powers and
573 * will be able to do better than the code above.
574 * still, it doesn't hurt
578 /* Fall back to pango for named colors */
579 if (pango_color_parse (&pango_color, str))
581 color->red = pango_color.red;
582 color->green = pango_color.green;
583 color->blue = pango_color.blue;
594 * clutter_color_to_string:
595 * @color: a #ClutterColor
597 * Returns a textual specification of @color in the hexadecimal form
598 * <literal>#rrggbbaa</literal>, where <literal>r</literal>,
599 * <literal>g</literal>, <literal>b</literal> and <literal>a</literal> are
600 * hex digits representing the red, green, blue and alpha components
603 * Return value: a newly-allocated text string
608 clutter_color_to_string (const ClutterColor *color)
610 g_return_val_if_fail (color != NULL, NULL);
612 return g_strdup_printf ("#%02x%02x%02x%02x",
620 * clutter_color_equal:
621 * @v1: a #ClutterColor
622 * @v2: a #ClutterColor
624 * Compares two #ClutterColor<!-- -->s and checks if they are the same.
626 * This function can be passed to g_hash_table_new() as the @key_equal_func
627 * parameter, when using #ClutterColor<!-- -->s as keys in a #GHashTable.
629 * Return value: %TRUE if the two colors are the same.
634 clutter_color_equal (gconstpointer v1,
637 const ClutterColor *a, *b;
639 g_return_val_if_fail (v1 != NULL, FALSE);
640 g_return_val_if_fail (v2 != NULL, FALSE);
648 return (a->red == b->red &&
649 a->green == b->green &&
650 a->blue == b->blue &&
651 a->alpha == b->alpha);
655 * clutter_color_hash:
656 * @v: a #ClutterColor
658 * Converts a #ClutterColor to a hash value.
660 * This function can be passed to g_hash_table_new() as the @hash_func
661 * parameter, when using #ClutterColor<!-- -->s as keys in a #GHashTable.
663 * Return value: a hash value corresponding to the color
668 clutter_color_hash (gconstpointer v)
670 return clutter_color_to_pixel ((const ClutterColor *) v);
674 * clutter_color_copy:
675 * @color: a #ClutterColor
677 * Makes a copy of the color structure. The result must be
678 * freed using clutter_color_free().
680 * Return value: an allocated copy of @color.
685 clutter_color_copy (const ClutterColor *color)
687 if (G_LIKELY (color != NULL))
688 return g_slice_dup (ClutterColor, color);
694 * clutter_color_free:
695 * @color: a #ClutterColor
697 * Frees a color structure created with clutter_color_copy().
702 clutter_color_free (ClutterColor *color)
704 if (G_LIKELY (color != NULL))
705 g_slice_free (ClutterColor, color);
710 * @red: red component of the color, between 0 and 255
711 * @green: green component of the color, between 0 and 255
712 * @blue: blue component of the color, between 0 and 255
713 * @alpha: alpha component of the color, between 0 and 255
715 * Creates a new #ClutterColor with the given values.
717 * Return value: the newly allocated color. Use clutter_color_free()
723 clutter_color_new (guint8 red,
730 color = g_slice_new (ClutterColor);
733 color->green = green;
735 color->alpha = alpha;
741 clutter_value_transform_color_string (const GValue *src,
744 gchar *string = clutter_color_to_string (src->data[0].v_pointer);
746 g_value_take_string (dest, string);
750 clutter_value_transform_string_color (const GValue *src,
753 ClutterColor color = { 0, };
755 clutter_color_from_string (&color, g_value_get_string (src));
757 clutter_value_set_color (dest, &color);
761 clutter_color_get_type (void)
763 static GType _clutter_color_type = 0;
765 if (G_UNLIKELY (_clutter_color_type == 0))
767 _clutter_color_type =
768 g_boxed_type_register_static (I_("ClutterColor"),
769 (GBoxedCopyFunc) clutter_color_copy,
770 (GBoxedFreeFunc) clutter_color_free);
772 g_value_register_transform_func (_clutter_color_type, G_TYPE_STRING,
773 clutter_value_transform_color_string);
774 g_value_register_transform_func (G_TYPE_STRING, _clutter_color_type,
775 clutter_value_transform_string_color);
778 return _clutter_color_type;
782 clutter_value_init_color (GValue *value)
784 value->data[0].v_pointer = NULL;
788 clutter_value_free_color (GValue *value)
790 if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
791 clutter_color_free (value->data[0].v_pointer);
795 clutter_value_copy_color (const GValue *src,
798 dest->data[0].v_pointer = clutter_color_copy (src->data[0].v_pointer);
802 clutter_value_peek_color (const GValue *value)
804 return value->data[0].v_pointer;
808 clutter_value_collect_color (GValue *value,
809 guint n_collect_values,
810 GTypeCValue *collect_values,
813 if (!collect_values[0].v_pointer)
814 value->data[0].v_pointer = NULL;
817 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
819 value->data[0].v_pointer = collect_values[0].v_pointer;
820 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
824 value->data[0].v_pointer =
825 clutter_color_copy (collect_values[0].v_pointer);
833 clutter_value_lcopy_color (const GValue *value,
834 guint n_collect_values,
835 GTypeCValue *collect_values,
838 ClutterColor **color_p = collect_values[0].v_pointer;
841 return g_strdup_printf ("value location for `%s' passed as NULL",
842 G_VALUE_TYPE_NAME (value));
844 if (!value->data[0].v_pointer)
848 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
849 *color_p = value->data[0].v_pointer;
851 *color_p = clutter_color_copy (value->data[0].v_pointer);
858 * clutter_value_set_color:
859 * @value: a #GValue initialized to #CLUTTER_TYPE_COLOR
860 * @color: the color to set
862 * Sets @value to @color.
867 clutter_value_set_color (GValue *value,
868 const ClutterColor *color)
870 g_return_if_fail (CLUTTER_VALUE_HOLDS_COLOR (value));
872 value->data[0].v_pointer = clutter_color_copy (color);
876 * clutter_value_get_color:
877 * @value: a #GValue initialized to #CLUTTER_TYPE_COLOR
879 * Gets the #ClutterColor contained in @value.
881 * Return value: the colors inside the passed #GValue
885 G_CONST_RETURN ClutterColor *
886 clutter_value_get_color (const GValue *value)
888 g_return_val_if_fail (CLUTTER_VALUE_HOLDS_COLOR (value), NULL);
890 return value->data[0].v_pointer;
894 param_color_init (GParamSpec *pspec)
896 ClutterParamSpecColor *cspec = CLUTTER_PARAM_SPEC_COLOR (pspec);
898 cspec->default_value = NULL;
902 param_color_finalize (GParamSpec *pspec)
904 ClutterParamSpecColor *cspec = CLUTTER_PARAM_SPEC_COLOR (pspec);
906 clutter_color_free (cspec->default_value);
910 param_color_set_default (GParamSpec *pspec,
913 value->data[0].v_pointer = CLUTTER_PARAM_SPEC_COLOR (pspec)->default_value;
914 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
918 param_color_values_cmp (GParamSpec *pspec,
919 const GValue *value1,
920 const GValue *value2)
922 guint32 color1, color2;
924 color1 = clutter_color_to_pixel (value1->data[0].v_pointer);
925 color2 = clutter_color_to_pixel (value2->data[0].v_pointer);
929 else if (color1 == color2)
935 static const GTypeValueTable _clutter_color_value_table = {
936 clutter_value_init_color,
937 clutter_value_free_color,
938 clutter_value_copy_color,
939 clutter_value_peek_color,
941 clutter_value_collect_color,
943 clutter_value_lcopy_color
947 clutter_param_color_get_type (void)
949 static GType pspec_type = 0;
951 if (G_UNLIKELY (pspec_type == 0))
953 const GParamSpecTypeInfo pspec_info = {
954 sizeof (ClutterParamSpecColor),
958 param_color_finalize,
959 param_color_set_default,
961 param_color_values_cmp,
964 pspec_type = g_param_type_register_static (I_("ClutterParamSpecColor"),
972 * clutter_param_spec_color:
973 * @name: name of the property
975 * @blurb: description (can be translatable)
976 * @default_value: default value
977 * @flags: flags for the param spec
979 * Creates a #GParamSpec for properties using #ClutterColor.
981 * Return value: the newly created #GParamSpec
986 clutter_param_spec_color (const gchar *name,
989 const ClutterColor *default_value,
992 ClutterParamSpecColor *cspec;
994 cspec = g_param_spec_internal (CLUTTER_TYPE_PARAM_COLOR,
995 name, nick, blurb, flags);
997 cspec->default_value = clutter_color_copy (default_value);
999 return G_PARAM_SPEC (cspec);