Skip to main content

Python library to calculate and modulate color contrasts according to WCAG2.1

Project description

color-contrast

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

PyPI - Version GitHub Tag GitHub Issues or Pull Requests

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

color_contrast-0.1.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

color_contrast-0.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file color_contrast-0.1.0.tar.gz.

File metadata

  • Download URL: color_contrast-0.1.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.6 Linux/6.7.12-amd64

File hashes

Hashes for color_contrast-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cb9cd0c09b3000e4ebeefe5da3c0f81ca7c9f5d60212b7473f1c2b41eb7e4b81
MD5 9559390e08e2d8a1f60b8a2536ff9852
BLAKE2b-256 4633c06f94b4eb048c59f0ad2640d822eb33e01bfb128a27b33a65fe93f47419

See more details on using hashes here.

File details

Details for the file color_contrast-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: color_contrast-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.6 Linux/6.7.12-amd64

File hashes

Hashes for color_contrast-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fcbf3e3bb9f4ad87af95acf9945f8505aa668f660d5a20843e7d7d65fda21756
MD5 e4d6b6cc9e382550fe3a61a0fca8ab3f
BLAKE2b-256 7054d85287d9550f649a7034655d8c5918244f979efad6e70a67dfbfacbfae38

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page