A lightweight, zero-dependency Python library for color manipulation.
Project description
ColorBrew
A lightweight, zero-dependency Python library for working with colors.
Features
- Parse colors from hex strings (
#3498db,#fff,3498db) - Parse CSS function strings (
rgb(52, 152, 219),hsl(204, 70%, 53%)) - Parse CSS named colors (
cornflowerblue,red, etc.) - Convert between hex, RGB, HSL, and CMYK formats
- Input validation with clear error messages
- 148 built-in CSS named colors
- Immutable
Colorclass with format properties and dunder methods - CSS/HTML-ready output (
rgb(),rgba(),hsl(),hsla(), hex) - Format string support (
f"{color:rgb}",f"{color:hsl}") - Reverse name lookup — find the closest CSS named color with distance score
- Color manipulation — lighten, darken, saturate, desaturate, rotate hue, invert, grayscale, mix
- Photoshop-style blend modes — multiply, screen, overlay, soft light, hard light, difference
- Palette generation — complementary, analogous, triadic, split-complementary, tetradic
- WCAG accessibility — luminance, contrast ratio, AA/AAA compliance checks
Installation
pip install colorbrew
Requires Python 3.12+. Zero runtime dependencies.
Quick Start
from colorbrew import Color
# Create from any format
c = Color("#3498db")
c = Color(52, 152, 219)
c = Color("rgb(52, 152, 219)")
c = Color("cornflowerblue")
# Alternate constructors
c = Color.from_hsl(204, 70, 53)
c = Color.from_cmyk(76, 31, 0, 14)
c = Color.from_name("cornflowerblue")
# Access format properties
c.rgb # (52, 152, 219)
c.hex # "#3498db"
c.hsl # (204, 70, 53)
c.cmyk # (76, 31, 0, 14)
# CSS output
c.css_rgb # "rgb(52, 152, 219)"
c.css_hsl # "hsl(204, 70%, 53%)"
c.css_hex # "#3498db"
c.css_rgba(0.5) # "rgba(52, 152, 219, 0.5)"
c.css_hsla(0.8) # "hsla(204, 70%, 53%, 0.8)"
# Format strings
f"{c:rgb}" # "rgb(52, 152, 219)"
f"{c:hsl}" # "hsl(204, 70%, 53%)"
f"{c:hex}" # "#3498db"
# Reverse name lookup
match = c.closest_name
match.name # "steelblue"
match.distance # 10.7703
match.exact # False
# Manipulation (all return new Color instances)
c.lighten(20) # lighter color
c.darken(10) # darker color
c.saturate(15) # more vivid
c.desaturate(15) # more muted
c.rotate(180) # shift hue
c.invert() # RGB inverse
c.grayscale() # remove saturation
c.mix(Color("red"), 0.5) # blend two colors
# Blend modes
c.blend(Color("white"), "multiply") # multiply blend
c.blend(Color("black"), "screen") # screen blend
# Palette generation
c.complementary() # opposite hue
c.analogous() # 3 neighboring hues
c.triadic() # 2 colors at 120° intervals
c.split_complementary() # 2 colors flanking the complement
c.tetradic() # 3 colors at 90° intervals
# Accessibility (WCAG 2.1)
c.luminance # relative luminance (0.0-1.0)
c.contrast(Color("white")) # contrast ratio (1.0-21.0)
c.meets_aa(Color("white")) # True if passes WCAG AA
c.meets_aaa(Color("white")) # True if passes WCAG AAA
# Iteration and comparison
r, g, b = c # unpack RGB
Color("#ff0000") == Color("red") # True
Development
uv sync
uv run pytest
uv run ruff check src/
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file colorbrew-0.4.0.tar.gz.
File metadata
- Download URL: colorbrew-0.4.0.tar.gz
- Upload date:
- Size: 24.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ac4f6a5803409beb56f499808745d7227fef1b1646bc0082f6aacf3cfefbec4
|
|
| MD5 |
71aef365e34365d9120a3a9495c18a33
|
|
| BLAKE2b-256 |
0bf5e0dba4ffc83599faa8a021052e6efa41c9520fc30a7a95eb82240025e56b
|
Provenance
The following attestation bundles were made for colorbrew-0.4.0.tar.gz:
Publisher:
publish.yml on zfoq/colorbrew
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
colorbrew-0.4.0.tar.gz -
Subject digest:
8ac4f6a5803409beb56f499808745d7227fef1b1646bc0082f6aacf3cfefbec4 - Sigstore transparency entry: 1005371372
- Sigstore integration time:
-
Permalink:
zfoq/colorbrew@3ee75ecf39f8c79d1d5e2b8e421b2473fb859878 -
Branch / Tag:
refs/tags/0.4.0 - Owner: https://github.com/zfoq
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3ee75ecf39f8c79d1d5e2b8e421b2473fb859878 -
Trigger Event:
push
-
Statement type:
File details
Details for the file colorbrew-0.4.0-py3-none-any.whl.
File metadata
- Download URL: colorbrew-0.4.0-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
017ad5d3498d4de09ee8acde9eb21edff5c6060f2712d2d1ba140177d8588fd5
|
|
| MD5 |
7451e746a649920ad395096cfb7e9f11
|
|
| BLAKE2b-256 |
01d18cd631936ac687f21de834fe2a129b4cb598a6575b8204dbdb5444820507
|
Provenance
The following attestation bundles were made for colorbrew-0.4.0-py3-none-any.whl:
Publisher:
publish.yml on zfoq/colorbrew
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
colorbrew-0.4.0-py3-none-any.whl -
Subject digest:
017ad5d3498d4de09ee8acde9eb21edff5c6060f2712d2d1ba140177d8588fd5 - Sigstore transparency entry: 1005371373
- Sigstore integration time:
-
Permalink:
zfoq/colorbrew@3ee75ecf39f8c79d1d5e2b8e421b2473fb859878 -
Branch / Tag:
refs/tags/0.4.0 - Owner: https://github.com/zfoq
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3ee75ecf39f8c79d1d5e2b8e421b2473fb859878 -
Trigger Event:
push
-
Statement type: