diff --git a/.gitignore b/.gitignore index 2843094..10f9ffd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,10 @@ -**/__pycache__ - -.venv/ -dist/ +__pycache__ +.mypy_cache +.pytest_cache +.venv .vscode -.python-version + +dist + poetry.lock diff --git a/README.md b/README.md index b27a301..4d65f3d 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ Python 3.4+. ## Changelog +- color-hash 1.0.4 *(2021-11-30)* + - Support only for python 3.6+ + - Add tests - color-hash 1.0.3 *(2020-12-04)* - Drop support for python 2 - Handover of project maintenance diff --git a/colorhash/__init__.py b/colorhash/__init__.py index fd67c3f..2128878 100644 --- a/colorhash/__init__.py +++ b/colorhash/__init__.py @@ -1,3 +1,3 @@ -from .colorhash import ColorHash +from colorhash.colorhash import ColorHash -__all__ = [ColorHash] +__all__ = ["ColorHash"] diff --git a/colorhash/colorhash.py b/colorhash/colorhash.py index c5b391a..04e4379 100644 --- a/colorhash/colorhash.py +++ b/colorhash/colorhash.py @@ -16,13 +16,15 @@ >>> c.hex '#2dd24b' """ - from binascii import crc32 from numbers import Number +from typing import Any +from typing import Tuple -def crc32_hash(obj): - """Generate a hash for ``obj``. +def crc32_hash(obj: Any): + """ + Generate a hash for ``obj``. This function first converts the object to a string and encodes it into UTF-8, then calculates and returns the CRC-32 checksum of the result. The @@ -33,8 +35,9 @@ def crc32_hash(obj): return crc32(bs) & 0xFFFFFFFF -def hsl2rgb(hsl): - """Convert an HSL color value into RGB. +def hsl2rgb(hsl: Tuple[int, float, float]): + """ + Convert an HSL color value into RGB. >>> hsl2rgb((0, 1, 0.5)) (255, 0, 0) @@ -70,8 +73,9 @@ def hsl2rgb(hsl): return tuple(rgb) -def rgb2hex(rgb): - """Format an RGB color value into a hexadecimal color string. +def rgb2hex(rgb: Tuple[int, int, int]): + """ + Format an RGB color value into a hexadecimal color string. >>> rgb2hex((255, 0, 0)) '#ff0000' @@ -83,14 +87,15 @@ def rgb2hex(rgb): def color_hash( - obj, + obj: Any, hashfunc=crc32_hash, lightness=(0.35, 0.5, 0.65), saturation=(0.35, 0.5, 0.65), min_h=None, max_h=None, ): - """Calculate the color for the given object. + """ + Calculate the color for the given object. Args: obj: the value. @@ -131,7 +136,8 @@ def color_hash( class ColorHash: - """Generate a color value and provide it in several format. + """ + Generate a color value and provide it in several format. This class takes the same arguments as the ``color_hash`` function. diff --git a/pyproject.toml b/pyproject.toml index 7477506..14d215b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,12 +1,15 @@ [tool.poetry] name = "colorhash" -version = "1.0.3" +version = "1.0.4" description = "Generate color based on any object" authors = [ "dimostenis", "Felix Krull " ] license = "MIT" +include = [ + "README.md", + ] readme = "README.md" homepage = "https://github.com/dimostenis/color-hash-python" @@ -18,16 +21,22 @@ keywords = ["color", "hash"] "Bug Tracker" = "https://github.com/dimostenis/color-hash-python/issues" [tool.poetry.dependencies] -python = "^3.3" +python = "^3.6" [tool.poetry.dev-dependencies] mypy = "^0.782" flake8 = "^3.8.3" black = "^20.8b1" +pytest = "^6.2.5" -[build-system] -requires = ["poetry>=0.12"] -build-backend = "poetry.masonry.api" +[tool.isort] +profile = "black" +multi_line_output = 7 +force_single_line = true [tool.black] line-length = 88 + +[build-system] +requires = ["poetry>=0.12"] +build-backend = "poetry.masonry.api" diff --git a/test/test_colorhash.py b/test/test_colorhash.py new file mode 100644 index 0000000..1e8c325 --- /dev/null +++ b/test/test_colorhash.py @@ -0,0 +1,28 @@ +from pytest import fixture + +from colorhash import ColorHash + + +@fixture +def objs(): + return ( + "foo", + "bar", + None, + [0, 1], + {1, 2}, + {"a": 0}, + ColorHash("w00t"), + ) + + +def test_colorhash_returns_some_color(objs): + + for obj in objs: + assert isinstance(ColorHash(obj).hex, str) + + +def test_colorhash_is_deterministic(objs): + + for obj in objs: + assert ColorHash(obj).hex == ColorHash(obj).hex