ColorHash

2.3.0 · active · verified Thu Apr 16

ColorHash is a lightweight Python library for generating deterministic colors from any object. By calculating a color value based on an object's hash, it ensures consistent results: the same input will always yield the same color. It is currently at version 2.3.0 and has an active development cycle, with recent updates focusing on performance and wider Python version compatibility.

Common errors

Warnings

Install

Imports

Quickstart

Initialize ColorHash with any object (typically a string) and retrieve its deterministic color in HSL, RGB, or hexadecimal format. You can also customize the lightness, saturation, or hue ranges during initialization to influence the generated colors.

from colorhash import ColorHash

# Generate a color for a string
c = ColorHash('Hello World')

# Access color representations
print(f"HSL: {c.hsl}")   # (Hue, Saturation, Lightness)
print(f"RGB: {c.rgb}")   # (Red, Green, Blue)
print(f"Hex: {c.hex}")   # Hexadecimal color string

# Customize color generation (e.g., specific lightness range)
c_custom = ColorHash('Another Object', lightness=[0.2, 0.4, 0.6])
print(f"Custom Hex: {c_custom.hex}")

view raw JSON →