Skip to content

Commit

Permalink
Re-apply formatting with updated line length.
Browse files Browse the repository at this point in the history
  • Loading branch information
ubernostrum committed May 20, 2024
1 parent ac73e73 commit 6c625ae
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 239 deletions.
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"python": ("https://docs.python.org/3", None),
}

# Spelling check needs an additional module that is not installed by default.
# Add it only if spelling check is requested so docs can be generated without it.
# Spelling check needs an additional module that is not installed by default. Add it
# only if spelling check is requested so docs can be generated without it.
if "spelling" in sys.argv:
extensions.append("sphinxcontrib.spelling")

Expand Down
75 changes: 34 additions & 41 deletions src/webcolors/_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def name_to_hex(name: str, spec: str = _definitions.CSS3) -> str:
"""
Convert a color name to a normalized hexadecimal color value.
The color name will be normalized to lower-case before being looked
up.
The color name will be normalized to lower-case before being looked up.
Examples:
Expand All @@ -41,8 +40,8 @@ def name_to_hex(name: str, spec: str = _definitions.CSS3) -> str:
ValueError: "goldenrod" is not defined as a named color in html4.
:param name: The color name to convert.
:param spec: The specification from which to draw the list of color
names. Default is :data:`CSS3`.
:param spec: The specification from which to draw the list of color names. Default
is :data:`CSS3`.
:raises ValueError: when the given name has no definition in the given spec.
"""
Expand All @@ -59,8 +58,7 @@ def name_to_rgb(name: str, spec: str = _definitions.CSS3) -> IntegerRGB:
Convert a color name to a 3-:class:`tuple` of :class:`int` suitable for use in
an ``rgb()`` triplet specifying that color.
The color name will be normalized to lower-case before being looked
up.
The color name will be normalized to lower-case before being looked up.
Examples:
Expand All @@ -74,8 +72,8 @@ def name_to_rgb(name: str, spec: str = _definitions.CSS3) -> IntegerRGB:
IntegerRGB(red=218, green=165, blue=32)
:param name: The color name to convert.
:param spec: The specification from which to draw the list of color
names. Default is :data:`CSS3.`
:param spec: The specification from which to draw the list of color names. Default
is :data:`CSS3.`
:raises ValueError: when the given name has no definition in the given spec.
"""
Expand All @@ -84,11 +82,10 @@ def name_to_rgb(name: str, spec: str = _definitions.CSS3) -> IntegerRGB:

def name_to_rgb_percent(name: str, spec: str = _definitions.CSS3) -> PercentRGB:
"""
Convert a color name to a 3-:class:`tuple` of percentages suitable for use
in an ``rgb()`` triplet specifying that color.
Convert a color name to a 3-:class:`tuple` of percentages suitable for use in an
``rgb()`` triplet specifying that color.
The color name will be normalized to lower-case before being looked
up.
The color name will be normalized to lower-case before being looked up.
Examples:
Expand All @@ -102,8 +99,8 @@ def name_to_rgb_percent(name: str, spec: str = _definitions.CSS3) -> PercentRGB:
PercentRGB(red='85.49%', green='64.71%', blue='12.5%')
:param name: The color name to convert.
:param spec: The specification from which to draw the list of color
names. Default is :data:`CSS3`.
:param spec: The specification from which to draw the list of color names. Default
is :data:`CSS3`.
:raises ValueError: when the given name has no definition in the given spec.
"""
Expand All @@ -116,8 +113,8 @@ def name_to_rgb_percent(name: str, spec: str = _definitions.CSS3) -> PercentRGB:

def hex_to_name(hex_value: str, spec: str = _definitions.CSS3) -> str:
"""
Convert a hexadecimal color value to its corresponding normalized
color name, if any such name exists.
Convert a hexadecimal color value to its corresponding normalized color name, if
any such name exists.
The hexadecimal value will be normalized before being looked up.
Expand Down Expand Up @@ -147,10 +144,10 @@ def hex_to_name(hex_value: str, spec: str = _definitions.CSS3) -> str:
ValueError: "#daa520" has no defined color name in html4.
:param hex_value: The hexadecimal color value to convert.
:param spec: The specification from which to draw the list of color
names. Default is :data:`CSS3`.
:raises ValueError: when the given color has no name in the given
spec, or when the supplied hex value is invalid.
:param spec: The specification from which to draw the list of color names. Default
is :data:`CSS3`.
:raises ValueError: when the given color has no name in the given spec, or when the
supplied hex value is invalid.
"""
if spec not in _definitions.SUPPORTED_SPECIFICATIONS:
Expand Down Expand Up @@ -189,8 +186,8 @@ def hex_to_rgb(hex_value: str) -> IntegerRGB:

def hex_to_rgb_percent(hex_value: str) -> PercentRGB:
"""
Convert a hexadecimal color value to a 3-:class:`tuple` of percentages
suitable for use in an ``rgb()`` triplet representing that color.
Convert a hexadecimal color value to a 3-:class:`tuple` of percentages suitable
for use in an ``rgb()`` triplet representing that color.
The hexadecimal value will be normalized before being converted.
Expand All @@ -217,11 +214,10 @@ def hex_to_rgb_percent(hex_value: str) -> PercentRGB:
def rgb_to_name(rgb_triplet: IntTuple, spec: str = _definitions.CSS3) -> str:
"""
Convert a 3-:class:`tuple` of :class:`int`, suitable for use in an ``rgb()``
color triplet, to its corresponding normalized color name, if any
such name exists.
color triplet, to its corresponding normalized color name, if any such name exists.
To determine the name, the triplet will be converted to a
normalized hexadecimal value.
To determine the name, the triplet will be converted to a normalized hexadecimal
value.
.. note:: **Spelling variants**
Expand All @@ -241,8 +237,8 @@ def rgb_to_name(rgb_triplet: IntTuple, spec: str = _definitions.CSS3) -> str:
'navy'
:param rgb_triplet: The ``rgb()`` triplet.
:param spec: The specification from which to draw the list of color
names. Default is :data:`CSS3`.
:param spec: The specification from which to draw the list of color names. Default
is :data:`CSS3`.
:raises ValueError: when the given color has no name in the given spec.
"""
Expand Down Expand Up @@ -273,8 +269,8 @@ def rgb_to_hex(rgb_triplet: IntTuple) -> str:
def rgb_to_rgb_percent(rgb_triplet: IntTuple) -> PercentRGB:
"""
Convert a 3-:class:`tuple` of :class:`int`, suitable for use in an ``rgb()``
color triplet, to a 3-:class:`tuple` of percentages suitable for use in
representing that color.
color triplet, to a 3-:class:`tuple` of percentages suitable for use in representing
that color.
.. note:: **Floating-point precision**
Expand Down Expand Up @@ -325,11 +321,10 @@ def rgb_percent_to_name(
) -> str:
"""
Convert a 3-:class:`tuple` of percentages, suitable for use in an ``rgb()``
color triplet, to its corresponding normalized color name, if any
such name exists.
color triplet, to its corresponding normalized color name, if any such name exists.
To determine the name, the triplet will be converted to a
normalized hexadecimal value.
To determine the name, the triplet will be converted to a normalized hexadecimal
value.
.. note:: **Spelling variants**
Expand All @@ -351,8 +346,8 @@ def rgb_percent_to_name(
'goldenrod'
:param rgb_percent_triplet: The ``rgb()`` triplet.
:param spec: The specification from which to draw the list of color
names. Default is :data:`CSS3`.
:param spec: The specification from which to draw the list of color names. Default
is :data:`CSS3`.
:raises ValueError: when the given color has no name in the given spec.
"""
Expand All @@ -365,8 +360,7 @@ def rgb_percent_to_name(
def rgb_percent_to_hex(rgb_percent_triplet: PercentTuple) -> str:
"""
Convert a 3-:class:`tuple` of percentages, suitable for use in an ``rgb()``
color triplet, to a normalized hexadecimal color value for that
color.
color triplet, to a normalized hexadecimal color value for that color.
Examples:
Expand All @@ -393,9 +387,8 @@ def rgb_percent_to_rgb(rgb_percent_triplet: PercentTuple) -> IntegerRGB:
color triplet, to a 3-:class:`tuple` of :class:`int` suitable for use in
representing that color.
Some precision may be lost in this conversion. See the note
regarding precision for :func:`~webcolors.rgb_to_rgb_percent` for
details.
Some precision may be lost in this conversion. See the note regarding precision for
:func:`~webcolors.rgb_to_rgb_percent` for details.
Examples:
Expand Down
52 changes: 23 additions & 29 deletions src/webcolors/_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

def _reversedict(dict_to_reverse: dict) -> dict:
"""
Internal helper for generating reverse mappings; given a
dictionary, returns a new dictionary with keys and values swapped.
Internal helper for generating reverse mappings; given a dictionary, returns a
new dictionary with keys and values swapped.
"""
return {value: key for key, value in dict_to_reverse.items()}
Expand All @@ -36,14 +36,13 @@ def _reversedict(dict_to_reverse: dict) -> dict:

# The HTML 4 named colors.
#
# The canonical source for these color definitions is the HTML 4
# specification:
# The canonical source for these color definitions is the HTML 4 specification:
#
# http://www.w3.org/TR/html401/types.html#h-6.5
#
# 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.
# 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 = {
"aqua": "#00ffff",
"black": "#000000",
Expand Down Expand Up @@ -71,23 +70,20 @@ def _reversedict(dict_to_reverse: dict) -> dict:

# The CSS3/SVG named colors.
#
# The canonical source for these color definitions is the SVG
# specification's color list (which was adopted as CSS 3's color
# definition):
# The canonical source for these color definitions is the SVG specification's color list
# (which was adopted as CSS 3's color definition):
#
# http://www.w3.org/TR/SVG11/types.html#ColorKeywords
#
# CSS3 also provides definitions of these colors:
#
# http://www.w3.org/TR/css3-color/#svg-color
#
# SVG provides the definitions as RGB triplets. CSS3 provides them
# both as RGB triplets and as hexadecimal. Since hex values are more
# common in real-world HTML and CSS, the 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.
# SVG provides the definitions as RGB triplets. CSS3 provides them both as RGB triplets
# and as hexadecimal. Since hex values are more common in real-world HTML and CSS, the
# 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 = {
"aliceblue": "#f0f8ff",
"antiquewhite": "#faebd7",
Expand Down Expand Up @@ -250,19 +246,17 @@ def _reversedict(dict_to_reverse: dict) -> dict:

CSS3_HEX_TO_NAMES = _reversedict(CSS3_NAMES_TO_HEX)

# CSS3 defines both "gray" and "grey", as well as defining either
# variant for other related colors like "darkgray"/"darkgrey". For a
# "forward" lookup from name to hex, this is straightforward, but a
# "reverse" lookup from hex to name requires picking one spelling.
# CSS3 defines both "gray" and "grey", as well as defining either variant for other
# related colors like "darkgray"/"darkgrey". For a "forward" lookup from name to hex,
# this is straightforward, but a "reverse" lookup from hex to name requires picking one
# spelling.
#
# The way in which _reversedict() generates the reverse mappings will
# pick a spelling based on the ordering of dictionary keys, which
# varies according to the version and implementation of Python in use,
# and in some Python versions is explicitly not to be relied on for
# consistency. So here we manually pick a single spelling that will
# consistently be returned. Since "gray" was the only spelling
# supported in HTML 4, CSS1, and CSS2, "gray" and its variants are
# chosen.
# The way in which _reversedict() generates the reverse mappings will pick a spelling
# based on the ordering of dictionary keys, which varies according to the version and
# implementation of Python in use, and in some Python versions is explicitly not to be
# relied on for consistency. So here we manually pick a single spelling that will
# consistently be returned. Since "gray" was the only spelling supported in HTML 4,
# CSS1, and CSS2, "gray" and its variants are chosen.
CSS3_HEX_TO_NAMES["#a9a9a9"] = "darkgray"
CSS3_HEX_TO_NAMES["#2f4f4f"] = "darkslategray"
CSS3_HEX_TO_NAMES["#696969"] = "dimgray"
Expand Down
16 changes: 7 additions & 9 deletions src/webcolors/_html5.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"""
HTML5 color algorithms.
Note that these functions are written in a way that may seem strange to
developers familiar with Python, because they do not use the most
efficient or idiomatic way of accomplishing their tasks. This is
because, for compliance, these functions are written as literal
translations into Python of the algorithms in HTML5:
Note that these functions are written in a way that may seem strange to developers
familiar with Python, because they do not use the most efficient or idiomatic way of
accomplishing their tasks. This is because, for compliance, these functions are written
as literal translations into Python of the algorithms in HTML5:
https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#colours
For ease of understanding, the relevant steps of the algorithm from
the standard are included as comments interspersed in the
implementation.
For ease of understanding, the relevant steps of the algorithm from the standard are
included as comments interspersed in the implementation.
"""

Expand Down Expand Up @@ -40,7 +38,7 @@ def html5_parse_simple_color(value: str) -> HTML5SimpleColor:
:param value: The color to parse.
:type value: :class:`str`, which must consist of exactly the character ``"#"``
followed by six hexadecimal digits
followed by six hexadecimal digits.
:raises ValueError: when the given value is not a Unicode string of length 7,
consisting of exactly the character ``#`` followed by six hexadecimal digits.
Expand Down
Loading

0 comments on commit 6c625ae

Please sign in to comment.