22import re
33import collections
44
5+ from ._compat import basestring
56from .parser import parse_one_component_value
67
78
@@ -16,42 +17,27 @@ class RGBA(collections.namedtuple('RGBA', ['red', 'green', 'blue', 'alpha'])):
1617 type = 'rgba'
1718
1819
19- def parse_color_string (css_string ):
20- """Parse a CSS string as a color value,
21- as defined in `CSS Color Level 3 <http://www.w3.org/TR/css3-color/>`_.
22-
23- This is a convenience wrapper around :func:`parse_color` in case you
24- have a string that is not from a CSS stylesheet.
25-
26- :param css_string:
27- An unicode string in CSS syntax.
28- :returns:
29- Same as :func:`parse_color`.
30-
31- """
32- return parse_color (parse_one_component_value (css_string ))
33-
34-
35- def parse_color (token ):
36- """Parse single token as a color value,
37- as defined in `CSS Color Level 3 <http://www.w3.org/TR/css3-color/>`_.
20+ def parse_color (input ):
21+ """Parse a color value as defined in `CSS Color Level 3
22+ <http://www.w3.org/TR/css3-color/>`_.
3823
3924 :param token:
40- A single :class:`~.token_data.Token` or
41- :class:`~.token_data.ContainerToken`, as found eg. in a
42- property value.
25+ A :term:`string`, or a single :term:`component value`.
4326 :returns:
4427 * :obj:`None` if the token is not a valid CSS 3 color value.
4528 (No exception is raised.)
46- * For the *currentColor* keyword: the string ``'currentColor'``
47- * Every other values (including keywords, HSL and HSLA) is converted
48- to RGBA and returned as an :class:`RGBA` object (a 4-tuple with
49- attribute access).
29+ * The string ``'currentColor'`` for the *currentColor* keyword
30+ * Or a :class:`RGBA` object for every other values
31+ (including keywords, HSL and HSLA.)
5032 The alpha channel is clipped to [0, 1], but R, G, or B can be
5133 out of range (eg. ``rgb(-51, 306, 0)`` is represented as
5234 ``(-.2, 1.2, 0, 1)``.)
5335
5436 """
37+ if isinstance (input , basestring ):
38+ token = parse_one_component_value (input )
39+ else :
40+ token = input
5541 if token .type == 'ident' :
5642 return _COLOR_KEYWORDS .get (token .lower_value )
5743 elif token .type == 'hash' :
0 commit comments