Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dimostenis committed Nov 30, 2021
1 parent bdb07e7 commit dd8a766
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
**/__pycache__

.venv/
dist/
__pycache__

.mypy_cache
.pytest_cache
.venv
.vscode
.python-version

dist

poetry.lock
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions colorhash/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .colorhash import ColorHash
from colorhash.colorhash import ColorHash

__all__ = [ColorHash]
__all__ = ["ColorHash"]
26 changes: 16 additions & 10 deletions colorhash/colorhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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'
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 14 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
]
license = "MIT"
include = [
"README.md",
]

readme = "README.md"
homepage = "https://github.com/dimostenis/color-hash-python"
Expand All @@ -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"
28 changes: 28 additions & 0 deletions test/test_colorhash.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dd8a766

Please sign in to comment.