Skip to content

Commit

Permalink
Be absolutely clear about the mappings no longer being supported API.
Browse files Browse the repository at this point in the history
  • Loading branch information
ubernostrum committed Jun 6, 2024
1 parent 785d6da commit 68ba427
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
62 changes: 31 additions & 31 deletions src/webcolors/_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ def _reversedict(dict_to_reverse: dict) -> dict:
return {value: key for key, value in dict_to_reverse.items()}


HEX_COLOR_RE = re.compile(r"^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$")
_HEX_COLOR_RE = re.compile(r"^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$")

HTML4 = "html4"
CSS2 = "css2"
CSS21 = "css21"
CSS3 = "css3"

SUPPORTED_SPECIFICATIONS = (HTML4, CSS2, CSS21, CSS3)
_SUPPORTED_SPECIFICATIONS = (HTML4, CSS2, CSS21, CSS3)

SPECIFICATION_ERROR_TEMPLATE = (
_SPECIFICATION_ERROR_TEMPLATE = (
f"{{spec}} is not a supported specification for color name lookups; "
f"supported specifications are: {SUPPORTED_SPECIFICATIONS}."
f"supported specifications are: {_SUPPORTED_SPECIFICATIONS}."
)

# Mappings of color names to normalized hexadecimal color values.
Expand All @@ -43,7 +43,7 @@ def _reversedict(dict_to_reverse: dict) -> dict:
# The file tests/definitions.py in the source distribution of this module downloads a
# copy of the HTML 4 standard and parses out the color names to ensure the values below
# are correct.
HTML4_NAMES_TO_HEX = {
_HTML4_NAMES_TO_HEX = {
"aqua": "#00ffff",
"black": "#000000",
"blue": "#0000ff",
Expand All @@ -63,10 +63,10 @@ def _reversedict(dict_to_reverse: dict) -> dict:
}

# CSS2 used the same list as HTML 4.
CSS2_NAMES_TO_HEX = HTML4_NAMES_TO_HEX
_CSS2_NAMES_TO_HEX = _HTML4_NAMES_TO_HEX

# CSS2.1 added orange.
CSS21_NAMES_TO_HEX = {"orange": "#ffa500", **HTML4_NAMES_TO_HEX}
_CSS21_NAMES_TO_HEX = {"orange": "#ffa500", **_HTML4_NAMES_TO_HEX}

# The CSS3/SVG named colors.
#
Expand All @@ -84,7 +84,7 @@ def _reversedict(dict_to_reverse: dict) -> dict:
# mapping below is to hex values instead. The file tests/definitions.py in the source
# distribution of this module downloads a copy of the CSS3 color module and parses out
# the color names to ensure the values below are correct.
CSS3_NAMES_TO_HEX = {
_CSS3_NAMES_TO_HEX = {
"aliceblue": "#f0f8ff",
"antiquewhite": "#faebd7",
"aqua": "#00ffff",
Expand Down Expand Up @@ -238,13 +238,13 @@ def _reversedict(dict_to_reverse: dict) -> dict:
# Mappings of normalized hexadecimal color values to color names.
# --------------------------------------------------------------------------------

HTML4_HEX_TO_NAMES = _reversedict(HTML4_NAMES_TO_HEX)
_HTML4_HEX_TO_NAMES = _reversedict(_HTML4_NAMES_TO_HEX)

CSS2_HEX_TO_NAMES = HTML4_HEX_TO_NAMES
_CSS2_HEX_TO_NAMES = _HTML4_HEX_TO_NAMES

CSS21_HEX_TO_NAMES = _reversedict(CSS21_NAMES_TO_HEX)
_CSS21_HEX_TO_NAMES = _reversedict(_CSS21_NAMES_TO_HEX)

CSS3_HEX_TO_NAMES = _reversedict(CSS3_NAMES_TO_HEX)
_CSS3_HEX_TO_NAMES = _reversedict(_CSS3_NAMES_TO_HEX)

# CSS3 defines both "gray" and "grey", as well as defining either spelling variant for
# other related colors like "darkgray"/"darkgrey", etc. For a "forward" lookup from
Expand All @@ -253,27 +253,27 @@ def _reversedict(dict_to_reverse: dict) -> dict:
#
# Since "gray" was the only spelling supported in HTML 4, CSS1, and CSS2, "gray" and its
# variants are chosen here.
CSS3_HEX_TO_NAMES["#a9a9a9"] = "darkgray"
CSS3_HEX_TO_NAMES["#2f4f4f"] = "darkslategray"
CSS3_HEX_TO_NAMES["#696969"] = "dimgray"
CSS3_HEX_TO_NAMES["#808080"] = "gray"
CSS3_HEX_TO_NAMES["#d3d3d3"] = "lightgray"
CSS3_HEX_TO_NAMES["#778899"] = "lightslategray"
CSS3_HEX_TO_NAMES["#708090"] = "slategray"
_CSS3_HEX_TO_NAMES["#a9a9a9"] = "darkgray"
_CSS3_HEX_TO_NAMES["#2f4f4f"] = "darkslategray"
_CSS3_HEX_TO_NAMES["#696969"] = "dimgray"
_CSS3_HEX_TO_NAMES["#808080"] = "gray"
_CSS3_HEX_TO_NAMES["#d3d3d3"] = "lightgray"
_CSS3_HEX_TO_NAMES["#778899"] = "lightslategray"
_CSS3_HEX_TO_NAMES["#708090"] = "slategray"


_names_to_hex = {
HTML4: HTML4_NAMES_TO_HEX,
CSS2: CSS2_NAMES_TO_HEX,
CSS21: CSS21_NAMES_TO_HEX,
CSS3: CSS3_NAMES_TO_HEX,
HTML4: _HTML4_NAMES_TO_HEX,
CSS2: _CSS2_NAMES_TO_HEX,
CSS21: _CSS21_NAMES_TO_HEX,
CSS3: _CSS3_NAMES_TO_HEX,
}

_hex_to_names = {
HTML4: HTML4_HEX_TO_NAMES,
CSS2: CSS2_HEX_TO_NAMES,
CSS21: CSS21_HEX_TO_NAMES,
CSS3: CSS3_HEX_TO_NAMES,
HTML4: _HTML4_HEX_TO_NAMES,
CSS2: _CSS2_HEX_TO_NAMES,
CSS21: _CSS21_HEX_TO_NAMES,
CSS3: _CSS3_HEX_TO_NAMES,
}


Expand All @@ -284,8 +284,8 @@ def _get_name_to_hex_map(spec: str):
:raises ValueError: when the given spec is not supported.
"""
if spec not in SUPPORTED_SPECIFICATIONS:
raise ValueError(SPECIFICATION_ERROR_TEMPLATE.format(spec=spec))
if spec not in _SUPPORTED_SPECIFICATIONS:
raise ValueError(_SPECIFICATION_ERROR_TEMPLATE.format(spec=spec))
return _names_to_hex[spec]


Expand All @@ -296,6 +296,6 @@ def _get_hex_to_name_map(spec: str):
:raises ValueError: when the given spec is not supported.
"""
if spec not in SUPPORTED_SPECIFICATIONS:
raise ValueError(SPECIFICATION_ERROR_TEMPLATE.format(spec=spec))
if spec not in _SUPPORTED_SPECIFICATIONS:
raise ValueError(_SPECIFICATION_ERROR_TEMPLATE.format(spec=spec))
return _hex_to_names[spec]
4 changes: 2 additions & 2 deletions src/webcolors/_html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import string

from ._definitions import CSS3_NAMES_TO_HEX
from ._definitions import _CSS3_NAMES_TO_HEX
from ._types import HTML5SimpleColor, IntTuple


Expand Down Expand Up @@ -164,7 +164,7 @@ def html5_parse_legacy_color(value: str) -> HTML5SimpleColor:
# return the simple color corresponding to that keyword.
#
# Note: CSS2 System Colors are not recognized.
keyword_hex = CSS3_NAMES_TO_HEX.get(value.lower())
keyword_hex = _CSS3_NAMES_TO_HEX.get(value.lower())
if keyword_hex is not None:
return html5_parse_simple_color(keyword_hex)

Expand Down
4 changes: 2 additions & 2 deletions src/webcolors/_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# SPDX-License-Identifier: BSD-3-Clause

from ._definitions import HEX_COLOR_RE
from ._definitions import _HEX_COLOR_RE
from ._types import IntegerRGB, IntTuple, PercentRGB, PercentTuple


Expand Down Expand Up @@ -44,7 +44,7 @@ def normalize_hex(hex_value: str) -> str:
:raises ValueError: when the input is not a valid hexadecimal color value.
"""
match = HEX_COLOR_RE.match(hex_value)
match = _HEX_COLOR_RE.match(hex_value)
if match is None:
raise ValueError(f'"{hex_value}" is not a valid hexadecimal color value.')
hex_digits = match.group(1)
Expand Down
8 changes: 5 additions & 3 deletions tests/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_color_definitions(self):
"""
for color_name, color_value in self.html4_colors.items():
extracted = webcolors._definitions.HTML4_NAMES_TO_HEX[color_name.lower()]
extracted = webcolors._definitions._HTML4_NAMES_TO_HEX[color_name.lower()]
assert color_value.lower() == extracted


Expand Down Expand Up @@ -83,7 +83,7 @@ def test_color_definitions(self):
"""
for color_name, color_value in self.css21_colors.items():
extracted = webcolors._definitions.CSS21_NAMES_TO_HEX[color_name.lower()]
extracted = webcolors._definitions._CSS21_NAMES_TO_HEX[color_name.lower()]
assert color_value.lower() == extracted


Expand Down Expand Up @@ -134,7 +134,9 @@ def test_color_definitions(self):
"""
for color_name, color_values in self.css3_colors.items():
extracted_hex = webcolors._definitions.CSS3_NAMES_TO_HEX[color_name.lower()]
extracted_hex = webcolors._definitions._CSS3_NAMES_TO_HEX[
color_name.lower()
]
extracted_rgb = webcolors.name_to_rgb(color_name)
assert color_values["hex"].lower() == extracted_hex
assert color_values["rgb"] == extracted_rgb
Expand Down
6 changes: 3 additions & 3 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_hex_to_name_specs(self):
specification raises ValueError.
"""
for supported_spec in webcolors._definitions.SUPPORTED_SPECIFICATIONS:
for supported_spec in webcolors._definitions._SUPPORTED_SPECIFICATIONS:
result = webcolors.hex_to_name("#ffffff", spec=supported_spec)
assert "white" == result

Expand Down Expand Up @@ -135,7 +135,7 @@ def test_rgb_to_name_specs(self):
specification raises ValueError.
"""
for supported_spec in webcolors._definitions.SUPPORTED_SPECIFICATIONS:
for supported_spec in webcolors._definitions._SUPPORTED_SPECIFICATIONS:
result = webcolors.rgb_to_name((255, 255, 255), spec=supported_spec)
assert "white" == result

Expand Down Expand Up @@ -219,7 +219,7 @@ def test_name_to_hex_specs(self):
specification raises ValueError.
"""
for supported_spec in webcolors._definitions.SUPPORTED_SPECIFICATIONS:
for supported_spec in webcolors._definitions._SUPPORTED_SPECIFICATIONS:
result = webcolors.name_to_hex("white", spec=supported_spec)
assert "#ffffff" == result

Expand Down
2 changes: 1 addition & 1 deletion tests/test_html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_parse_legacy_color_names(self):
Test the HTML5 legacy color parsing of SVG/CSS3 color names.
"""
for name in webcolors._definitions.CSS3_NAMES_TO_HEX:
for name in webcolors._definitions._CSS3_NAMES_TO_HEX:
parsed = webcolors.html5_parse_legacy_color(name)
assert parsed == webcolors.name_to_rgb(name)

Expand Down

0 comments on commit 68ba427

Please sign in to comment.