diff --git a/docs/conf.py b/docs/conf.py index ba8b0df..e273d04 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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") diff --git a/src/webcolors/_conversion.py b/src/webcolors/_conversion.py index 2568a49..1cf6f12 100644 --- a/src/webcolors/_conversion.py +++ b/src/webcolors/_conversion.py @@ -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: @@ -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. """ @@ -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: @@ -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. """ @@ -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: @@ -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. """ @@ -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. @@ -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: @@ -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. @@ -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** @@ -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. """ @@ -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** @@ -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** @@ -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. """ @@ -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: @@ -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: diff --git a/src/webcolors/_definitions.py b/src/webcolors/_definitions.py index f76ef82..f21c81a 100644 --- a/src/webcolors/_definitions.py +++ b/src/webcolors/_definitions.py @@ -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()} @@ -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", @@ -71,9 +70,8 @@ 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 # @@ -81,13 +79,11 @@ def _reversedict(dict_to_reverse: dict) -> dict: # # 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", @@ -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" diff --git a/src/webcolors/_html5.py b/src/webcolors/_html5.py index 2962c73..0411cfb 100644 --- a/src/webcolors/_html5.py +++ b/src/webcolors/_html5.py @@ -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. """ @@ -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. diff --git a/src/webcolors/_normalization.py b/src/webcolors/_normalization.py index 710cd88..6124461 100644 --- a/src/webcolors/_normalization.py +++ b/src/webcolors/_normalization.py @@ -11,14 +11,13 @@ def normalize_hex(hex_value: str) -> str: """ - Normalize a hexadecimal color value to a string consisting of the - character `#` followed by six lowercase hexadecimal digits (what - HTML5 terms a "valid lowercase simple color"). + Normalize a hexadecimal color value to a string consisting of the character `#` + followed by six lowercase hexadecimal digits (what HTML5 terms a "valid lowercase + simple color"). - If the supplied value cannot be interpreted as a hexadecimal color - value, :exc:`ValueError` is raised. See :ref:`the conventions used - by this module ` for information on acceptable formats - for hexadecimal values. + If the supplied value cannot be interpreted as a hexadecimal color value, + :exc:`ValueError` is raised. See :ref:`the conventions used by this module + ` for information on acceptable formats for hexadecimal values. Examples: @@ -56,8 +55,8 @@ def normalize_hex(hex_value: str) -> str: def _normalize_integer_rgb(value: int) -> int: """ - Internal normalization function for clipping integer values into - the permitted range (0-255, inclusive). + Internal normalization function for clipping integer values into the permitted + range (0-255, inclusive). """ return 0 if value < 0 else 255 if value > 255 else value @@ -65,8 +64,8 @@ def _normalize_integer_rgb(value: int) -> int: def normalize_integer_triplet(rgb_triplet: IntTuple) -> IntegerRGB: """ - Normalize an integer ``rgb()`` triplet so that all values are - within the range 0..255. + Normalize an integer ``rgb()`` triplet so that all values are within the range + 0..255. Examples: @@ -89,8 +88,8 @@ def normalize_integer_triplet(rgb_triplet: IntTuple) -> IntegerRGB: def _normalize_percent_rgb(value: str) -> str: """ - Internal normalization function for clipping percent values into - the permitted range (0%-100%, inclusive). + Internal normalization function for clipping percent values into the permitted + range (0%-100%, inclusive). """ value = value.split("%")[0] @@ -101,8 +100,8 @@ def _normalize_percent_rgb(value: str) -> str: def normalize_percent_triplet(rgb_triplet: PercentTuple) -> PercentRGB: """ - Normalize a percentage ``rgb()`` triplet so that all values are - within the range 0%..100%. + Normalize a percentage ``rgb()`` triplet so that all values are within the range + 0%..100%. Examples: @@ -123,8 +122,8 @@ def normalize_percent_triplet(rgb_triplet: PercentTuple) -> PercentRGB: def _percent_to_integer(percent: str) -> int: """ - Internal helper for converting a percentage value to an integer - between 0 and 255 inclusive. + Internal helper for converting a percentage value to an integer between 0 and + 255 inclusive. """ return int(round(float(percent.split("%")[0]) / 100 * 255)) diff --git a/src/webcolors/_types.py b/src/webcolors/_types.py index d4d097f..d16dec8 100644 --- a/src/webcolors/_types.py +++ b/src/webcolors/_types.py @@ -1,6 +1,5 @@ """ -Types and type aliases used to represent colors in various -formats. +Types and type aliases used to represent colors in various formats. """ @@ -11,11 +10,9 @@ class IntegerRGB(typing.NamedTuple): """ - :class:`~typing.NamedTuple` representing an integer RGB - triplet. + :class:`~typing.NamedTuple` representing an integer RGB triplet. - Has three fields, each of type :class:`int` and in the range 0-255 - inclusive: + Has three fields, each of type :class:`int` and in the range 0-255 inclusive: .. attribute:: red @@ -38,11 +35,10 @@ class IntegerRGB(typing.NamedTuple): class PercentRGB(typing.NamedTuple): """ - :class:`~typing.NamedTuple` representing a percentage RGB - triplet. + :class:`~typing.NamedTuple` representing a percentage RGB triplet. - Has three fields, each of type :class:`str` and representing a - percentage value in the range 0%-100% inclusive: + Has three fields, each of type :class:`str` and representing a percentage value in + the range 0%-100% inclusive: .. attribute:: red @@ -65,11 +61,9 @@ class PercentRGB(typing.NamedTuple): class HTML5SimpleColor(typing.NamedTuple): """ - :class:`~typing.NamedTuple` representing an HTML5 simple - color. + :class:`~typing.NamedTuple` representing an HTML5 simple color. - Has three fields, each of type :class:`int` and in the range 0-255 - inclusive: + Has three fields, each of type :class:`int` and in the range 0-255 inclusive: .. attribute:: red diff --git a/tests/definitions.py b/tests/definitions.py index 5ba912e..2b9c3cb 100644 --- a/tests/definitions.py +++ b/tests/definitions.py @@ -1,6 +1,6 @@ """ -Test accuracy of the mappings of color names and values, by extracting -the definitions of the colors from the relevant standards documents. +Test accuracy of the mappings of color names and values, by extracting the +definitions of the colors from the relevant standards documents. """ @@ -18,9 +18,8 @@ class HTML4DefinitionTests(unittest.TestCase): """ - Extract the names and values of the 16 defined HTML 4 colors from - the online version of the standard, and check them against this - module's definitions of them. + Extract the names and values of the 16 defined HTML 4 colors from the online + version of the standard, and check them against this module's definitions of them. """ @@ -44,8 +43,7 @@ def setUp(self): def test_color_definitions(self): """ - Ensure the values in this module match those in the HTML 4 - specification. + Ensure the values in this module match those in the HTML 4 specification. """ for color_name, color_value in self.html4_colors.items(): @@ -55,9 +53,8 @@ def test_color_definitions(self): class CSS21DefinitionTests(unittest.TestCase): """ - Extract the names and values of the 17 defined CSS 2.1 colors from - the online version of the standard, and check them against this - module's definitions of them. + Extract the names and values of the 17 defined CSS 2.1 colors from the online + version of the standard, and check them against this module's definitions of them. """ @@ -81,8 +78,7 @@ def setUp(self): def test_color_definitions(self): """ - Ensure thie values in this module match those in the - CSS2.1 specification. + Ensure thie values in this module match those in the CSS2.1 specification. """ for color_name, color_value in self.css21_colors.items(): @@ -92,15 +88,15 @@ def test_color_definitions(self): class CSS3DefinitionTests(unittest.TestCase): """ - Extract the names and values of the 147 defined CSS 3 colors from - the online version of the standard, and check them against this - module's definitions of them. + Extract the names and values of the 147 defined CSS 3 colors from the online + version of the standard, and check them against this module's definitions of them. """ def setUp(self): """ Fetch the CSS3 color definitions and store them on self.css3_colors. + """ self.css3_colors = {} soup = BeautifulSoup( @@ -133,8 +129,7 @@ def setUp(self): def test_color_definitions(self): """ - Ensure the values in this module match those in the CSS3 - specification. + Ensure the values in this module match those in the CSS3 specification. """ for color_name, color_values in self.css3_colors.items(): diff --git a/tests/full_colors.py b/tests/full_colors.py index 4ef9514..22e0249 100644 --- a/tests/full_colors.py +++ b/tests/full_colors.py @@ -1,27 +1,23 @@ """ -Test case which exercises webcolors' conversion functions across all -16,777,216 possible hexadecimal values and all 16,777,216 possible -integer rgb() triplet values. - -You should not ever need to run this test; it is not part of the -normal unit-test suite, and is used only as a final check when -preparing a new release of webcolors. - -Because it generates each of the nearly 17 million color values -multiple times, this test case takes some time to run and consumes -most or all available CPU while running. As a consolation, it is -somewhat efficient with respect to memory. - -Due to the inherent imprecision of floating-point percentage values, -and the fact that the legal (with respect to the CSS standards) set of -percentage rgb() triplets is uncountably infinite, percentage rgb() -triplets are not exhaustively tested here, and the normal test suite -is used to ensure correctness of the conversion functions for those -values. - -The only test performed here for percentage rgb() triplets is to -ensure that converting an integer rgb() triplet to percentage and back -returns the original integer values, for consistency. +Test case which exercises webcolors' conversion functions across all 16,777,216 +possible hexadecimal values and all 16,777,216 possible integer rgb() triplet values. + +You should not ever need to run this test; it is not part of the normal unit-test suite, +and is used only as a final check when preparing a new release of webcolors. + +Because it generates each of the nearly 17 million color values multiple times, this +test case takes some time to run and consumes most or all available CPU while +running. As a consolation, it is somewhat efficient with respect to memory. + +Due to the inherent imprecision of floating-point percentage values, and the fact that +the legal (with respect to the CSS standards) set of percentage rgb() triplets is +uncountably infinite, percentage rgb() triplets are not exhaustively tested here, and +the normal test suite is used to ensure correctness of the conversion functions for +those values. + +The only test performed here for percentage rgb() triplets is to ensure that converting +an integer rgb() triplet to percentage and back returns the original integer values, for +consistency. """ @@ -34,8 +30,8 @@ def hex_colors(): """ - Generator which yields all 2**24 hexadecimal color values, in - order starting from #000000. + Generator which yields all 2**24 hexadecimal color values, in order starting + from #000000. """ for i in range(16777217): @@ -44,8 +40,8 @@ def hex_colors(): def int_colors(): """ - Generator which yields all 2**24 integer rgb() triplets, in - order starting from (0,0,0). + Generator which yields all 2**24 integer rgb() triplets, in order starting from + (0,0,0). """ red_counter = tuple(range(256)) @@ -75,13 +71,12 @@ def test_full_colors(self): def test_triplet_conversion(self): """ - Test conversion between integer and percentage rgb() as - fully as possible. + Test conversion between integer and percentage rgb() as fully as possible. - Because exhaustive testing is not possible (the set of valid - percentgae rgb() triplets is uncountably infinite), this test - ensures that for each of the 2**24 possible integer triplets, - conversion to percentage and back gives the starting value. + Because exhaustive testing is not possible (the set of valid percentgae rgb() + triplets is uncountably infinite), this test ensures that for each of the 2**24 + possible integer triplets, conversion to percentage and back gives the starting + value. """ for int_triplet in int_colors(): diff --git a/tests/test_conformance.py b/tests/test_conformance.py index 4a01358..0be600e 100644 --- a/tests/test_conformance.py +++ b/tests/test_conformance.py @@ -1,12 +1,10 @@ """ -Conformance tests which do not require an internet connection or HTML -parsing libraries, and so can be run as part of the normal test suite -of webcolors. +Conformance tests which do not require an internet connection or HTML parsing +libraries, and so can be run as part of the normal test suite of webcolors. -For tests which extract the relevant values, during the test run, from -the online standards documents (and so require both an internet -connection and an HTML parsing library), see the file -``definitions.py`` in this directory. +For tests which extract the relevant values, during the test run, from the online +standards documents (and so require both an internet connection and an HTML parsing +library), see the file ``definitions.py`` in this directory. """ @@ -16,13 +14,11 @@ import webcolors -# The mappings of color names to values below are used for conformance -# testing; while the main webcolors module makes use of alphabetized, -# normalized mappings to hex values, the mappings below are the -# definitions in precisely the form they take in the relevant -# standards documents (they were produced via automated extraction -# from the HTML of those documents, to avoid the possibility of human -# copy/paste error). +# The mappings of color names to values below are used for conformance testing; while +# the main webcolors module makes use of alphabetized, normalized mappings to hex +# values, the mappings below are the definitions in precisely the form they take in the +# relevant standards documents (they were produced via automated extraction from the +# HTML of those documents, to avoid the possibility of human copy/paste error). # # Sources are: # @@ -31,8 +27,8 @@ # SVG colors (which CSS 3 adopted): # http://www.w3.org/TR/SVG/types.html#ColorKeywords # -# Conformance of this module with the relevant standards is proven by -# comparing its output to these mappings. +# Conformance of this module with the relevant standards is proven by comparing its +# output to these mappings. HTML4_COLOR_DEFINITIONS = { "Black": "#000000", "Silver": "#C0C0C0", @@ -205,15 +201,15 @@ class ConformanceTests(unittest.TestCase): """ - Demonstrate that this module conforms to the relevant standards - documents governing colors on the Web. + Demonstrate that this module conforms to the relevant standards documents + governing colors on the web. """ def test_html_definition_conformance(self): """ - Compare the results of name-to-hex conversion to the canonical - hex values provided in the HTML 4 specification. + Compare the results of name-to-hex conversion to the canonical hex values + provided in the HTML 4 specification. """ for color, hex_value in HTML4_COLOR_DEFINITIONS.items(): @@ -222,8 +218,8 @@ def test_html_definition_conformance(self): def test_svg_definition_conformance(self): """ - Compare the results of name-to-rgb-triplet conversion to the - canonical triplet values provided in the SVG specification. + Compare the results of name-to-rgb-triplet conversion to the canonical + triplet values provided in the SVG specification. """ for color, triplet in SVG_COLOR_DEFINITIONS.items(): diff --git a/tests/test_conversion.py b/tests/test_conversion.py index 76c1163..ee049f7 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -13,14 +13,14 @@ class HexConversionTests(unittest.TestCase): """ - Test the functions which convert from hex color codes to other - formats. + Test the functions which convert from hex color codes to other formats. """ def test_hex_to_name(self): """ Test conversion from hex to color name. + """ test_pairs = ( ("#ffffff", "white"), @@ -34,9 +34,8 @@ def test_hex_to_name(self): def test_hex_to_name_unnamed(self): """ - A hex code which does not correspond to a named color, or does - not correspond to a named color in the given specification, - raises ValueError. + A hex code which does not correspond to a named color, or does not + correspond to a named color in the given specification, raises ValueError. """ # No name in any spec. @@ -49,8 +48,8 @@ def test_hex_to_name_unnamed(self): def test_hex_to_name_specs(self): """ - Using one of the supported specifications succeeds; using an - unsupported specification raises ValueError. + Using one of the supported specifications succeeds; using an unsupported + specification raises ValueError. """ for supported_spec in webcolors._definitions.SUPPORTED_SPECIFICATIONS: @@ -97,8 +96,7 @@ def test_hex_to_rgb_percent(self): class IntegerRGBConversionTests(unittest.TestCase): """ - Test the functions which convert from integer RGB triplets to - other formats. + Test the functions which convert from integer RGB triplets to other formats. """ @@ -119,9 +117,8 @@ def test_rgb_to_name(self): def test_rgb_to_name_unnamed(self): """ - An integer RGB triplet which does not correspond to a named - color, or does not correspond to a named color in the given - specification, raises ValueError. + An integer RGB triplet which does not correspond to a named color, or does + not correspond to a named color in the given specification, raises ValueError. """ # No name in any spec. @@ -134,8 +131,8 @@ def test_rgb_to_name_unnamed(self): def test_rgb_to_name_specs(self): """ - Using one of the supported specifications succeeds; an - unsupported specification raises ValueError. + Using one of the supported specifications succeeds; an unsupported + specification raises ValueError. """ for supported_spec in webcolors._definitions.SUPPORTED_SPECIFICATIONS: @@ -166,8 +163,7 @@ def test_rgb_to_hex(self): def test_rgb_to_rgb_percent(self): """ - Test conversion from integer RGB triplet to percent RGB - triplet. + Test conversion from integer RGB triplet to percent RGB triplet. """ test_pairs = ( @@ -184,14 +180,14 @@ def test_rgb_to_rgb_percent(self): class NameConversionTests(unittest.TestCase): """ - Test the functions which convert from color names to other - formats. + Test the functions which convert from color names to other formats. """ def test_name_to_hex(self): """ Test correct conversion of color names to hex. + """ test_pairs = ( ("white", "#ffffff"), @@ -204,9 +200,8 @@ def test_name_to_hex(self): def test_name_to_hex_bad_name(self): """ - A name which does not correspond to a color, or does not - correspond to a color in the given specification, raises - ValueError. + A name which does not correspond to a color, or does not correspond to a + color in the given specification, raises ValueError. """ test_values = ( @@ -220,8 +215,8 @@ def test_name_to_hex_bad_name(self): def test_name_to_hex_specs(self): """ - Using one of the supported specifications succeeds; using an - unsupported specification raises ValueError. + Using one of the supported specifications succeeds; using an unsupported + specification raises ValueError. """ for supported_spec in webcolors._definitions.SUPPORTED_SPECIFICATIONS: @@ -268,8 +263,7 @@ def test_name_to_rgb_percent(self): class PercentRGBConversionTests(unittest.TestCase): """ - Test the functions which convert from percent RGB triplets to - other formats. + Test the functions which convert from percent RGB triplets to other formats. """ @@ -289,9 +283,8 @@ def test_rgb_percent_to_name(self): def test_rgb_percent_to_name_unnamed(self): """ - A percent RGB triplet which does not correspond to a named - color, or does not correspond to a named color in the given - specification, raises ValueError. + A percent RGB triplet which does not correspond to a named color, or does + not correspond to a named color in the given specification, raises ValueError. """ # No name in any spec. @@ -309,8 +302,8 @@ def test_rgb_percent_to_name_unnamed(self): def test_rgb_percent_to_name_specs(self): """ - Using one of the supported specifications succeeds; an - unsupported specification raises ValueError. + Using one of the supported specifications succeeds; an unsupported + specification raises ValueError. """ for supported_spec in ("html4", "css2", "css21", "css3"): @@ -343,8 +336,7 @@ def test_rgb_percent_to_hex(self): def test_rgb_percent_to_rgb(self): """ - Test conversion from percent RGB triplet to integer RGB - triplet. + Test conversion from percent RGB triplet to integer RGB triplet. """ test_pairs = ( @@ -361,16 +353,15 @@ def test_rgb_percent_to_rgb(self): class ConversionTests(unittest.TestCase): """ - Test other aspects of convevrsion not covered by format-specific - test cases. + Test other aspects of conversion not covered by format-specific test cases. """ def test_spelling_variants(self): """ - When asked to name a color value that maps to either of 'gray' or - 'grey' in CSS3, or a related color like 'darkgray'/'darkgrey', - webcolors always picks 'gray' as the spelling. + When asked to name a color value that maps to either of 'gray' or 'grey' in + CSS3, or a related color like 'darkgray'/'darkgrey', webcolors always picks + 'gray' as the spelling. """ test_values = ( diff --git a/tests/test_html5.py b/tests/test_html5.py index 961d545..b972bbc 100644 --- a/tests/test_html5.py +++ b/tests/test_html5.py @@ -18,8 +18,8 @@ class HTML5Tests(unittest.TestCase): def test_parse_simple_color(self): """ - Test implementation of the HTML5 simple color parsing - algorithm. + Test implementation of the HTML5 simple color parsing algorithm. + """ test_pairs = ( ("#ffffff", (255, 255, 255)), @@ -33,8 +33,7 @@ def test_parse_simple_color(self): def test_parse_simple_color_error(self): """ - Test error conditions of the HTML5 simple color parsing - algorithm. + Test error conditions of the HTML5 simple color parsing algorithm. """ test_values = ( @@ -50,8 +49,7 @@ def test_parse_simple_color_error(self): def test_serialize_simple_color(self): """ - Test implementation of the HTML5 simple color serialization - algorithm. + Test implementation of the HTML5 simple color serialization algorithm. """ test_pairs = ( @@ -67,14 +65,12 @@ def test_serialize_simple_color(self): def test_parse_legacy_color(self): """ - Test implementation of the HTML5 legacy color parsing - algorithm. + Test implementation of the HTML5 legacy color parsing algorithm. """ - # One of these is the famous "chucknorris" value. Another is a - # CSS 2 system color. The final two are randomly-generated but - # believable junk strings. Correct output values obtained - # manually. + # One of these is the famous "chucknorris" value. Another is a CSS 2 system + # color. The final two are randomly-generated but believable junk + # strings. Correct output values obtained manually. test_pairs = ( ("chucknorris", (192, 0, 0)), ("Window", (0, 13, 0)), @@ -98,8 +94,8 @@ def test_parse_legacy_color_names(self): def test_parse_legacy_color_hex(self): """ - Test the HTML5 legacy color parsing of three- and six-digit - hexadecimal color values. + Test the HTML5 legacy color parsing of three- and six-digit hexadecimal + color values. """ test_values = ("#000", "#000000", "#fff", "#ffffff", "#000080") diff --git a/tests/test_normalization.py b/tests/test_normalization.py index 579b0b9..e0f0232 100644 --- a/tests/test_normalization.py +++ b/tests/test_normalization.py @@ -12,15 +12,14 @@ class NormalizationTests(unittest.TestCase): """ - Test both the publicly-exposed and internal normalization - functions. + Test both the publicly-exposed and internal normalization functions. """ def test_normalize_hex(self): """ - Hexadecimal normalization normalizes valid hex color codes to - 6 digits, lowercase. + Hexadecimal normalization normalizes valid hex color codes to 6 digits, + lowercase. """ test_pairs = (