Colorize
Colorize is a small, typed Python library for manipulating colors in the OKLCH color space. It provides immutable color values, lightness and saturation adjustments, color harmonies, WCAG contrast helpers, and palette/theme generation.
Requirements
- Python 3.12 or newer
Installation
Install the package from this repository:
python -m pip install .
Usage
from colorize import Colorize
brand = Colorize("#336699")
print(brand.lighten(0.2)) # #5084b9
print(brand.harmonies.complement) # #865815
print(brand.wcag().best_text_color()) # #ffffff
print(brand.alpha(0.5)) # #33669980
palette = brand.palette
print(palette[500]) # #336699
rating = brand.wcag(contrasting_with=Colorize("#ffffff")).rating
print(rating.aa.normal) # True
Operations can be chained to build more complex adjustments:
brand = Colorize("#336699")
print(brand.tint(0.2).lighten(0.4)) # #8bb6e2
print(brand.lighten(0.4).alpha(0.5)) # #6ea3da80
Adjustment values
Adjustment values are proportions from 0.0 to 1.0. For example,
brand.lighten(0.2) lightens the color by 20%, moving its OKLCH lightness 20%
of the remaining distance toward white.
darken(0.2)reduces the current lightness by 20%.saturate(0.2)increases the current chroma by 20%.desaturate(0.2)reduces the current chroma by 20%.tint(0.2)mixes in 20% white, whileshade(0.2)mixes in 20% black.alpha(0.5)sets the color to 50% opacity.
Values outside the supported range are clamped: negative values become 0.0,
and values greater than 1.0 become 1.0. Adjustment methods return new
Colorize values and leave the original unchanged.
Three- and six-digit hex values are supported, as are four- and eight-digit values with an alpha channel.
HexColor
HexColor is the immutable, normalized hexadecimal value stored in
Colorize.hex. It supports RGB/RGBA conversion and detects whether an alpha
channel is present.
from colorize import HexColor, Palettes
color = HexColor("abc8")
print(color) # #aabbcc88
print(color.has_alpha) # True
print(color.to_rgb().rgb) # [170, 187, 204]
print(color.to_rgba().rgba) # [170, 187, 204, 136]
green = HexColor.from_rgb(red=0, green=128, blue=0)
print(green) # #008000
transparent_blue = HexColor.from_rgb(Palettes.RGBA(red=51, green=102, blue=153, alpha=128))
print(transparent_blue) # #33669980
Returned types
Colorize uses typed model objects where a result has named components. The
palette models are available through Palettes; lower-level helper types are
available from colorize.types.
| API | Return type | Description |
|---|---|---|
color.hex |
HexColor |
Normalized hexadecimal value |
HexColor.to_rgb() |
Palettes.RGB |
Named red, green, and blue channels |
HexColor.to_rgba() |
Palettes.RGBA |
Named red, green, blue, and alpha channels |
color.oklch |
Palettes.Oklch |
Named lightness, chroma, and hue values |
color.oklch.values |
list[float] |
OKLCH components in lightness, chroma, hue order |
color.lightness, color.chroma, color.hue |
float |
Individual OKLCH components |
rotate_hue(), darken(), lighten() |
Colorize |
Adjusted immutable color |
saturate(), desaturate(), tint(), shade() |
Colorize |
Adjusted immutable color |
tints(), shades() |
list[Colorize] |
Generated color scale |
alpha() |
str |
Eight-digit hexadecimal string with alpha |
harmonies |
Harmonics |
Harmony generator for the color |
harmonies.complement |
Colorize |
Complementary color |
harmonies.analogous(), harmonies.triadic |
Palettes.Triadic |
Named three-color palette |
harmonies.split_complementary() |
Palettes.Triadic |
Named split-complementary palette |
dual.colors, triadic.colors |
list[Colorize] |
Palette colors in field order |
quadratic.colors |
list[Colorize] |
Four palette colors in field order |
harmonies.monochromatic() |
list[Colorize] |
Monochromatic color scale |
contrast_ratio() |
float |
Contrast ratio against another color, or white by default |
wcag(contrasting_with=...) |
WCAG |
Accessibility helper using the provided color, or white by default |
wcag().rating |
Rating |
Ratio with aa and aaa Level results |
wcag().best_text_color(), wcag().shade() |
Colorize |
Contrast-selected color |
palette |
dict[int, Colorize] |
Palette keyed by stops from 50 through 950 |
theme |
ColorTheme |
Complete generated color theme |
Palette results expose both named fields and ordered list helpers:
from colorize import Colorize, Palettes
from colorize.types import Harmonics, Level, Rating, WCAG
brand = Colorize("#336699")
oklch: Palettes.Oklch = brand.oklch
harmonies: Harmonics = brand.harmonies
analogous: Palettes.Triadic = harmonies.analogous()
wcag: WCAG = brand.wcag(contrasting_with=Colorize("#ffffff"))
rating: Rating = wcag.rating
theme = brand.theme
print(oklch.lightness)
assert oklch.values == [oklch.lightness, oklch.chroma, oklch.hue]
print(analogous.primary, analogous.secondary, analogous.tertiary)
assert analogous.colors == [analogous.primary, analogous.secondary, analogous.tertiary]
print(rating.aa == Level(normal=True, large=True)) # True
print(theme.primary) # #336699
Use .serialize to convert colors, harmonies, RGB values, palettes, WCAG
ratings, and themes to built-in Python values:
data = brand.serialize
print(data["hex"]) # #336699
print(data["alpha"]) # False
print(data["lightness"]) # 0.4993144558452082
print(data["chroma"]) # 0.09866437712418324
print(data["hue"]) # 250.4330574201755
print(brand.harmonies.serialize["complement"])
# {"hex": "#865815" ... }
{
"hex": "#336699",
"alpha": False,
"lightness": 0.4993144558452082,
"chroma": 0.09866437712418324,
"hue": 250.4330574201755
}
Tests
Run the verification checks from the project root:
python -m pytest
Metadata
Release files for py-colorized 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py_colorized-1.0.0.tar.gz | 15.3 kB | Without provenance | Details for py_colorized-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py_colorized-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for py_colorized-1.0.0-py3-none-any.whl |
Total release size: 29.4 kB
Release files / py_colorized-1.0.0.tar.gz
| Download URL | py_colorized-1.0.0.tar.gz |
|---|---|
| Size | 15.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
06cf69340381102487458bd8d818c016a97c1c81b4ab1bdbd064137dc614d599
|
|
BLAKE2b-256 checksum How to use checksums |
66c5fa83f16e91b57723cca4b7c52436c6f0deefc9498082fae332f711403122
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.3
|
Release files / py_colorized-1.0.0-py3-none-any.whl
| Download URL | py_colorized-1.0.0-py3-none-any.whl |
|---|---|
| Size | 14.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
28e6f1c6e9f73f7fdc96115078352541dc2a46a80d9c5eaa9c9c4a57816c4458
|
|
BLAKE2b-256 checksum How to use checksums |
8f7fc041bc22a88cbbcea416b264207f6b39e142bab86eb0d635a61161bf0f3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.3
|
Security for py-colorized 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.