rsvg_css_parse_color (const char *str, gboolean * inherit)
{
    gint val = 0;

    SETINHERIT ();

    if (str[0] == '#') {
        int i;
        for (i = 1; str[i]; i++) {
            int hexval;
            if (str[i] >= '0' && str[i] <= '9')
                hexval = str[i] - '0';
            else if (str[i] >= 'A' && str[i] <= 'F')
                hexval = str[i] - 'A' + 10;
            else if (str[i] >= 'a' && str[i] <= 'f')
                hexval = str[i] - 'a' + 10;
            else
                break;
            val = (val << 4) + hexval;
        }
        /* handle #rgb case */
        if (i == 4) {
            val = ((val & 0xf00) << 8) | ((val & 0x0f0) << 4) | (val & 0x00f);
            val |= val << 4;
        }
    }
    /* i want to use g_str_has_prefix but it isn't in my gstrfuncs.h?? */
    else if (strstr (str, "rgb") != NULL) {
        gint r, g, b;
        r = g = b = 0;

        if (strstr (str, "%") != 0) {
            guint i, nb_toks;
            char **toks;

            /* assume rgb (9%, 100%, 23%) */
            for (i = 0; str[i] != '('; i++);

            i++;

            toks = rsvg_css_parse_list (str + i, &nb_toks);

            if (toks) {
                if (nb_toks == 3) {
                    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));
                }

                g_strfreev (toks);
            }
        } else {
            /* assume "rgb (r, g, b)" */
            if (3 == sscanf (str, " rgb ( %d , %d , %d ) ", &r, &g, &b)) {
                r = rsvg_css_clip_rgb (r);
                g = rsvg_css_clip_rgb (g);
                b = rsvg_css_clip_rgb (b);
            } else
                r = g = b = 0;
        }

        val = PACK_RGB (r, g, b);
    } else if (!strcmp (str, "inherit"))
        UNSETINHERIT ();
    else {
        CRRgb rgb;

        if (cr_rgb_set_from_name (&rgb, (const guchar *) str) == CR_OK) {
            val = PACK_RGB (rgb.red, rgb.green, rgb.blue);
        } else {
            /* default to black on failed lookup */
            UNSETINHERIT ();
            val = 0;
        }
    }

    return val;
}
rsvg_css_parse_color (const char *str, gboolean * inherit)
{
    gint val = 0;

    SETINHERIT ();

    if (str[0] == '#') {
        int i;
        for (i = 1; str[i]; i++) {
            int hexval;
            if (str[i] >= '0' && str[i] <= '9')
                hexval = str[i] - '0';
            else if (str[i] >= 'A' && str[i] <= 'F')
                hexval = str[i] - 'A' + 10;
            else if (str[i] >= 'a' && str[i] <= 'f')
                hexval = str[i] - 'a' + 10;
            else
                break;
            val = (val << 4) + hexval;
        }
         
        if (i == 4) {
            val = ((val & 0xf00) << 8) | ((val & 0x0f0) << 4) | (val & 0x00f);
            val |= val << 4;
        }
    }
     
    else if (strstr (str, "rgb") != NULL) {
        gint r, g, b;
        r = g = b = 0;

        if (strstr (str, "%") != 0) {
            guint i, nb_toks;
            char **toks;

             
            for (i = 0; str[i] != '('; i++);

            i++;

            toks = rsvg_css_parse_list (str + i, &nb_toks);

            if (toks) {
                if (nb_toks == 3) {
                    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));
                }

                g_strfreev (toks);
            }
        } else {
             
            if (3 == sscanf (str, " rgb ( %d , %d , %d ) ", &r, &g, &b)) {
                r = rsvg_css_clip_rgb (r);
                g = rsvg_css_clip_rgb (g);
                b = rsvg_css_clip_rgb (b);
            } else
                r = g = b = 0;
        }

        val = PACK_RGB (r, g, b);
    } else if (!strcmp (str, "inherit"))
        UNSETINHERIT ();
    else {
        CRRgb rgb;

        if (cr_rgb_set_from_name (&rgb, (const guchar *) str) == CR_OK) {
            val = PACK_RGB (rgb.red, rgb.green, rgb.blue);
        } else {
             
            UNSETINHERIT ();
            val = 0;
        }
    }

    return val;
}