Commit b8643078 authored by Benjamin Otte's avatar Benjamin Otte

css: Add support for parsing rgba() colors

This also requires adding alpha support to the solid color paint server.

https://bugzilla.gnome.org/show_bug.cgi?id=639750
parent 1e22da8d
......@@ -162,20 +162,18 @@ _set_source_rsvg_solid_colour (RsvgDrawingCtx * ctx,
{
RsvgCairoRender *render = RSVG_CAIRO_RENDER (ctx->render);
cairo_t *cr = render->cr;
guint32 rgb = colour->rgb;
double r, g, b;
guint32 argb = colour->argb;
double r, g, b, a;
if (colour->currentcolour)
rgb = current_colour;
argb = current_colour;
r = ((rgb >> 16) & 0xff) / 255.0;
g = ((rgb >> 8) & 0xff) / 255.0;
b = ((rgb >> 0) & 0xff) / 255.0;
r = ((argb >> 16) & 0xff) / 255.0;
g = ((argb >> 8) & 0xff) / 255.0;
b = ((argb >> 0) & 0xff) / 255.0;
a = (argb >> 24) / 255.0 * (opacity / 255.0);
if (opacity == 0xff)
cairo_set_source_rgb (cr, r, g, b);
else
cairo_set_source_rgba (cr, r, g, b, opacity / 255.0);
cairo_set_source_rgba (cr, r, g, b, a);
}
static void
......
......@@ -304,7 +304,8 @@ rsvg_css_clip_rgb (gint rgb)
}
/* pack 3 [0,255] ints into one 32 bit one */
#define PACK_RGB(r,g,b) (((r) << 16) | ((g) << 8) | (b))
#define PACK_RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
#define PACK_RGB(r,g,b) PACK_RGBA(r, g, b, 255)
/**
* rsvg_css_parse_color:
......@@ -343,6 +344,46 @@ rsvg_css_parse_color (const char *str, gboolean * inherit)
}
}
/* i want to use g_str_has_prefix but it isn't in my gstrfuncs.h?? */
else if (strstr (str, "rgba") != NULL) {
gint r, g, b, a;
r = g = b = 0;
a = 255;
if (strstr (str, "%") != 0) {
guint i, nb_toks;
char **toks;
/* assume rgba (9%, 100%, 23%, 100%) */
for (i = 0; str[i] != '('; i++);
i++;
toks = rsvg_css_parse_list (str + i, &nb_toks);
if (toks) {
if (nb_toks == 4) {
r = rsvg_css_clip_rgb_percent (g_ascii_strtod (toks[0], NULL));
g = rsvg_css_clip_rgb_percent (g_ascii_strtod (toks[1], NULL));
b = rsvg_css_clip_rgb_percent (g_ascii_strtod (toks[2], NULL));
a = rsvg_css_clip_rgb_percent (g_ascii_strtod (toks[3], NULL));
}
g_strfreev (toks);
}
} else {
float alpha;
/* assume "rgb (r, g, b)" */
if (4 == sscanf (str, " rgba ( %d , %d , %d , %g ) ", &r, &g, &b, &alpha)) {
r = rsvg_css_clip_rgb (r);
g = rsvg_css_clip_rgb (g);
b = rsvg_css_clip_rgb (b);
a = CLAMP (alpha * 255, 0, 255);
}
}
val = PACK_RGBA (r, g, b, a);
}
else if (strstr (str, "rgb") != NULL) {
gint r, g, b;
r = g = b = 0;
......@@ -396,6 +437,7 @@ rsvg_css_parse_color (const char *str, gboolean * inherit)
}
#undef PACK_RGB
#undef PACK_RGBA
guint
rsvg_css_parse_opacity (const char *str)
......
......@@ -37,14 +37,14 @@
#include "rsvg-css.h"
static RsvgPaintServer *
rsvg_paint_server_solid (guint32 rgb)
rsvg_paint_server_solid (guint32 argb)
{
RsvgPaintServer *result = g_new (RsvgPaintServer, 1);
result->refcnt = 1;
result->type = RSVG_PAINT_SERVER_SOLID;
result->core.colour = g_new (RsvgSolidColour, 1);
result->core.colour->rgb = rgb;
result->core.colour->argb = argb;
result->core.colour->currentcolour = FALSE;
return result;
......@@ -115,7 +115,7 @@ rsvg_paint_server_parse (gboolean * inherit, const RsvgDefs * defs, const char *
guint32 current_color)
{
char *name;
guint32 rgb;
guint32 argb;
if (inherit != NULL)
*inherit = 1;
if (str == NULL || !strcmp (str, "none"))
......@@ -146,8 +146,8 @@ rsvg_paint_server_parse (gboolean * inherit, const RsvgDefs * defs, const char *
ps = rsvg_paint_server_solid_current_colour ();
return ps;
} else {
rgb = rsvg_css_parse_color (str, inherit);
return rsvg_paint_server_solid (rgb);
argb = rsvg_css_parse_color (str, inherit);
return rsvg_paint_server_solid (argb);
}
}
......
......@@ -109,7 +109,7 @@ struct _RsvgPattern {
struct _RsvgSolidColour {
gboolean currentcolour;
guint32 rgb;
guint32 argb;
};
typedef struct _RsvgSolidColour RsvgPaintServerColour;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment