Generate color based on any object
Maintained. Last release 6 weeks ago, commits in 1 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install colorhashcolor-hash
ColorHash is a lightweight Python library for generating deterministic colors from objects' string representations.
By calculating a color value based on an object's hash, it ensures consistent results: the same input will always yield the same color. This is particularly useful for UI elements like user avatars or category labels where visual consistency is required.
This module is a port of the original color-hash Javascript library.
Key features:
- Deterministic: Same input always results in the same color.
- Customizable: Influence hue range, lightness, and saturation.
- No runtime dependencies: Lightweight and easy to integrate.
- Compatibility: Supports Python 3.8–3.15.
Quick Start
>>> from colorhash import ColorHash
>>> c = ColorHash('Hello World')
>>> c.hsl
(131, 0.65, 0.5)
>>> c.rgb
(45, 210, 75)
>>> c.hex
'#2dd24b'
Installation
It is available on PyPI.
pip install colorhash
Basic (intended) use
| code | hex | color |
|---|---|---|
ColorHash('hey') | #782d86 | ![]() |
ColorHash('oh') | #d29d79 | ![]() |
ColorHash('boi') | #6ce072 | ![]() |
Advanced usage
You can influence every aspect of final color. Default values are following:
ColorHash(
obj: Any,
lightness: Union[int, float, Sequence[Union[int, float]]] = (0.35, 0.5, 0.65), # picks deterministically one
saturation: Union[int, float, Sequence[Union[int, float]]] = (0.35, 0.5, 0.65), # picks deterministically one
min_h: Optional[int] = None, # hue, min 0
max_h: Optional[int] = None, # hue, max 360
)


























