Skip to content

Commit 1828e3c

Browse files
committed
Move the parse_color_string() behavior into parse_color()
1 parent 0534fb8 commit 1828e3c

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

tinycss2/color3.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import collections
44

5+
from ._compat import basestring
56
from .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':

tinycss2/test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
HashToken, IdentToken, LiteralToken, NumberToken, ParenthesesBlock,
1717
ParseError, PercentageToken, SquareBracketsBlock, StringToken, URLToken,
1818
UnicodeRangeToken, WhitespaceToken, Declaration, AtRule, QualifiedRule)
19-
from .color3 import parse_color_string, RGBA
19+
from .color3 import parse_color, RGBA
2020
from .nth import parse_nth
2121

2222

@@ -97,19 +97,19 @@ def test(css, expected):
9797
test_stylesheet = json_test(parse_stylesheet)
9898
test_rule_list = json_test(parse_rule_list)
9999
test_one_rule = json_test(parse_one_rule)
100-
test_color3 = json_test(parse_color_string, filename='color3.json')
100+
test_color3 = json_test(parse_color, filename='color3.json')
101101
test_nth = json_test(parse_nth, filename='An+B.json')
102102

103103

104104
# Do not use @pytest.mark.parametrize because it is slow with that many values.
105105
def test_color3_hsl():
106106
for css, expected in load_json('color3_hsl.json'):
107-
assert to_json(parse_color_string(css)) == expected
107+
assert to_json(parse_color(css)) == expected
108108

109109

110110
def test_color3_keywords():
111111
for css, expected in load_json('color3_keywords.json'):
112-
result = parse_color_string(css)
112+
result = parse_color(css)
113113
if result is not None:
114114
r, g, b, a = result
115115
result = [r * 255, g * 255, b * 255, a]

0 commit comments

Comments
 (0)