diff options
author | Matthias Clasen <mclasen@redhat.com> | 2015-08-14 22:51:46 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2015-08-14 22:58:15 -0400 |
commit | 194f4668b32a86993d969bccfed59186c08f63dd (patch) | |
tree | 1421677f952943123386feaad0dd0a52c2d88973 /pango/pango-color.c | |
parent | c9809629caf8ceda767b81b1aacfb045e3514119 (diff) | |
download | pango-194f4668b32a86993d969bccfed59186c08f63dd.tar.gz |
Allow rgba colors in markup
For foreground and background colors, allow #rrggbbaa to specify
a color with alpha. This will be returned as two attributes, for
color and alpha. underline_color and strikethrough_color are still
limited to #rrggbb, with no alpha component.
Diffstat (limited to 'pango/pango-color.c')
-rw-r--r-- | pango/pango-color.c | 98 |
1 files changed, 72 insertions, 26 deletions
diff --git a/pango/pango-color.c b/pango/pango-color.c index 47a98236..2f344765 100644 --- a/pango/pango-color.c +++ b/pango/pango-color.c @@ -205,45 +205,55 @@ hex (const char *spec, return TRUE; } -/** - * pango_color_parse: - * @color: (nullable): a #PangoColor structure in which to store the - * result, or %NULL - * @spec: a string specifying the new color - * - * Fill in the fields of a color from a string specification. The - * string can either one of a large set of standard names. (Taken - * from the CSS <ulink url="http://dev.w3.org/csswg/css-color/#named-colors">specification</ulink>), or it can be a hexadecimal - * value in the - * form '#rgb' '#rrggbb' '#rrrgggbbb' or '#rrrrggggbbbb' where - * 'r', 'g' and 'b' are hex digits of the red, green, and blue - * components of the color, respectively. (White in the four - * forms is '#fff' '#ffffff' '#fffffffff' and '#ffffffffffff') - * - * Return value: %TRUE if parsing of the specifier succeeded, - * otherwise false. - **/ + +/* Like pango_color_parse, but allow strings of the form + * '#rgba', '#rrggbbaa', '#rrrrggggbbbbaaaa', + * if alpha is not NULL. If no alpha component is found + * in the string, *alpha is set to 0. + */ gboolean -pango_color_parse (PangoColor *color, - const char *spec) +_pango_color_parse_with_alpha (PangoColor *color, + guint16 *alpha, + const char *spec) { g_return_val_if_fail (spec != NULL, FALSE); + if (alpha) + *alpha = 0; + if (spec[0] == '#') { size_t len; - unsigned int r, g, b; + unsigned int r, g, b, a; + gboolean has_alpha; spec++; len = strlen (spec); - if (len % 3 || len < 3 || len > 12) - return FALSE; - - len /= 3; + switch (len) + { + case 3: + case 6: + case 9: + case 12: + len /= 3; + has_alpha = FALSE; + break; + case 4: + case 8: + case 16: + if (!alpha) + return FALSE; + len /= 4; + has_alpha = TRUE; + break; + default: + return FALSE; + } if (!hex (spec, len, &r) || !hex (spec + len, len, &g) || - !hex (spec + len * 2, len, &b)) + !hex (spec + len * 2, len, &b) || + (has_alpha && !hex (spec + len * 3, len, &a))) return FALSE; if (color) @@ -263,6 +273,18 @@ pango_color_parse (PangoColor *color, color->green = g; color->blue = b; } + + if (alpha && has_alpha) + { + int bits = len * 4; + a <<= 16 - bits; + while (bits < 16) + { + a |= (a >> bits); + bits *= 2; + } + *alpha = a; + } } else { @@ -271,3 +293,27 @@ pango_color_parse (PangoColor *color, } return TRUE; } +/** + * pango_color_parse: + * @color: (nullable): a #PangoColor structure in which to store the + * result, or %NULL + * @spec: a string specifying the new color + * + * Fill in the fields of a color from a string specification. The + * string can either one of a large set of standard names. (Taken + * from the CSS <ulink url="http://dev.w3.org/csswg/css-color/#named-colors">specification</ulink>), or it can be a hexadecimal + * value in the + * form '#rgb' '#rrggbb' '#rrrgggbbb' or '#rrrrggggbbbb' where + * 'r', 'g' and 'b' are hex digits of the red, green, and blue + * components of the color, respectively. (White in the four + * forms is '#fff' '#ffffff' '#fffffffff' and '#ffffffffffff') + * + * Return value: %TRUE if parsing of the specifier succeeded, + * otherwise false. + **/ +gboolean +pango_color_parse (PangoColor *color, + const char *spec) +{ + return _pango_color_parse_with_alpha (color, NULL, spec); +} |