-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
676735c
commit 333f62b
Showing
29 changed files
with
355 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,75 @@ Its hosted on PyPI. | |
pip install colorhash | ||
``` | ||
|
||
## Advanced usage | ||
|
||
You can influence every aspect of final color. **Default values** are following: | ||
|
||
```python | ||
ColorHash( | ||
obj: Any, | ||
lightness: Sequence[float, ...] = (0.35, 0.5, 0.65), # picks 'randomly' one | ||
saturation: Sequence[float, ...] = (0.35, 0.5, 0.65), # picks 'randomly' one | ||
min_h: Optional[int] = None, # hue, min 0 | ||
max_h: Optional[int] = None, # hue, max 360 | ||
) | ||
``` | ||
|
||
But be careful, **setting tight conditions may result in very similar colors**. See example tables. | ||
|
||
You can fix lightness or saturation to single value(s) by using sequence with 1 element (eg. `[0.5]`). | ||
|
||
| code | hex | color | | ||
|:--------------------------------------|:---------:|:-------------------------------:| | ||
| `ColorHash('hey') # default` | `#782d86` | ![#782d86](./docs/782d86.png) | | ||
| `ColorHash('hey', lightness=[0.55])` | `#b453c6` | ![#b453c6](./docs/b453c6.png) | | ||
| `ColorHash('hey', lightness=[0.75])` | `#d69fdf` | ![#d69fdf](./docs/d69fdf.png) | | ||
| `ColorHash('hey', lightness=[0.95])` | `#f7ecf9` | ![#f7ecf9](./docs/f7ecf9.png) | | ||
| `ColorHash('hey', saturation=[0.15])` | `#8d6c93` | ![#8d6c93](./docs/8d6c93.png) | | ||
| `ColorHash('hey', saturation=[0.55])` | `#b139c6` | ![#b139c6](./docs/b139c6.png) | | ||
| `ColorHash('hey', saturation=[0.95])` | `#d406f9` | ![#d406f9](./docs/d406f9.png) | | ||
| `ColorHash('hey', lightness=[0.95], saturation=[0.95])` | `#fbe6fe` | ![#fbe6fe](./docs/fbe6fe.png) | | ||
| `ColorHash('oh', lightness=[0.95], saturation=[0.95])` | `#fef0e6` | ![#fef0e6](./docs/fef0e6.png) | | ||
| `ColorHash('boi', lightness=[0.95], saturation=[0.95])` | `#e6fee7` | ![#e6fee7](./docs/e6fee7.png) | | ||
|
||
You can set hue range or even fix it by setting `min_h` = `max_h`. | ||
|
||
| code | hex | color | | ||
|:--------------------------------------|:---------:|:-------------------------------:| | ||
| `ColorHash('hey', min_h=150)` | `#2d5886` | ![#2d5886](./docs/2d5886.png) | | ||
| `ColorHash('hey', min_h=300)` | `#862d6c` | ![#862d6c](./docs/862d6c.png) | | ||
| `ColorHash('hey', max_h=150)` | `#866e2d` | ![#866e2d](./docs/866e2d.png) | | ||
| `ColorHash('hey', min_h=150, max_h=360)` | `#2d5886` | ![#2d5886](./docs/2d5886.png) | | ||
| `ColorHash('hey', min_h=150, max_h=150) # fixed hue` | `#2d8659` | ![#2d8659](./docs/2d8659.png) | | ||
|
||
Or you can let `ColorHash` decide between combination of many `lightness` and `saturation` options (mind `min_h` and `max_h` are equal in this example). | ||
|
||
| code | hex | color | | ||
|:--------------------------------------|:---------:|:-------------------------------:| | ||
| `ColorHash('stick', min_h=65, max_h=65, saturation=[x/10 for x in range(1, 10)], lightness=[x/10 for x in range(1, 10)])` | `#869108` | ![#869108](./docs/869108.png) | | ||
| `ColorHash('with', min_h=65, max_h=65, saturation=[x/10 for x in range(1, 10)], lightness=[x/10 for x in range(1, 10)])` | `#eef5a3` | ![#eef5a3](./docs/eef5a3.png) | | ||
| `ColorHash('one', min_h=65, max_h=65, saturation=[x/10 for x in range(1, 10)], lightness=[x/10 for x in range(1, 10)])` | `#ddeb47` | ![#ddeb47](./docs/ddeb47.png) | | ||
|
||
Finally some bad examples. When you set too strict rules, colors may be almost identical. | ||
|
||
| code | hex | color | | ||
|:--------------------------------------|:---------:|:-------------------------------:| | ||
| `ColorHash('lets', lightness=[0.95], saturation=[0.95], min_h=300)` | `#fee6f8` | ![#fee6f8](./docs/fee6f8.png) | | ||
| `ColorHash('break', lightness=[0.95], saturation=[0.95], min_h=300)` | `#fee6fb` | ![#fee6fb](./docs/fee6fb.png) | | ||
| `ColorHash('it', lightness=[0.95], saturation=[0.95], min_h=300)` | `#fee6fa` | ![#fee6fa](./docs/fee6fa.png) | | ||
| `ColorHash('here', min_h=150, max_h=150)` | `#6ce0a6` | ![#6ce0a6](./docs/6ce0a6.png) | | ||
| `ColorHash('goes', min_h=150, max_h=150)` | `#79d2a6` | ![#79d2a6](./docs/79d2a6.png) | | ||
| `ColorHash('almost', min_h=150, max_h=150)` | `#6ce0a6` | ![#6ce0a6](./docs/6ce0a6.png) | | ||
| `ColorHash('same', min_h=150, max_h=150)` | `#79d2a6` | ![#79d2a6](./docs/79d2a6.png) | | ||
| `ColorHash('color', min_h=150, max_h=150)` | `#6ce0a6` | ![#6ce0a6](./docs/6ce0a6.png) | | ||
|
||
|
||
## Changelog | ||
|
||
- color-hash **2.0.0** *(2023-09-22)* | ||
- ✨ Expose params to influence colors | ||
- ✨ Runtime validation of input params | ||
- 📝 Update docs for advanced usage | ||
- color-hash **1.3.2** *(2023-09-21)* | ||
- ⚡️ 30%+ speedup on `hsl2rgb()` | ||
- ✅ Add tests for all named colors (1500+ tests) | ||
|
@@ -48,7 +115,7 @@ pip install colorhash | |
- ➕ Add `hatch` (build & test) | ||
- ➕ Add `twine` (publish) | ||
- ✨ Support `python3.11` | ||
- ⚰️ Drop support for `python3.6` | ||
- ⚰️ Drop support for `python3.6` (downloads from PyPI are under 1%) | ||
- color-hash **1.2.2** *(2022-10-17)* | ||
- ✨ Add publish helper script | ||
- color-hash **1.2.1** *(2022-10-17)* | ||
|
@@ -71,6 +138,18 @@ pip install colorhash | |
- color-hash **1.0.0** *(2016-07-07)* | ||
- 🎉 Initial port. | ||
|
||
## Speed comparison | ||
|
||
Running `pytest` (1600+ tests) on different python versions. | ||
|
||
| python | secs | | ||
| :----: | :-----: | | ||
| 3.7 | `1.252` | | ||
| 3.8 | `1.239` | | ||
| 3.9 | `0.720` | | ||
| 3.10 | `0.690` | | ||
| 3.11 | `0.892` | | ||
|
||
## License | ||
|
||
Copyright (c) 2016 Felix Krull <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from pathlib import Path | ||
|
||
from PIL import Image | ||
|
||
# needed for eval() | ||
from colorhash import ColorHash # noqa | ||
|
||
NEW_TABLE_SEPARATOR = object() # sentinel | ||
|
||
lst = [ | ||
NEW_TABLE_SEPARATOR, | ||
"ColorHash('hey') # default", | ||
"ColorHash('hey', lightness=[0.55])", | ||
"ColorHash('hey', lightness=[0.75])", | ||
"ColorHash('hey', lightness=[0.95])", | ||
"ColorHash('hey', saturation=[0.15])", | ||
"ColorHash('hey', saturation=[0.55])", | ||
"ColorHash('hey', saturation=[0.95])", | ||
"ColorHash('hey', lightness=[0.95], saturation=[0.95])", | ||
"ColorHash('oh', lightness=[0.95], saturation=[0.95])", | ||
"ColorHash('boi', lightness=[0.95], saturation=[0.95])", | ||
NEW_TABLE_SEPARATOR, | ||
"ColorHash('hey', min_h=150)", | ||
"ColorHash('hey', min_h=300)", | ||
"ColorHash('hey', max_h=150)", | ||
"ColorHash('hey', min_h=150, max_h=360)", | ||
"ColorHash('hey', min_h=150, max_h=150) # fixed hue", | ||
NEW_TABLE_SEPARATOR, | ||
"ColorHash('stick', min_h=65, max_h=65, saturation=[x/10 for x in range(1, 10)], lightness=[x/10 for x in range(1, 10)])", # noqa | ||
"ColorHash('with', min_h=65, max_h=65, saturation=[x/10 for x in range(1, 10)], lightness=[x/10 for x in range(1, 10)])", # noqa | ||
"ColorHash('one', min_h=65, max_h=65, saturation=[x/10 for x in range(1, 10)], lightness=[x/10 for x in range(1, 10)])", # noqa | ||
NEW_TABLE_SEPARATOR, | ||
"ColorHash('lets', lightness=[0.95], saturation=[0.95], min_h=300)", | ||
"ColorHash('break', lightness=[0.95], saturation=[0.95], min_h=300)", | ||
"ColorHash('it', lightness=[0.95], saturation=[0.95], min_h=300)", | ||
"ColorHash('here', min_h=150, max_h=150)", | ||
"ColorHash('goes', min_h=150, max_h=150)", | ||
"ColorHash('almost', min_h=150, max_h=150)", | ||
"ColorHash('same', min_h=150, max_h=150)", | ||
"ColorHash('color', min_h=150, max_h=150)", | ||
] | ||
|
||
WIDTH = 120 | ||
HEIGHT = 25 | ||
OUT = Path("docs") | ||
HEADER = "| code | hex | color |\n|:--------------------------------------|:---------:|:-------------------------------:|" # noqa | ||
|
||
# delete old tiles | ||
[x.unlink() for x in OUT.glob("*.png")] | ||
|
||
# gen tiles + docs table text | ||
for code in lst: | ||
# start new table | ||
if code == NEW_TABLE_SEPARATOR: | ||
print("\n") | ||
print(HEADER) | ||
continue | ||
|
||
hex_color = eval(code).hex | ||
tile = Image.new("RGB", (WIDTH, HEIGHT), hex_color) | ||
|
||
filename = f"{hex_color[1:]}.png" # eg. "eafbf6.png" | ||
tile.save(OUT / filename) | ||
|
||
s: str = f"| `{code}` | `{hex_color}` | ![{hex_color}](./docs/{filename}) |" | ||
print(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ requires-python = ">=3.6" | |
name = "colorhash" | ||
description = "Generate color based on any object" | ||
license = { text = "MIT" } | ||
version = "1.3.2" | ||
version = "2.0.0" | ||
readme = "README.md" | ||
authors = [ | ||
{ name = "dimostenis", email = "[email protected]" }, | ||
|
@@ -15,7 +15,7 @@ dependencies = [] | |
Homepage = "https://github.com/dimostenis/color-hash-python" | ||
"Bug Tracker" = "https://github.com/dimostenis/color-hash-python/issues" | ||
[project.optional-dependencies] | ||
dev = ["black", "pre-commit", "pytest"] | ||
dev = ["black", "pre-commit", "pytest", "pillow"] | ||
|
||
[tool.isort] | ||
profile = "black" | ||
|
Oops, something went wrong.