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, see <http://www.gnu.org/licenses/>.
25 * SECTION:clutter-color
26 * @short_description: Color management and manipulation.
28 * #ClutterColor is a simple type for representing colors in Clutter.
30 * A #ClutterColor is expressed as a 4-tuple of values ranging from
31 * zero to 255, one for each color channel plus one for the alpha.
33 * The alpha channel is fully opaque at 255 and fully transparent at 0.
42 #include <pango/pango-attributes.h>
44 #include "clutter-interval.h"
45 #include "clutter-main.h"
46 #include "clutter-color.h"
47 #include "clutter-private.h"
48 #include "clutter-debug.h"
54 * @result: (out caller-allocates): return location for the result
56 * Adds @a to @b and saves the resulting color inside @result.
58 * The alpha channel of @result is set as as the maximum value
59 * between the alpha channels of @a and @b.
62 clutter_color_add (const ClutterColor *a,
63 const ClutterColor *b,
66 g_return_if_fail (a != NULL);
67 g_return_if_fail (b != NULL);
68 g_return_if_fail (result != NULL);
70 result->red = CLAMP (a->red + b->red, 0, 255);
71 result->green = CLAMP (a->green + b->green, 0, 255);
72 result->blue = CLAMP (a->blue + b->blue, 0, 255);
74 result->alpha = MAX (a->alpha, b->alpha);
78 * clutter_color_subtract:
81 * @result: (out caller-allocates): return location for the result
83 * Subtracts @b from @a and saves the resulting color inside @result.
85 * This function assumes that the components of @a are greater than the
86 * components of @b; the result is, otherwise, undefined.
88 * The alpha channel of @result is set as the minimum value
89 * between the alpha channels of @a and @b.
92 clutter_color_subtract (const ClutterColor *a,
93 const ClutterColor *b,
96 g_return_if_fail (a != NULL);
97 g_return_if_fail (b != NULL);
98 g_return_if_fail (result != NULL);
100 result->red = CLAMP (a->red - b->red, 0, 255);
101 result->green = CLAMP (a->green - b->green, 0, 255);
102 result->blue = CLAMP (a->blue - b->blue, 0, 255);
104 result->alpha = MIN (a->alpha, b->alpha);
108 * clutter_color_lighten:
109 * @color: a #ClutterColor
110 * @result: (out caller-allocates): return location for the lighter color
112 * Lightens @color by a fixed amount, and saves the changed color
116 clutter_color_lighten (const ClutterColor *color,
117 ClutterColor *result)
119 clutter_color_shade (color, 1.3, result);
123 * clutter_color_darken:
124 * @color: a #ClutterColor
125 * @result: (out caller-allocates): return location for the darker color
127 * Darkens @color by a fixed amount, and saves the changed color
131 clutter_color_darken (const ClutterColor *color,
132 ClutterColor *result)
134 clutter_color_shade (color, 0.7, result);
138 * clutter_color_to_hls:
139 * @color: a #ClutterColor
140 * @hue: (out): return location for the hue value or %NULL
141 * @luminance: (out): return location for the luminance value or %NULL
142 * @saturation: (out): return location for the saturation value or %NULL
144 * Converts @color to the HLS format.
146 * The @hue value is in the 0 .. 360 range. The @luminance and
147 * @saturation values are in the 0 .. 1 range.
150 clutter_color_to_hls (const ClutterColor *color,
155 float red, green, blue;
156 float min, max, delta;
159 g_return_if_fail (color != NULL);
161 red = color->red / 255.0;
162 green = color->green / 255.0;
163 blue = color->blue / 255.0;
197 s = (max - min) / (max + min);
199 s = (max - min) / (2.0 - max - min);
204 h = (green - blue) / delta;
205 else if (green == max)
206 h = 2.0 + (blue - red) / delta;
207 else if (blue == max)
208 h = 4.0 + (red - green) / delta;
227 * clutter_color_from_hls:
228 * @color: (out): return location for a #ClutterColor
229 * @hue: hue value, in the 0 .. 360 range
230 * @luminance: luminance value, in the 0 .. 1 range
231 * @saturation: saturation value, in the 0 .. 1 range
233 * Converts a color expressed in HLS (hue, luminance and saturation)
234 * values into a #ClutterColor.
237 clutter_color_from_hls (ClutterColor *color,
251 color->red = color->green = color->blue = (luminance * 255);
256 if (luminance <= 0.5)
257 tmp2 = luminance * (1.0 + saturation);
259 tmp2 = luminance + saturation - (luminance * saturation);
261 tmp1 = 2.0 * luminance - tmp2;
263 tmp3[0] = hue + 1.0 / 3.0;
265 tmp3[2] = hue - 1.0 / 3.0;
267 for (i = 0; i < 3; i++)
275 if (6.0 * tmp3[i] < 1.0)
276 clr[i] = tmp1 + (tmp2 - tmp1) * tmp3[i] * 6.0;
277 else if (2.0 * tmp3[i] < 1.0)
279 else if (3.0 * tmp3[i] < 2.0)
280 clr[i] = (tmp1 + (tmp2 - tmp1) * ((2.0 / 3.0) - tmp3[i]) * 6.0);
285 color->red = floorf (clr[0] * 255.0 + 0.5);
286 color->green = floorf (clr[1] * 255.0 + 0.5);
287 color->blue = floorf (clr[2] * 255.0 + 0.5);
291 * clutter_color_shade:
292 * @color: a #ClutterColor
293 * @factor: the shade factor to apply
294 * @result: (out caller-allocates): return location for the shaded color
296 * Shades @color by @factor and saves the modified color into @result.
299 clutter_color_shade (const ClutterColor *color,
301 ClutterColor *result)
305 g_return_if_fail (color != NULL);
306 g_return_if_fail (result != NULL);
308 clutter_color_to_hls (color, &h, &l, &s);
322 clutter_color_from_hls (result, h, l, s);
324 result->alpha = color->alpha;
328 * clutter_color_to_pixel:
329 * @color: a #ClutterColor
331 * Converts @color into a packed 32 bit integer, containing
332 * all the four 8 bit channels used by #ClutterColor.
334 * Return value: a packed color
337 clutter_color_to_pixel (const ClutterColor *color)
339 g_return_val_if_fail (color != NULL, 0);
341 return (color->alpha |
348 * clutter_color_from_pixel:
349 * @color: (out caller-allocates): return location for a #ClutterColor
350 * @pixel: a 32 bit packed integer containing a color
352 * Converts @pixel from the packed representation of a four 8 bit channel
353 * color to a #ClutterColor.
356 clutter_color_from_pixel (ClutterColor *color,
359 g_return_if_fail (color != NULL);
361 color->red = pixel >> 24;
362 color->green = (pixel >> 16) & 0xff;
363 color->blue = (pixel >> 8) & 0xff;
364 color->alpha = pixel & 0xff;
368 * clutter_color_from_string:
369 * @color: (out caller-allocates): return location for a #ClutterColor
370 * @str: a string specifiying a color (named color or #RRGGBBAA)
372 * Parses a string definition of a color, filling the
373 * <structfield>red</structfield>, <structfield>green</structfield>,
374 * <structfield>blue</structfield> and <structfield>alpha</structfield>
375 * channels of @color. If alpha is not specified it will be set full opaque.
377 * The @color is not allocated.
379 * The color may be defined by any of the formats understood by
380 * pango_color_from_string(); these include literal color names, like
381 * <literal>Red</literal> or <literal>DarkSlateGray</literal>, or
382 * hexadecimal specifications like <literal>#3050b2</literal> or
383 * <literal>#333</literal>.
385 * Return value: %TRUE if parsing succeeded.
390 clutter_color_from_string (ClutterColor *color,
393 PangoColor pango_color = { 0, };
395 g_return_val_if_fail (color != NULL, FALSE);
396 g_return_val_if_fail (str != NULL, FALSE);
398 /* if the string contains a color encoded using the hexadecimal
399 * notations (#rrggbbaa or #rgba) we attempt a rough pass at
400 * parsing the color ourselves, as we need the alpha channel that
401 * Pango can't retrieve.
407 if (sscanf (str + 1, "%x", &result))
409 gsize length = strlen (str);
413 case 9: /* rrggbbaa */
414 color->red = (result >> 24) & 0xff;
415 color->green = (result >> 16) & 0xff;
416 color->blue = (result >> 8) & 0xff;
418 color->alpha = result & 0xff;
422 case 7: /* #rrggbb */
423 color->red = (result >> 16) & 0xff;
424 color->green = (result >> 8) & 0xff;
425 color->blue = result & 0xff;
432 color->red = ((result >> 12) & 0xf);
433 color->green = ((result >> 8) & 0xf);
434 color->blue = ((result >> 4) & 0xf);
435 color->alpha = result & 0xf;
437 color->red = (color->red << 4) | color->red;
438 color->green = (color->green << 4) | color->green;
439 color->blue = (color->blue << 4) | color->blue;
440 color->alpha = (color->alpha << 4) | color->alpha;
445 color->red = ((result >> 8) & 0xf);
446 color->green = ((result >> 4) & 0xf);
447 color->blue = result & 0xf;
449 color->red = (color->red << 4) | color->red;
450 color->green = (color->green << 4) | color->green;
451 color->blue = (color->blue << 4) | color->blue;
458 /* pass through to Pango */
464 /* Fall back to pango for named colors */
465 if (pango_color_parse (&pango_color, str))
467 color->red = pango_color.red;
468 color->green = pango_color.green;
469 color->blue = pango_color.blue;
480 * clutter_color_to_string:
481 * @color: a #ClutterColor
483 * Returns a textual specification of @color in the hexadecimal form
484 * <literal>#rrggbbaa</literal>, where <literal>r</literal>,
485 * <literal>g</literal>, <literal>b</literal> and <literal>a</literal> are
486 * hex digits representing the red, green, blue and alpha components
489 * Return value: (transfer full): a newly-allocated text string
494 clutter_color_to_string (const ClutterColor *color)
496 g_return_val_if_fail (color != NULL, NULL);
498 return g_strdup_printf ("#%02x%02x%02x%02x",
506 * clutter_color_equal:
507 * @v1: a #ClutterColor
508 * @v2: a #ClutterColor
510 * Compares two #ClutterColor<!-- -->s and checks if they are the same.
512 * This function can be passed to g_hash_table_new() as the @key_equal_func
513 * parameter, when using #ClutterColor<!-- -->s as keys in a #GHashTable.
515 * Return value: %TRUE if the two colors are the same.
520 clutter_color_equal (gconstpointer v1,
523 const ClutterColor *a, *b;
525 g_return_val_if_fail (v1 != NULL, FALSE);
526 g_return_val_if_fail (v2 != NULL, FALSE);
534 return (a->red == b->red &&
535 a->green == b->green &&
536 a->blue == b->blue &&
537 a->alpha == b->alpha);
541 * clutter_color_hash:
542 * @v: a #ClutterColor
544 * Converts a #ClutterColor to a hash value.
546 * This function can be passed to g_hash_table_new() as the @hash_func
547 * parameter, when using #ClutterColor<!-- -->s as keys in a #GHashTable.
549 * Return value: a hash value corresponding to the color
554 clutter_color_hash (gconstpointer v)
556 return clutter_color_to_pixel ((const ClutterColor *) v);
560 * clutter_color_interpolate:
561 * @initial: the initial #ClutterColor
562 * @final: the final #ClutterColor
563 * @progress: the interpolation progress
564 * @result: (out): return location for the interpolation
566 * Interpolates between @initial and @final #ClutterColor<!-- -->s
572 clutter_color_interpolate (const ClutterColor *initial,
573 const ClutterColor *final,
575 ClutterColor *result)
577 g_return_if_fail (initial != NULL);
578 g_return_if_fail (final != NULL);
579 g_return_if_fail (result != NULL);
581 result->red = initial->red + (final->red - initial->red) * progress;
582 result->green = initial->green + (final->green - initial->green) * progress;
583 result->blue = initial->blue + (final->blue - initial->blue) * progress;
584 result->alpha = initial->alpha + (final->alpha - initial->alpha) * progress;
588 clutter_color_progress (const GValue *a,
593 const ClutterColor *a_color = clutter_value_get_color (a);
594 const ClutterColor *b_color = clutter_value_get_color (b);
595 ClutterColor res = { 0, };
597 clutter_color_interpolate (a_color, b_color, progress, &res);
598 clutter_value_set_color (retval, &res);
604 * clutter_color_copy:
605 * @color: a #ClutterColor
607 * Makes a copy of the color structure. The result must be
608 * freed using clutter_color_free().
610 * Return value: (transfer full): an allocated copy of @color.
615 clutter_color_copy (const ClutterColor *color)
617 if (G_LIKELY (color != NULL))
618 return g_slice_dup (ClutterColor, color);
624 * clutter_color_free:
625 * @color: a #ClutterColor
627 * Frees a color structure created with clutter_color_copy().
632 clutter_color_free (ClutterColor *color)
634 if (G_LIKELY (color != NULL))
635 g_slice_free (ClutterColor, color);
640 * @red: red component of the color, between 0 and 255
641 * @green: green component of the color, between 0 and 255
642 * @blue: blue component of the color, between 0 and 255
643 * @alpha: alpha component of the color, between 0 and 255
645 * Creates a new #ClutterColor with the given values.
647 * Return value: (transfer full): the newly allocated color.
648 * Use clutter_color_free() when done
653 clutter_color_new (guint8 red,
660 color = g_slice_new (ClutterColor);
663 color->green = green;
665 color->alpha = alpha;
671 clutter_value_transform_color_string (const GValue *src,
674 const ClutterColor *color = g_value_get_boxed (src);
678 gchar *string = clutter_color_to_string (color);
680 g_value_take_string (dest, string);
683 g_value_set_string (dest, NULL);
687 clutter_value_transform_string_color (const GValue *src,
690 const char *str = g_value_get_string (src);
694 ClutterColor color = { 0, };
696 clutter_color_from_string (&color, str);
698 clutter_value_set_color (dest, &color);
701 clutter_value_set_color (dest, NULL);
704 G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterColor, clutter_color,
707 CLUTTER_REGISTER_VALUE_TRANSFORM_TO (G_TYPE_STRING, clutter_value_transform_color_string)
708 CLUTTER_REGISTER_VALUE_TRANSFORM_FROM (G_TYPE_STRING, clutter_value_transform_string_color)
709 CLUTTER_REGISTER_INTERVAL_PROGRESS (clutter_color_progress));
712 * clutter_value_set_color:
713 * @value: a #GValue initialized to #CLUTTER_TYPE_COLOR
714 * @color: the color to set
716 * Sets @value to @color.
721 clutter_value_set_color (GValue *value,
722 const ClutterColor *color)
724 g_return_if_fail (CLUTTER_VALUE_HOLDS_COLOR (value));
726 g_value_set_boxed (value, color);
730 * clutter_value_get_color:
731 * @value: a #GValue initialized to #CLUTTER_TYPE_COLOR
733 * Gets the #ClutterColor contained in @value.
735 * Return value: (transfer none): the color inside the passed #GValue
739 G_CONST_RETURN ClutterColor *
740 clutter_value_get_color (const GValue *value)
742 g_return_val_if_fail (CLUTTER_VALUE_HOLDS_COLOR (value), NULL);
744 return g_value_get_boxed (value);
748 param_color_init (GParamSpec *pspec)
750 ClutterParamSpecColor *cspec = CLUTTER_PARAM_SPEC_COLOR (pspec);
752 cspec->default_value = NULL;
756 param_color_finalize (GParamSpec *pspec)
758 ClutterParamSpecColor *cspec = CLUTTER_PARAM_SPEC_COLOR (pspec);
760 clutter_color_free (cspec->default_value);
764 param_color_set_default (GParamSpec *pspec,
767 const ClutterColor *default_value =
768 CLUTTER_PARAM_SPEC_COLOR (pspec)->default_value;
769 clutter_value_set_color (value, default_value);
773 param_color_values_cmp (GParamSpec *pspec,
774 const GValue *value1,
775 const GValue *value2)
777 const ClutterColor *color1 = g_value_get_boxed (value1);
778 const ClutterColor *color2 = g_value_get_boxed (value2);
782 return color2 == NULL ? 0 : -1;
784 pixel1 = clutter_color_to_pixel (color1);
785 pixel2 = clutter_color_to_pixel (color2);
789 else if (pixel1 == pixel2)
796 clutter_param_color_get_type (void)
798 static GType pspec_type = 0;
800 if (G_UNLIKELY (pspec_type == 0))
802 const GParamSpecTypeInfo pspec_info = {
803 sizeof (ClutterParamSpecColor),
807 param_color_finalize,
808 param_color_set_default,
810 param_color_values_cmp,
813 pspec_type = g_param_type_register_static (I_("ClutterParamSpecColor"),
821 * clutter_param_spec_color: (skip)
822 * @name: name of the property
824 * @blurb: description (can be translatable)
825 * @default_value: default value
826 * @flags: flags for the param spec
828 * Creates a #GParamSpec for properties using #ClutterColor.
830 * Return value: the newly created #GParamSpec
835 clutter_param_spec_color (const gchar *name,
838 const ClutterColor *default_value,
841 ClutterParamSpecColor *cspec;
843 cspec = g_param_spec_internal (CLUTTER_TYPE_PARAM_COLOR,
844 name, nick, blurb, flags);
846 cspec->default_value = clutter_color_copy (default_value);
848 return G_PARAM_SPEC (cspec);