|
6 | 6 | # SPDX-License-Identifier: BSD-3-Clause
|
7 | 7 |
|
8 | 8 | import re
|
| 9 | +from typing import List |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def _reversedict(dict_to_reverse: dict) -> dict:
|
@@ -299,3 +300,43 @@ def _get_hex_to_name_map(spec: str):
|
299 | 300 | if spec not in _SUPPORTED_SPECIFICATIONS:
|
300 | 301 | raise ValueError(_SPECIFICATION_ERROR_TEMPLATE.format(spec=spec))
|
301 | 302 | return _hex_to_names[spec]
|
| 303 | + |
| 304 | + |
| 305 | +def names(spec: str = CSS3) -> List[str]: |
| 306 | + """ |
| 307 | + Return the list of valid color names for the given specification. |
| 308 | +
|
| 309 | + The color names will be normalized to all-lowercase, and will be returned in |
| 310 | + alphabetical order. |
| 311 | +
|
| 312 | + .. note:: **Spelling variants** |
| 313 | +
|
| 314 | + Some values representing named gray colors can map to either of two names in |
| 315 | + CSS3, because it supports both ``"gray"`` and ``"grey"`` spelling variants for |
| 316 | + those colors. Functions which produce a name from a color value in other formats |
| 317 | + all normalize to the ``"gray"`` spelling for consistency with earlier CSS and |
| 318 | + HTML specifications which only supported ``"gray"``. Here, however, *all* valid |
| 319 | + names are returned, including -- for CSS3 -- both variant spellings for each of |
| 320 | + the affected ``"gray"``/``"grey"`` colors. |
| 321 | +
|
| 322 | + Examples: |
| 323 | +
|
| 324 | + .. doctest:: |
| 325 | +
|
| 326 | + >>> names(spec=HTML4) |
| 327 | + ['aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', |
| 328 | + 'lime', 'maroon', 'navy', 'olive', 'purple', 'red', |
| 329 | + 'silver', 'teal', 'white', 'yellow'] |
| 330 | + >>> names(spec="CSS1") |
| 331 | + Traceback (most recent call last): |
| 332 | + ... |
| 333 | + ValueError: "CSS1" is not a supported specification ... |
| 334 | +
|
| 335 | +
|
| 336 | + :raises ValueError: when the given spec is not supported. |
| 337 | +
|
| 338 | + """ |
| 339 | + if spec not in _SUPPORTED_SPECIFICATIONS: |
| 340 | + raise ValueError(_SPECIFICATION_ERROR_TEMPLATE.format(spec=spec)) |
| 341 | + mapping = _names_to_hex[spec] |
| 342 | + return list(sorted(mapping.keys())) |
0 commit comments