* Return value: a pointer to a static color; the returned pointer
* is owned by Clutter and it should never be modified or freed
*
- * Since: 1.4
+ * Since: 1.6
*/
-G_CONST_RETURN ClutterColor *
+const ClutterColor *
clutter_color_get_static (ClutterStaticColor color)
{
g_return_val_if_fail (color >= CLUTTER_COLOR_WHITE &&
color->alpha = pixel & 0xff;
}
+static inline void
+skip_whitespace (gchar **str)
+{
+ while (g_ascii_isspace (**str))
+ *str += 1;
+}
+
+static inline void
+parse_rgb_value (gchar *str,
+ guint8 *color,
+ gchar **endp)
+{
+ gdouble number;
+ gchar *p;
+
+ skip_whitespace (&str);
+
+ number = g_ascii_strtod (str, endp);
+
+ p = *endp;
+
+ skip_whitespace (&p);
+
+ if (*p == '%')
+ {
+ *endp = (gchar *) (p + 1);
+
+ *color = CLAMP (number / 100.0, 0.0, 1.0) * 255;
+ }
+ else
+ *color = CLAMP (number, 0, 255);
+}
+
+static gboolean
+parse_rgba (ClutterColor *color,
+ gchar *str,
+ gboolean has_alpha)
+{
+ skip_whitespace (&str);
+
+ if (*str != '(')
+ return FALSE;
+
+ str += 1;
+
+ /* red */
+ parse_rgb_value (str, &color->red, &str);
+ skip_whitespace (&str);
+ if (*str != ',')
+ return FALSE;
+
+ str += 1;
+
+ /* green */
+ parse_rgb_value (str, &color->green, &str);
+ skip_whitespace (&str);
+ if (*str != ',')
+ return FALSE;
+
+ str += 1;
+
+ /* blue */
+ parse_rgb_value (str, &color->blue, &str);
+ skip_whitespace (&str);
+
+ /* alpha (optional); since the alpha channel value can only
+ * be between 0 and 1 we don't use the parse_rgb_value()
+ * function
+ */
+ if (has_alpha)
+ {
+ gdouble number;
+
+ if (*str != ',')
+ return FALSE;
+
+ str += 1;
+
+ skip_whitespace (&str);
+ number = g_ascii_strtod (str, &str);
+
+ color->alpha = CLAMP (number * 255.0, 0, 255);
+ }
+ else
+ color->alpha = 255;
+
+ skip_whitespace (&str);
+ if (*str != ')')
+ return FALSE;
+
+ return TRUE;
+}
+
+static gboolean
+parse_hsla (ClutterColor *color,
+ gchar *str,
+ gboolean has_alpha)
+{
+ gdouble number;
+ gdouble h, l, s;
+
+ skip_whitespace (&str);
+
+ if (*str != '(')
+ return FALSE;
+
+ str += 1;
+
+ /* hue */
+ skip_whitespace (&str);
+ /* we don't do any angle normalization here because
+ * clutter_color_from_hls() will do it for us
+ */
+ number = g_ascii_strtod (str, &str);
+ skip_whitespace (&str);
+ if (*str != ',')
+ return FALSE;
+
+ h = number;
+
+ str += 1;
+
+ /* saturation */
+ skip_whitespace (&str);
+ number = g_ascii_strtod (str, &str);
+ skip_whitespace (&str);
+ if (*str != '%')
+ return FALSE;
+
+ str += 1;
+
+ s = CLAMP (number / 100.0, 0.0, 1.0);
+ skip_whitespace (&str);
+ if (*str != ',')
+ return FALSE;
+
+ str += 1;
+
+ /* luminance */
+ skip_whitespace (&str);
+ number = g_ascii_strtod (str, &str);
+ skip_whitespace (&str);
+ if (*str != '%')
+ return FALSE;
+
+ str += 1;
+
+ l = CLAMP (number / 100.0, 0.0, 1.0);
+ skip_whitespace (&str);
+
+ /* alpha (optional); since the alpha channel value can only
+ * be between 0 and 1 we don't use the parse_rgb_value()
+ * function
+ */
+ if (has_alpha)
+ {
+ if (*str != ',')
+ return FALSE;
+
+ str += 1;
+
+ skip_whitespace (&str);
+ number = g_ascii_strtod (str, &str);
+
+ color->alpha = CLAMP (number * 255.0, 0, 255);
+ }
+ else
+ color->alpha = 255;
+
+ skip_whitespace (&str);
+ if (*str != ')')
+ return FALSE;
+
+ clutter_color_from_hls (color, h, l, s);
+
+ return TRUE;
+}
+
/**
* clutter_color_from_string:
* @color: (out caller-allocates): return location for a #ClutterColor
- * @str: a string specifiying a color (named color or #RRGGBBAA)
+ * @str: a string specifiying a color
*
* Parses a string definition of a color, filling the
* <structfield>red</structfield>, <structfield>green</structfield>,
* <structfield>blue</structfield> and <structfield>alpha</structfield>
- * channels of @color. If alpha is not specified it will be set full opaque.
+ * channels of @color.
*
* The @color is not allocated.
*
- * The color may be defined by any of the formats understood by
- * pango_color_from_string(); these include literal color names, like
- * <literal>Red</literal> or <literal>DarkSlateGray</literal>, or
- * hexadecimal specifications like <literal>#3050b2</literal> or
- * <literal>#333</literal>.
+ * The format of @str can be either one of:
+ *
+ * <itemizedlist>
+ * <listitem>
+ * <para>a standard name (as taken from the X11 rgb.txt file)</para>
+ * </listitem>
+ * <listitem>
+ * <para>an hexadecimal value in the form: <literal>#rgb</literal>,
+ * <literal>#rrggbb</literal>, <literal>#rgba</literal> or
+ * <literal>#rrggbbaa</literal></para>
+ * </listitem>
+ * <listitem>
+ * <para>a RGB color in the form: <literal>rgb(r, g, b)</literal></para>
+ * </listitem>
+ * <listitem>
+ * <para>a RGB color in the form: <literal>rgba(r, g, b, a)</literal></para>
+ * </listitem>
+ * <listitem>
+ * <para>a HSL color in the form: <literal>hsl(h, s, l)</literal></para>
+ * </listitem>
+ * <listitem>
+ * <para>a HSL color in the form: <literal>hsla(h, s, l, a)</literal></para>
+ * </listitem>
+ * </itemizedlist>
+ *
+ * where 'r', 'g', 'b' and 'a' are (respectively) the red, green, blue color
+ * intensities and the opacity. The 'h', 's' and 'l' are (respectively) the
+ * hue, saturation and luminance values.
+ *
+ * In the rgb() and rgba() formats, the 'r', 'g', and 'b' values are either
+ * integers between 0 and 255, or percentage values in the range between 0%
+ * and 100%; the percentages require the '%' character. The 'a' value, if
+ * specified, can only be a floating point value between 0.0 and 1.0.
+ *
+ * In the hls() and hlsa() formats, the 'h' value (hue) it's an angle between
+ * 0 and 360.0 degrees; the 'l' and 's' values (luminance and saturation) are
+ * a floating point value between 0.0 and 1.0. The 'a' value, if specified,
+ * can only be a floating point value between 0.0 and 1.0.
*
- * Return value: %TRUE if parsing succeeded.
+ * Whitespace inside the definitions is ignored; no leading whitespace
+ * is allowed.
+ *
+ * If the alpha component is not specified then it is assumed to be set to
+ * be fully opaque.
+ *
+ * Return value: %TRUE if parsing succeeded, and %FALSE otherwise
*
* Since: 1.0
*/
g_return_val_if_fail (color != NULL, FALSE);
g_return_val_if_fail (str != NULL, FALSE);
+ if (strncmp (str, "rgb", 3) == 0)
+ {
+ gchar *s = (gchar *) str;
+ gboolean res;
+
+ if (strncmp (str, "rgba", 4) == 0)
+ res = parse_rgba (color, s + 4, TRUE);
+ else
+ res = parse_rgba (color, s + 3, FALSE);
+
+ return res;
+ }
+
+ if (strncmp (str, "hsl", 3) == 0)
+ {
+ gchar *s = (gchar *) str;
+ gboolean res;
+
+ if (strncmp (str, "hsla", 4) == 0)
+ res = parse_hsla (color, s + 4, TRUE);
+ else
+ res = parse_hsla (color, s + 3, FALSE);
+
+ return res;
+ }
+
/* if the string contains a color encoded using the hexadecimal
* notations (#rrggbbaa or #rgba) we attempt a rough pass at
* parsing the color ourselves, as we need the alpha channel that
* Pango can't retrieve.
*/
- if (str[0] == '#')
+ if (str[0] == '#' && str[1] != '\0')
{
+ gsize length = strlen (str + 1);
gint32 result;
- if (sscanf (str + 1, "%x", &result))
+ if (sscanf (str + 1, "%x", &result) == 1)
{
- gsize length = strlen (str);
-
switch (length)
{
- case 9: /* rrggbbaa */
+ case 8: /* rrggbbaa */
color->red = (result >> 24) & 0xff;
color->green = (result >> 16) & 0xff;
color->blue = (result >> 8) & 0xff;
return TRUE;
- case 7: /* #rrggbb */
+ case 6: /* #rrggbb */
color->red = (result >> 16) & 0xff;
color->green = (result >> 8) & 0xff;
color->blue = result & 0xff;
return TRUE;
- case 5: /* #rgba */
+ case 4: /* #rgba */
color->red = ((result >> 12) & 0xf);
color->green = ((result >> 8) & 0xf);
color->blue = ((result >> 4) & 0xf);
return TRUE;
- case 4: /* #rgb */
+ case 3: /* #rgb */
color->red = ((result >> 8) & 0xf);
color->green = ((result >> 4) & 0xf);
color->blue = result & 0xf;
return TRUE;
default:
- /* pass through to Pango */
- break;
+ return FALSE;
}
}
}
-
- /* Fall back to pango for named colors */
+
+ /* fall back to pango for X11-style named colors; see:
+ *
+ * http://en.wikipedia.org/wiki/X11_color_names
+ *
+ * for a list. at some point we might even ship with our own list generated
+ * from X11/rgb.txt, like we generate the key symbols.
+ */
if (pango_color_parse (&pango_color, str))
{
color->red = pango_color.red;
* Returns a textual specification of @color in the hexadecimal form
* <literal>#rrggbbaa</literal>, where <literal>r</literal>,
* <literal>g</literal>, <literal>b</literal> and <literal>a</literal> are
- * hex digits representing the red, green, blue and alpha components
+ * hexadecimal digits representing the red, green, blue and alpha components
* respectively.
*
* Return value: (transfer full): a newly-allocated text string
*
* Creates a new #ClutterColor with the given values.
*
+ * This function is the equivalent of:
+ *
+ * |[
+ * clutter_color_init (clutter_color_alloc (), red, green, blue, alpha);
+ * ]|
+ *
* Return value: (transfer full): the newly allocated color.
* Use clutter_color_free() when done
*
guint8 blue,
guint8 alpha)
{
- ClutterColor *color;
+ return clutter_color_init (clutter_color_alloc (),
+ red,
+ green,
+ blue,
+ alpha);
+}
- color = g_slice_new (ClutterColor);
+/**
+ * clutter_color_alloc:
+ *
+ * Allocates a new, transparent black #ClutterColor.
+ *
+ * Return value: (transfer full): the newly allocated #ClutterColor; use
+ * clutter_color_free() to free its resources
+ *
+ * Since: 1.12
+ */
+ClutterColor *
+clutter_color_alloc (void)
+{
+ return g_slice_new0 (ClutterColor);
+}
+
+/**
+ * clutter_color_init:
+ * @color: a #ClutterColor
+ * @red: red component of the color, between 0 and 255
+ * @green: green component of the color, between 0 and 255
+ * @blue: blue component of the color, between 0 and 255
+ * @alpha: alpha component of the color, between 0 and 255
+ *
+ * Initializes @color with the given values.
+ *
+ * Return value: (transfer none): the initialized #ClutterColor
+ *
+ * Since: 1.12
+ */
+ClutterColor *
+clutter_color_init (ClutterColor *color,
+ guint8 red,
+ guint8 green,
+ guint8 blue,
+ guint8 alpha)
+{
+ g_return_val_if_fail (color != NULL, NULL);
- color->red = red;
+ color->red = red;
color->green = green;
- color->blue = blue;
+ color->blue = blue;
color->alpha = alpha;
return color;
*
* Since: 0.8.4
*/
-G_CONST_RETURN ClutterColor *
+const ClutterColor *
clutter_value_get_color (const GValue *value)
{
g_return_val_if_fail (CLUTTER_VALUE_HOLDS_COLOR (value), NULL);