Clean up of the ClutterColor API

The ClutterColor API has some inconsistencies:

  - the string deserialization function does not match the rest of
    the conversion function naming policy; the naming should be:

        clutter_color_parse()   ->      clutter_color_from_string()

    and the first parameter should be the ClutterColor that will
    be set from the string, not the string itself (a GDK-ism).

  - the fixed point API should not be exposed, especially in the
    form of ClutterFixed values

  - the non-fixed point HLS conversion functions do not make any
    sense. The values returned should be:

      hue        := range [ 0, 360 ]
      luminance  := range [ 0,   1 ]
      saturation := range [ 0,   1 ]

    like the current fixed point API does. Returning a value in
    the [ 0, 255 ] range is completely useless

  - the clutter_color_equal() should be converted for its use inside
    a GHashTable; a clutter_color_hash() should be added as well

  - the second parameter of the clutter_color_shade() function should
    be the shading factor, not the result (another GDK-ism). this way
    the function call can be translated from this:

      color.shade(out result, factor)

    to the more natural:

      color.shade(factor, out result)

This somewhat large commit fixes all these issues and updates the
internal users of the API.
This commit is contained in:
Emmanuele Bassi 2009-02-28 16:16:09 +00:00
parent 8a1eca7287
commit 7d674db9d2
7 changed files with 376 additions and 343 deletions

View File

