Skip to main content

color-contrast

A python library to calculate and modulate color contrasts according to WCAG2.1

PyPI - Version GitHub Tag GitHub Issues or Pull Requests PyPI - License

Usage

Install color-contrast into your python environment, using:

pip install color-contrast

After installing, you can use the project as follows:

from colour import Color

from color_contrast import AccessibilityLevel, check_contrast

bg = Color("#123456")

check_contrast("#404040", bg, level=AccessibilityLevel.AA18)  # True

check_contrast("#404040", bg, level=AccessibilityLevel.AA)  # False

check_contrast("white", bg, level=AccessibilityLevel.AAA)  # True

check_contrast(Color(hsl=(1, 1, 1)), bg)  # True

Or if you want to modulate the colors automatically:

from color_contrast import modulate, ModulationMode

modulate("5d3eb2", "5d3eb2", mode=ModulationMode.BOTH)
# Returns:
# <Color: #32215f>, <Color: #9e89d6>, true

This example used the same foreground and background color, this is the before and after:

Before After

Contributing

To set the development environment up, follow the following steps. Make sure you have poetry installed.

# Clone the repository
git clone git@github.com:ZugBahnHof/color-contrast.git

cd color-contrast

# Activate the virtual environment
poetry shell

# Install the requirements
poetry install

# Activate the commit hooks
pre-commit install 

Useful commands:

# Run the linter
ruff check

# Reformat
ruff check --fix
ruff format

# Run the test cases
python -m unittest tests

Metadata

Release files for color-contrast 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for color-contrast 0.1.1
File Size Uploaded
color_contrast-0.1.1.tar.gz 2.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for color-contrast 0.1.1
File Interpreter ABI Platform
color_contrast-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.3 kB

Release files / color_contrast-0.1.1.tar.gz

Download URL color_contrast-0.1.1.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
168d823434293a3e625a239ae03cfbc7371de695c873979d85df24e776c204cf
BLAKE2b-256 checksum
How to use checksums
f5d113ca64af15b9abf4dd3daf8d69aa0705a43169d7e493fe3f47d276691416
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.6 Linux/6.12.17-amd64

Release files / color_contrast-0.1.1-py3-none-any.whl

Download URL color_contrast-0.1.1-py3-none-any.whl
Size 3.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a8f379e29a813ebcca0196bb8a90ce46fec63615ce68a534b2d6719afb4099d
BLAKE2b-256 checksum
How to use checksums
a8a260889952b11c1a9443c467b874a8383a737e2c8287171c2c61df3c3397bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.6 Linux/6.12.17-amd64

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page