@ -27,7 +27,10 @@
* SECTION:clutter-color * SECTION:clutter-color
* @short_description: Color management and manipulation. * @short_description: Color management and manipulation.
* *
* #ClutterColor is a simple type for representing colors. * #ClutterColor is a simple type for representing colors in Clutter.
*
* A #ClutterColor is expressed as a 4-tuple of values ranging from
* zero to 255, one for each color channel plus one for the alpha.
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -44,117 +47,120 @@
/** /**
* clutter_color_add: * clutter_color_add:
* @src1: a #ClutterColor * @a: a #ClutterColor
* @src2: a #ClutterColor * @b: a #ClutterColor
* @dest: (out): return location for the result * @result: (out): return location for the result
* *
* Adds @src2 to @src1 and saves the resulting color * Adds @a to @b and saves the resulting color inside @result.
* inside @dest.
* *
* The alpha channel of @dest is as the maximum value * The alpha channel of @result is set as as the maximum value
* between the alpha channels of @src1 and @src2. * between the alpha channels of @a and @b.
*/ */
void void
clutter_color_add (const ClutterColor *src1, clutter_color_add (const ClutterColor *a,
const ClutterColor *src2, const ClutterColor *b,
ClutterColor *dest) ClutterColor *result)
{ {
g_return_if_fail (src1 != NULL); g_return_if_fail (a != NULL);
g_return_if_fail (src2 != NULL); g_return_if_fail (b != NULL);
g_return_if_fail (dest != NULL); g_return_if_fail (result != NULL);
dest->red = CLAMP (src1->red + src2->red, 0, 255); result->red = CLAMP (a->red + b->red, 0, 255);
dest->green = CLAMP (src1->green + src2->green, 0, 255); result->green = CLAMP (a->green + b->green, 0, 255);
dest->blue = CLAMP (src1->blue + src2->blue, 0, 255); result->blue = CLAMP (a->blue + b->blue, 0, 255);
dest->alpha = MAX (src1->alpha, src2->alpha); result->alpha = MAX (a->alpha, b->alpha);
} }
/** /**
* clutter_color_subtract: * clutter_color_subtract:
* @src1: a #ClutterColor * @a: a #ClutterColor
* @src2: a #ClutterColor * @b: a #ClutterColor
* @dest: (out): return location for the result * @result: (out): return location for the result
* *
* Subtracts @src2 from @src1 and saves the resulting * Subtracts @b from @a and saves the resulting color inside @result.
* color inside @dest. This function assumes that the components
* of @src1 are greater than the components of @src2; the result is,
* otherwise, undefined.
* *
* The alpha channel of @dest is set as the minimum value * This function assumes that the components of @a are greater than the
* between the alpha channels of @src1 and @src2. * components of @b; the result is, otherwise, undefined.
*
* The alpha channel of @result is set as the minimum value
* between the alpha channels of @a and @b.
*/ */
void void
clutter_color_subtract (const ClutterColor *src1, clutter_color_subtract (const ClutterColor *a,
const ClutterColor *src2, const ClutterColor *b,
ClutterColor *dest) ClutterColor *result)
{ {
g_return_if_fail (src1 != NULL); g_return_if_fail (a != NULL);
g_return_if_fail (src2 != NULL); g_return_if_fail (b != NULL);
g_return_if_fail (dest != NULL); g_return_if_fail (result != NULL);
dest->red = CLAMP (src1->red - src2->red, 0, 255); result->red = CLAMP (a->red - b->red, 0, 255);
dest->green = CLAMP (src1->green - src2->green, 0, 255); result->green = CLAMP (a->green - b->green, 0, 255);
dest->blue = CLAMP (src1->blue - src2->blue, 0, 255); result->blue = CLAMP (a->blue - b->blue, 0, 255);
dest->alpha = MIN (src1->alpha, src2->alpha); result->alpha = MIN (a->alpha, b->alpha);
} }
/** /**
* clutter_color_lighten: * clutter_color_lighten:
* @src: a #ClutterColor * @color: a #ClutterColor
* @dest: (out): return location for the lighter color * @result: (out): return location for the lighter color
* *
* Lightens @src by a fixed amount, and saves the changed * Lightens @color by a fixed amount, and saves the changed color
* color in @dest. * in @result.
*/ */
void void
clutter_color_lighten (const ClutterColor *src, clutter_color_lighten (const ClutterColor *color,
ClutterColor *dest) ClutterColor *result)
{ {
clutter_color_shadex (src, dest, CLUTTER_FLOAT_TO_FIXED (1.3)); clutter_color_shade (color, 1.3, result);
} }
/** /**
* clutter_color_darken: * clutter_color_darken:
* @src: a #ClutterColor * @color: a #ClutterColor
* @dest: (out): return location for the darker color * @result: (out): return location for the darker color
* *
* Darkens @src by a fixed amount, and saves the changed color * Darkens @color by a fixed amount, and saves the changed color
* in @dest. * in @result.
*/ */
void void
clutter_color_darken (const ClutterColor *src, clutter_color_darken (const ClutterColor *color,
ClutterColor *dest) ClutterColor *result)
{ {
clutter_color_shadex (src, dest, CLUTTER_FLOAT_TO_FIXED (0.7)); clutter_color_shade (color, 0.7, result);
} }
/** /*
* clutter_color_to_hlsx: * clutter_color_to_hlsx:
* @src: a #ClutterColor * @color: a #ClutterColor
* @hue: return location for the hue value or %NULL * @hue: return location for the hue value or %NULL
* @luminance: return location for the luminance value or %NULL * @luminance: return location for the luminance value or %NULL
* @saturation: return location for the saturation value or %NULL * @saturation: return location for the saturation value or %NULL
* *
* Converts @src to the HLS format. Returned hue is in degrees (0 .. 360), * Converts @color to the HLS format. Returned hue is in degrees (0 .. 360),
* luminance and saturation from interval <0 .. 1>. * luminance and saturation from interval <0 .. 1>.
*
* The implementation is in fixed point because we don't particularly
* care about precision. It can be moved to floating point at any later
* date.
*/ */
void static void
clutter_color_to_hlsx (const ClutterColor *src, clutter_color_to_hlsx (const ClutterColor *color,
ClutterFixed *hue, CoglFixed *hue,
ClutterFixed *luminance, CoglFixed *luminance,
ClutterFixed *saturation) CoglFixed *saturation)
{ {
ClutterFixed red, green, blue; CoglFixed red, green, blue;
ClutterFixed min, max, delta; CoglFixed min, max, delta;
ClutterFixed h, l, s; CoglFixed h, l, s;
g_return_if_fail (src != NULL); g_return_if_fail (color != NULL);
red = (float)(src->red) / 255; red = COGL_FIXED_FAST_DIV (color->red, COGL_FIXED_255);
green = (float)(src->green) / 255; green = COGL_FIXED_FAST_DIV (color->green, COGL_FIXED_255);
blue = (float)(src->blue) / 255; blue = COGL_FIXED_FAST_DIV (color->blue, COGL_FIXED_255);
if (red > green) if (red > green)
{ {
@ -187,31 +193,25 @@ clutter_color_to_hlsx (const ClutterColor *src,
if (max != min) if (max != min)
{ {
if (l <= 0.5) if (l <= COGL_FIXED_0_5)
s = CLUTTER_FIXED_DIV ((max - min), (max + min)); s = COGL_FIXED_DIV ((max - min), (max + min));
else else
s = CLUTTER_FIXED_DIV ((max - min), s = COGL_FIXED_DIV ((max - min),
((float)(2) - max - min)); (COGL_FIXED_FROM_INT (2) - max - min));
delta = max - min; delta = max - min;
if (red == max) if (red == max)
h = CLUTTER_FIXED_DIV ((green - blue), delta); h = COGL_FIXED_DIV ((green - blue), delta);
else if (green == max) else if (green == max)
{ h = COGL_FIXED_FROM_INT (2) + COGL_FIXED_DIV ((blue - red), delta);
h = (float)(2)
+ CLUTTER_FIXED_DIV ((blue - red), delta);
}
else if (blue == max) else if (blue == max)
{ h = COGL_FIXED_FROM_INT (4) + COGL_FIXED_DIV ((red - green), delta);
h = (float)(4)
+ CLUTTER_FIXED_DIV ((red - green), delta);
}
h *= 60; h *= 60;
if (h < 0) if (h < 0)
h += 360.0; h += COGL_FIXED_360;
} }
if (hue) if (hue)
@ -224,7 +224,7 @@ clutter_color_to_hlsx (const ClutterColor *src,
*saturation = s; *saturation = s;
} }
/** /*
* clutter_color_from_hlsx: * clutter_color_from_hlsx:
* @dest: (out): return location for a #ClutterColor * @dest: (out): return location for a #ClutterColor
* @hue: hue value (0 .. 360) * @hue: hue value (0 .. 360)
@ -234,336 +234,355 @@ clutter_color_to_hlsx (const ClutterColor *src,
* Converts a color expressed in HLS (hue, luminance and saturation) * Converts a color expressed in HLS (hue, luminance and saturation)
* values into a #ClutterColor. * values into a #ClutterColor.
*/ */
void void
clutter_color_from_hlsx (ClutterColor *dest, clutter_color_from_hlsx (ClutterColor *color,
ClutterFixed hue, CoglFixed hue,
ClutterFixed luminance, CoglFixed luminance,
ClutterFixed saturation) CoglFixed saturation)
{ {
ClutterFixed h, l, s; CoglFixed h, l, s;
ClutterFixed m1, m2; CoglFixed m1, m2;
g_return_if_fail (dest != NULL); g_return_if_fail (color != NULL);
l = luminance; l = luminance;
s = saturation; s = saturation;
if (l <= 0.5) if (l <= COGL_FIXED_0_5)
m2 = CLUTTER_FIXED_MUL (l, (1.0 + s)); m2 = COGL_FIXED_MUL (l, (COGL_FIXED_1 + s));
else else
m2 = l + s - CLUTTER_FIXED_MUL (l, s); m2 = l + s - COGL_FIXED_MUL (l, s);
m1 = 2 * l - m2; m1 = 2 * l - m2;
if (s == 0) if (s == 0)
{ {
dest->red = (guint8) (l * 255); color->red = (guint8) (COGL_FIXED_TO_INT (l) * 255);
dest->green = (guint8) (l * 255); color->green = (guint8) (COGL_FIXED_TO_INT (l) * 255);
dest->blue = (guint8) (l * 255); color->blue = (guint8) (COGL_FIXED_TO_INT (l) * 255);
} }
else else
{ {
h = hue + 120.0; h = hue + COGL_FIXED_120;
while (h > 360.0) while (h > COGL_FIXED_360)
h -= 360.0; h -= COGL_FIXED_360;
while (h < 0) while (h < 0)
h += 360.0; h += COGL_FIXED_360;
if (h < 60.0) if (h < COGL_FIXED_60)
{ {
float tmp; CoglFixed tmp;
tmp = (m1 + CLUTTER_FIXED_MUL ((m2 - m1), h) / 60); tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1), h, COGL_FIXED_60));
dest->red = (guint8) (tmp * 255); color->red = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
} }
else if (h < 180.0) else if (h < COGL_FIXED_180)
dest->red = (guint8) (m2 * 255); color->red = (guint8) (COGL_FIXED_TO_INT (m2) * 255);
else if (h < 240.0) else if (h < COGL_FIXED_240)
{ {
float tmp; CoglFixed tmp;
tmp = (m1 + CLUTTER_FIXED_MUL ((m2 - m1), (240.0 - h))) tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1),
/ 60; (COGL_FIXED_240 - h),
dest->red = (guint8) (tmp * 255); COGL_FIXED_60));
color->red = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
} }
else else
dest->red = (guint8) (m1 * 255); color->red = (guint8) (COGL_FIXED_TO_INT (m1) * 255);
h = hue; h = hue;
while (h > 360.0) while (h > COGL_FIXED_360)
h -= 360.0; h -= COGL_FIXED_360;
while (h < 0) while (h < 0)
h += 360.0; h += COGL_FIXED_360;
if (h < 60.0) if (h < COGL_FIXED_60)
{ {
float tmp; CoglFixed tmp;
tmp = (m1 + CLUTTER_FIXED_MUL ((m2 - m1), h) / 60); tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1), h, COGL_FIXED_60));
dest->green = (guint8) (tmp * 255); color->green = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
} }
else if (h < 180.0) else if (h < COGL_FIXED_180)
dest->green = (guint8) (m2 * 255); color->green = (guint8) (COGL_FIXED_TO_INT (m2) * 255);
else if (h < 240.0) else if (h < COGL_FIXED_240)
{ {
float tmp; CoglFixed tmp;
tmp = (m1 + CLUTTER_FIXED_MUL ((m2 - m1) , (240.0 - h))) tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1),
/ 60; (COGL_FIXED_240 - h),
dest->green = (guint8) (tmp * 255); COGL_FIXED_60));
color->green = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
} }
else else
dest->green = (guint8) (m1 * 255); color->green = (guint8) (COGL_FIXED_TO_INT (m1) * 255);
h = hue - 120.0; h = hue - COGL_FIXED_120;
while (h > 360.0) while (h > COGL_FIXED_360)
h -= 360.0; h -= COGL_FIXED_360;
while (h < 0) while (h < 0)
h += 360.0; h += COGL_FIXED_360;
if (h < 60.0) if (h < COGL_FIXED_60)
{ {
float tmp; CoglFixed tmp;
tmp = (m1 + CLUTTER_FIXED_MUL ((m2 - m1), h) / 60); tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1), h, COGL_FIXED_60));
dest->blue = (guint8) (tmp * 255); color->blue = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
} }
else if (h < 180.0) else if (h < COGL_FIXED_180)
dest->blue = (guint8) (m2 * 255); color->blue = (guint8) (COGL_FIXED_TO_INT (m2) * 255);
else if (h < 240.0) else if (h < COGL_FIXED_240)
{ {
float tmp; CoglFixed tmp;
tmp = (m1 + CLUTTER_FIXED_MUL ((m2 - m1), (240.0 - h))) tmp = (m1 + COGL_FIXED_MUL_DIV ((m2 - m1),
/ 60; (COGL_FIXED_240 - h),
dest->blue = (guint8) (tmp * 255); COGL_FIXED_60));
color->blue = (guint8) (COGL_FIXED_TO_INT (tmp) * 255);
} }
else else
dest->blue = (guint8) (m1 * 255); color->blue = (guint8) (COGL_FIXED_TO_INT (m1) * 255);
} }
} }
/** /**
* clutter_color_to_hls: * clutter_color_to_hls:
* @src: a #ClutterColor * @color: a #ClutterColor
* @hue: return location for the hue value or %NULL * @hue: return location for the hue value or %NULL
* @luminance: return location for the luminance value or %NULL * @luminance: return location for the luminance value or %NULL
* @saturation: return location for the saturation value or %NULL * @saturation: return location for the saturation value or %NULL
* *
* Converts @src to the HLS format. Returned HLS values are from interval * Converts @color to the HLS format.
* 0 .. 255. *
* The @hue value is in the 0 .. 360 range. The @luminance and
* @saturation values are in the 0 .. 1 range.
*/ */
void void
clutter_color_to_hls (const ClutterColor *src, clutter_color_to_hls (const ClutterColor *color,
guint8 *hue, gfloat *hue,
guint8 *luminance, gfloat *luminance,
guint8 *saturation) gfloat *saturation)
{ {
ClutterFixed h, l, s; CoglFixed h, l, s;
clutter_color_to_hlsx (src, &h, &l, &s); clutter_color_to_hlsx (color, &h, &l, &s);
if (hue) if (hue)
*hue = (guint8) (h * 255) / 360; *hue = COGL_FIXED_TO_FLOAT (h);
if (luminance) if (luminance)
*luminance = (guint8) (l * 255); *luminance = COGL_FIXED_TO_FLOAT (l);
if (saturation) if (saturation)
*saturation = (guint8) (s * 255); *saturation = COGL_FIXED_TO_FLOAT (s);
} }
/** /**
* clutter_color_from_hls: * clutter_color_from_hls:
* @dest: (out): return location for a #ClutterColor * @color: (out): return location for a #ClutterColor
* @hue: hue value (0 .. 255) * @hue: hue value, in the 0 .. 360 range
* @luminance: luminance value (0 .. 255) * @luminance: luminance value, in the 0 .. 1 range
* @saturation: saturation value (0 .. 255) * @saturation: saturation value, in the 0 .. 1 range
* *
* Converts a color expressed in HLS (hue, luminance and saturation) * Converts a color expressed in HLS (hue, luminance and saturation)
* values into a #ClutterColor. * values into a #ClutterColor.
*/ */
void void
clutter_color_from_hls (ClutterColor *dest, clutter_color_from_hls (ClutterColor *color,
guint8 hue, gfloat hue,
guint8 luminance, gfloat luminance,
guint8 saturation) gfloat saturation)
{ {
ClutterFixed h, l, s; CoglFixed h, l, s;
h = (float)(hue * 360) / 255; h = COGL_FIXED_FROM_FLOAT (hue);
l = (float)(luminance) / 255; l = COGL_FIXED_FROM_FLOAT (luminance);
s = (float)(saturation) / 255; s = COGL_FIXED_FROM_FLOAT (saturation);
clutter_color_from_hlsx (dest, h, l, s); clutter_color_from_hlsx (color, h, l, s);
}
/*
* clutter_color_shadex:
* @color: a #ClutterColor
* @factor: the shade factor to apply, as a fixed point value
* @result: (out): return location for the shaded color
*
* Shades @color by @factor and saves the modified color into @result.
*/
static void
clutter_color_shadex (const ClutterColor *color,
CoglFixed factor,
ClutterColor *result)
{
CoglFixed h, l, s;
g_return_if_fail (color != NULL);
g_return_if_fail (result != NULL);
clutter_color_to_hlsx (color, &h, &l, &s);
l = COGL_FIXED_MUL (l, factor);
if (l > COGL_FIXED_1)
l = COGL_FIXED_1;
else if (l < 0)
l = 0;
s = COGL_FIXED_MUL (s, factor);
if (s > COGL_FIXED_1)
s = COGL_FIXED_1;
else if (s < 0)
s = 0;
clutter_color_from_hlsx (result, h, l, s);
result->alpha = color->alpha;
} }
/** /**
* clutter_color_shade: * clutter_color_shade:
* @src: a #ClutterColor * @color: a #ClutterColor
* @dest: (out): return location for the shaded color * @factor: the shade factor to apply
* @shade: the shade factor to apply * @result: (out): return location for the shaded color
* *
* Shades @src by the factor of @shade and saves the modified * Shades @color by @factor and saves the modified color into @result.
* color into @dest.
*/ */
void void
clutter_color_shade (const ClutterColor *src, clutter_color_shade (const ClutterColor *color,
ClutterColor *dest, gdouble factor,
gdouble shade) ClutterColor *result)
{ {
clutter_color_shadex (src, dest, CLUTTER_FLOAT_TO_FIXED (shade)); clutter_color_shadex (color,
} COGL_FIXED_FROM_FLOAT (factor),
result);
/**
* clutter_color_shadex:
* @src: a #ClutterColor
* @dest: (out): return location for the shaded color
* @shade: #ClutterFixed the shade factor to apply
*
* Fixed point version of clutter_color_shade().
*
* Shades @src by the factor of @shade and saves the modified
* color into @dest.
*
* Since: 0.2
*/
void
clutter_color_shadex (const ClutterColor *src,
ClutterColor *dest,
ClutterFixed shade)
{
ClutterFixed h, l, s;
g_return_if_fail (src != NULL);
g_return_if_fail (dest != NULL);
clutter_color_to_hlsx (src, &h, &l, &s);
l = CLUTTER_FIXED_MUL (l, shade);
if (l > 1.0)
l = 1.0;
else if (l < 0)
l = 0;
s = CLUTTER_FIXED_MUL (s, shade);
if (s > 1.0)
s = 1.0;
else if (s < 0)
s = 0;
clutter_color_from_hlsx (dest, h, l, s);
dest->alpha = src->alpha;
} }
/** /**
* clutter_color_to_pixel: * clutter_color_to_pixel:
* @src: a #ClutterColor * @color: a #ClutterColor
* *
* Converts @src into a packed 32 bit integer, containing * Converts @color into a packed 32 bit integer, containing
* all the four 8 bit channels used by #ClutterColor. * all the four 8 bit channels used by #ClutterColor.
* *
* Return value: a packed color * Return value: a packed color
*/ */
guint32 guint32
clutter_color_to_pixel (const ClutterColor *src) clutter_color_to_pixel (const ClutterColor *color)
{ {
g_return_val_if_fail (src != NULL, 0); g_return_val_if_fail (color != NULL, 0);
return (src->alpha | src->blue << 8 | src->green << 16 | src->red << 24); return (color->alpha |
color->blue << 8 |
color->green << 16 |
color->red << 24);
} }
/** /**
* clutter_color_from_pixel: * clutter_color_from_pixel:
* @dest: (out): return location for a #ClutterColor * @color: (out): return location for a #ClutterColor
* @pixel: a 32 bit packed integer containing a color * @pixel: a 32 bit packed integer containing a color
* *
* Converts @pixel from the packed representation of a four 8 bit channel * Converts @pixel from the packed representation of a four 8 bit channel
* color to a #ClutterColor. * color to a #ClutterColor.
*/ */
void void
clutter_color_from_pixel (ClutterColor *dest, clutter_color_from_pixel (ClutterColor *color,
guint32 pixel) guint32 pixel)
{ {
g_return_if_fail (dest != NULL); g_return_if_fail (color != NULL);
dest->red = pixel >> 24; color->red = pixel >> 24;
dest->green = (pixel >> 16) & 0xff; color->green = (pixel >> 16) & 0xff;
dest->blue = (pixel >> 8) & 0xff; color->blue = (pixel >> 8) & 0xff;
dest->alpha = pixel & 0xff; color->alpha = pixel & 0xff;
} }
/** /**
* clutter_color_parse: * clutter_color_from_string:
* @color: a string specifiying a color (named color or #RRGGBBAA) * @color: (out): return location for a #ClutterColor
* @dest: (out): return location for a #ClutterColor * @str: a string specifiying a color (named color or #RRGGBBAA)
* *
* Parses a string definition of a color, filling the * Parses a string definition of a color, filling the
* <structfield>red</structfield>, <structfield>green</structfield>, * <structfield>red</structfield>, <structfield>green</structfield>,
* <structfield>blue</structfield> and <structfield>alpha</structfield> * <structfield>blue</structfield> and <structfield>alpha</structfield>
* channels of @dest. If alpha is not specified it will be set full opaque. * channels of @color. If alpha is not specified it will be set full opaque.
* The color in @dest is not allocated. *
* The @color is not allocated.
* *
* The color may be defined by any of the formats understood by * The color may be defined by any of the formats understood by
* <function>pango_color_parse</function>; these include literal color * pango_color_from_string(); these include literal color names, like
* names, like <literal>Red</literal> or <literal>DarkSlateGray</literal>, * <literal>Red</literal> or <literal>DarkSlateGray</literal>, or
* or hexadecimal specifications like <literal>&num;3050b2</literal> or * hexadecimal specifications like <literal>&num;3050b2</literal> or
* <literal>&num;333</literal>. * <literal>&num;333</literal>.
* *
* Return value: %TRUE if parsing succeeded. * Return value: %TRUE if parsing succeeded.
* *
* Since: 0.2 * Since: 1.0
*/ */
gboolean gboolean
clutter_color_parse (const gchar *color, clutter_color_from_string (ClutterColor *color,
ClutterColor *dest) const gchar *str)
{ {
PangoColor pango_color; PangoColor pango_color = { 0, };
g_return_val_if_fail (color != NULL, FALSE); g_return_val_if_fail (color != NULL, FALSE);
g_return_val_if_fail (dest != NULL, FALSE); g_return_val_if_fail (str != NULL, FALSE);
/* parse ourselves to get alpha */ /* if the string contains a color encoded using the hexadecimal
if (color[0] == '#') * notations (#rrggbbaa or #rrggbb) we attempt a rough pass at
* parsing the color ourselves, as we need the alpha channel that
* Pango can't retrieve.
*/
if (str[0] == '#')
{ {
gint32 result; gint32 result;
if (sscanf (color + 1, "%x", &result)) if (sscanf (str + 1, "%x", &result))
{ {
if (strlen (color) == 9) if (strlen (str) == 9)
{ {
dest->red = (result >> 24) & 0xff; /* #rrggbbaa */
dest->green = (result >> 16) & 0xff; color->red = (result >> 24) & 0xff;
dest->blue = (result >> 8) & 0xff; color->green = (result >> 16) & 0xff;
dest->alpha = result & 0xff; color->blue = (result >> 8) & 0xff;
color->alpha = result & 0xff;
return TRUE; return TRUE;
} }
else if (strlen (color) == 7) else if (strlen (str) == 7)
{ {
dest->red = (result >> 16) & 0xff; /* #rrggbb */
dest->green = (result >> 8) & 0xff; color->red = (result >> 16) & 0xff;
dest->blue = result & 0xff; color->green = (result >> 8) & 0xff;
dest->alpha = 0xff; color->blue = result & 0xff;
color->alpha = 0xff;
return TRUE; return TRUE;
} }
} }
/* XXX - should we return FALSE here? it's not like
* Pango is endowed with mystical parsing powers and
* will be able to do better than the code above.
* still, it doesn't hurt
*/
} }
/* Fall back to pango for named colors - note pango does not handle alpha */ /* Fall back to pango for named colors */
if (pango_color_parse (&pango_color, color)) if (pango_color_parse (&pango_color, str))
{ {
dest->red = pango_color.red; color->red = pango_color.red;
dest->green = pango_color.green; color->green = pango_color.green;
dest->blue = pango_color.blue; color->blue = pango_color.blue;
dest->alpha = 0xff;
color->alpha = 0xff;
return TRUE; return TRUE;
} }
@ -599,31 +618,58 @@ clutter_color_to_string (const ClutterColor *color)
/** /**
* clutter_color_equal: * clutter_color_equal:
* @a: a #ClutterColor * @v1: a #ClutterColor
* @b: a #ClutterColor * @v2: a #ClutterColor
* *
* Compares two #ClutterColor<!-- -->s and checks if they are the same. * Compares two #ClutterColor<!-- -->s and checks if they are the same.
* *
* This function can be passed to g_hash_table_new() as the @key_equal_func
* parameter, when using #ClutterColor<!-- -->s as keys in a #GHashTable.
*
* Return value: %TRUE if the two colors are the same. * Return value: %TRUE if the two colors are the same.
* *
* Since: 0.2 * Since: 0.2
*/ */
gboolean gboolean
clutter_color_equal (const ClutterColor *a, clutter_color_equal (gconstpointer v1,
const ClutterColor *b) gconstpointer v2)
{ {
g_return_val_if_fail (a != NULL, FALSE); const ClutterColor *a, *b;
g_return_val_if_fail (b != NULL, FALSE);
if (a == b) g_return_val_if_fail (v1 != NULL, FALSE);
g_return_val_if_fail (v2 != NULL, FALSE);
if (v1 == v2)
return TRUE; return TRUE;
return (a->red == b->red && a = v1;
b = v2;
return (a->red == b->red &&
a->green == b->green && a->green == b->green &&
a->blue == b->blue && a->blue == b->blue &&
a->alpha == b->alpha); a->alpha == b->alpha);
} }
/**
* clutter_color_hash:
* @v: a #ClutterColor
*
* Converts a #ClutterColor to a hash value.
*
* This function can be passed to g_hash_table_new() as the @hash_func
* parameter, when using #ClutterColor<!-- -->s as keys in a #GHashTable.
*
* Return value: a hash value corresponding to the color
*
* Since: 1.0
*/
guint
clutter_color_hash (gconstpointer v)
{
return clutter_color_to_pixel ((const ClutterColor *) v);
}
/** /**
* clutter_color_copy: * clutter_color_copy:
* @color: a #ClutterColor * @color: a #ClutterColor
@ -638,14 +684,10 @@ clutter_color_equal (const ClutterColor *a,
ClutterColor * ClutterColor *
clutter_color_copy (const ClutterColor *color) clutter_color_copy (const ClutterColor *color)
{ {
ClutterColor *result; if (G_LIKELY (color != NULL))
return g_slice_dup (ClutterColor, color);
g_return_val_if_fail (color != NULL, NULL);
result = g_slice_new (ClutterColor); return NULL;
*result = *color;
return result;
} }
/** /**
@ -659,7 +701,7 @@ clutter_color_copy (const ClutterColor *color)
void void
clutter_color_free (ClutterColor *color) clutter_color_free (ClutterColor *color)
{ {
if (G_LIKELY (color)) if (G_LIKELY (color != NULL))
g_slice_free (ClutterColor, color); g_slice_free (ClutterColor, color);
} }
@ -710,7 +752,7 @@ clutter_value_transform_string_color (const GValue *src,
{ {
ClutterColor color = { 0, }; ClutterColor color = { 0, };
clutter_color_parse (g_value_get_string (src), &color); clutter_color_from_string (&color, g_value_get_string (src));
clutter_value_set_color (dest, &color); clutter_value_set_color (dest, &color);
} }

View File

@ -3,9 +3,11 @@
* *
* An OpenGL based 'interactive canvas' library. * An OpenGL based 'interactive canvas' library.
* *
* Authored By Matthew Allum <mallum@openedhand.com> * Authored By: Matthew Allum <mallum@openedhand.com>
* Emmanuele Bassi <ebassi@linux.intel.com>
* *
* Copyright (C) 2006 OpenedHand * Copyright (C) 2006, 2007, 2008 OpenedHand
* Copyright (C) 2009 Intel Corp.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -48,6 +50,7 @@ typedef struct _ClutterColor ClutterColor;
*/ */
struct _ClutterColor struct _ClutterColor
{ {
/*< public >*/
guint8 red; guint8 red;
guint8 green; guint8 green;
guint8 blue; guint8 blue;
@ -55,59 +58,49 @@ struct _ClutterColor
guint8 alpha; guint8 alpha;
}; };
ClutterColor *clutter_color_new (guint8 red,
guint8 green,
guint8 blue,
guint8 alpha);
ClutterColor *clutter_color_copy (const ClutterColor *color);
void clutter_color_free (ClutterColor *color);
gboolean clutter_color_parse (const gchar *color,
ClutterColor *dest);
gboolean clutter_color_equal (const ClutterColor *a,
const ClutterColor *b);
gchar * clutter_color_to_string (const ClutterColor *color);
GType clutter_color_get_type (void) G_GNUC_CONST; GType clutter_color_get_type (void) G_GNUC_CONST;
ClutterColor *clutter_color_new (guint8 red,
guint8 green,
guint8 blue,
guint8 alpha);
ClutterColor *clutter_color_copy (const ClutterColor *color);
void clutter_color_free (ClutterColor *color);
void clutter_color_add (const ClutterColor *src1, void clutter_color_add (const ClutterColor *a,
const ClutterColor *src2, const ClutterColor *b,
ClutterColor *dest); ClutterColor *result);
void clutter_color_subtract (const ClutterColor *src1, void clutter_color_subtract (const ClutterColor *a,
const ClutterColor *src2, const ClutterColor *b,
ClutterColor *dest); ClutterColor *result);
void clutter_color_lighten (const ClutterColor *src, void clutter_color_lighten (const ClutterColor *color,
ClutterColor *dest); ClutterColor *result);
void clutter_color_darken (const ClutterColor *src, void clutter_color_darken (const ClutterColor *color,
ClutterColor *dest); ClutterColor *result);
void clutter_color_shade (const ClutterColor *src, void clutter_color_shade (const ClutterColor *color,
ClutterColor *dest, gdouble factor,
gdouble shade); ClutterColor *result);
void clutter_color_shadex (const ClutterColor *src,
ClutterColor *dest,
ClutterFixed shade);
void clutter_color_to_hlsx (const ClutterColor *src, gchar * clutter_color_to_string (const ClutterColor *color);
ClutterFixed *hue, gboolean clutter_color_from_string (ClutterColor *color,
ClutterFixed *luminance, const gchar *str);
ClutterFixed *saturation);
void clutter_color_from_hlsx (ClutterColor *dest,
ClutterFixed hue,
ClutterFixed luminance,
ClutterFixed saturation);
void clutter_color_to_hls (const ClutterColor *src, void clutter_color_to_hls (const ClutterColor *color,
guint8 *hue, gfloat *hue,
guint8 *luminance, gfloat *luminance,
guint8 *saturation); gfloat *saturation);
void clutter_color_from_hls (ClutterColor *dest, void clutter_color_from_hls (ClutterColor *color,
guint8 hue, gfloat hue,
guint8 luminance, gfloat luminance,
guint8 saturation); gfloat saturation);
guint32 clutter_color_to_pixel (const ClutterColor *src); guint32 clutter_color_to_pixel (const ClutterColor *color);
void clutter_color_from_pixel (ClutterColor *dest, void clutter_color_from_pixel (ClutterColor *color,
guint32 pixel); guint32 pixel);
guint clutter_color_hash (gconstpointer v);
gboolean clutter_color_equal (gconstpointer v1,
gconstpointer v2);
#define CLUTTER_TYPE_PARAM_COLOR (clutter_param_color_get_type ()) #define CLUTTER_TYPE_PARAM_COLOR (clutter_param_color_get_type ())
#define CLUTTER_PARAM_SPEC_COLOR(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), CLUTTER_TYPE_PARAM_COLOR, ClutterParamSpecColor)) #define CLUTTER_PARAM_SPEC_COLOR(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), CLUTTER_TYPE_PARAM_COLOR, ClutterParamSpecColor))

View File

@ -57,4 +57,6 @@
#define clutter_set_use_mipmapped_text clutter_actor_set_use_mipmapped_text_REPLACED_BY_clutter_set_font_flags #define clutter_set_use_mipmapped_text clutter_actor_set_use_mipmapped_text_REPLACED_BY_clutter_set_font_flags
#define clutter_get_use_mipmapped_text clutter_actor_get_use_mipmapped_text_REPLACED_BY_clutter_get_font_flags #define clutter_get_use_mipmapped_text clutter_actor_get_use_mipmapped_text_REPLACED_BY_clutter_get_font_flags
#define clutter_color_parse clutter_color_parse_REPLACED_BY_clutter_color_from_string
#endif /* CLUTTER_DEPRECATED_H */ #endif /* CLUTTER_DEPRECATED_H */

View File

@ -1052,7 +1052,7 @@ clutter_script_parse_node (ClutterScript *script,
ClutterColor color = { 0, }; ClutterColor color = { 0, };
if (str && str[0] != '\0') if (str && str[0] != '\0')
clutter_color_parse (str, &color); clutter_color_from_string (&color, str);
g_value_set_boxed (value, &color); g_value_set_boxed (value, &color);
retval = TRUE; retval = TRUE;

View File

@ -938,27 +938,23 @@ ClutterColor
clutter_color_new clutter_color_new
clutter_color_copy clutter_color_copy
clutter_color_free clutter_color_free
clutter_color_equal
clutter_color_hash
<SUBSECTION> <SUBSECTION>
clutter_color_parse clutter_color_from_string
clutter_color_to_string
clutter_color_from_hls clutter_color_from_hls
clutter_color_from_hlsx clutter_color_to_hls
clutter_color_from_pixel clutter_color_from_pixel
clutter_color_to_pixel
<SUBSECTION> <SUBSECTION>
clutter_color_add clutter_color_add
clutter_color_subtract clutter_color_subtract
clutter_color_equal
clutter_color_lighten clutter_color_lighten
clutter_color_darken clutter_color_darken
clutter_color_shade clutter_color_shade
clutter_color_shadex
<SUBSECTION>
clutter_color_to_hls
clutter_color_to_hlsx
clutter_color_to_pixel
clutter_color_to_string
<SUBSECTION> <SUBSECTION>
ClutterParamSpecColor ClutterParamSpecColor

View File

@ -156,7 +156,7 @@ test_behave_main (int argc, char *argv[])
clutter_rectangle_set_color (CLUTTER_RECTANGLE (rect), clutter_rectangle_set_color (CLUTTER_RECTANGLE (rect),
&rect_bg_color); &rect_bg_color);
clutter_rectangle_set_border_width (CLUTTER_RECTANGLE (rect), 10); clutter_rectangle_set_border_width (CLUTTER_RECTANGLE (rect), 10);
clutter_color_parse ("DarkSlateGray", &rect_border_color); clutter_color_from_string (&rect_border_color, "DarkSlateGray");
clutter_rectangle_set_border_color (CLUTTER_RECTANGLE (rect), clutter_rectangle_set_border_color (CLUTTER_RECTANGLE (rect),
&rect_border_color); &rect_border_color);
clutter_actor_show (rect); clutter_actor_show (rect);

View File

@ -756,7 +756,7 @@ test_layout_main (int argc, char *argv[])
stage = clutter_stage_get_default (); stage = clutter_stage_get_default ();
clutter_actor_set_size (stage, 800, 600); clutter_actor_set_size (stage, 800, 600);
clutter_color_parse ("Red", &bg_color); clutter_color_from_string (&bg_color, "Red");
main_timeline = clutter_timeline_new_for_duration (2000); main_timeline = clutter_timeline_new_for_duration (2000);
clutter_timeline_set_loop (main_timeline, TRUE); clutter_timeline_set_loop (main_timeline, TRUE);