Package to process and change colors between color spaces and to tweak input colors to meet WCAG 2.1 accessibility standards.
Project description
Table of Contents:
Introduction
This library help to handle different color values inputs in three color spaces: RGB
, HSL
and HEX
.
The main functionality is ability to check the contrast between two colors as well adjusting those colors to fit
WCAG 2.1 specification.
Overview
This library provides two main features:
- A class Color that represents a color in many
possible color spaces. This class provides following functionalities:
- Validating color values for each color space - using Validator class.
- Processing (formatting) color values for each color space - changing format of values to list, dictionary, as well as normalizing values in appropriate color spaces.
- Converting color values between color spaces.
- A class AccessibilityProcessor that provides following
functionalities:
- Calculating contrast ratio between two colors.
- Checking if the contrast between two colors has ratio big enough for levels AA and AAA, with ration 4.5:1 and 7:1 respectively.
- Calculating and getting color that is AA or AAA level of contrast with the given color - by changing either foreground color, background color or both.
Installation
To install colors-accessibility
library, run the following command:
pip install colors-accessibility
color-accessibility
library does not have any dependencies.
Usage
Color
Color class currently handles three color spaces: rgb
, hsl
, and hex
.
This class can process different type of input values. To initialize a color, following inputs are valid:
Input values
For rgb
color space:
Code
from colors_accessibility import Color
# All representations below are valid and will be processed correctly.
valid_rgb_representations = [
[100, 20, 50], ['100', 20, 50], ['100', '20', '50'], {'red': 200, 'green': 20, 'blue': 50},
{'r': '50', 'g': 0.2, 'b': 100}
]
colors = [
Color('rgb', representation) for representation in valid_rgb_representations
]
For hex
color space:
Code
from colors_accessibility import Color
# All representations below are valid and will be processed correctly.
valid_hex_representations = [
'1ad', '#1ad', '1ac4bb', '#1ac4bb', ['1ad'], ['#11aabb'],
{'hex': '1ac4ba'}
]
colors = [
Color('hex', representation) for representation in valid_hex_representations
]
For hsl
color space:
Code
from colors_accessibility import Color
# All representations below are valid and will be processed correctly.
valid_hsl_representations = [
[120, 0.1, 0.2], ['120', 20, 0.1], ['120', '20', '0.1'],
{'hue': '0.89', 'saturation': 20, 'lightness': '0.1'}
]
colors = [
Color('hsl', representation) for representation in valid_hsl_representations
]
Conversions
Color
class provides functionality of converting between rgb
, hsl
and hex
color spaces.
We can both calculate color values in different color space an
Code
from colors_accessibility import Color
color = Color('rgb', {'red': 170, 'green': 0.23, 'blue': 0})
print(color)
# Calculate coordinates in different color spaces:
hex_color_values = color.to_hex()
print(hex_color_values)
hsl_color_values = color.to_hsl()
print(hsl_color_values)
# Set color to different color space:
color.set_to('hex')
print(color)
Relative luminance
We also have an ability to calculate relative luminance of a color.
Code
from colors_accessibility import Color
color = Color('rgb', {'red': 170, 'green': 0.23, 'blue': 0})
relative_luminance = color.relative_luminance()
print(relative_luminance)
Single space representation
We can get color representations for the specified color. We can choose from: rgb
, hsl
, hex
and all
. With all
we get representation for all three color spaces.
Code
from colors_accessibility import Color
color = Color('rgb', [120, 53, 89])
print(color)
representation = color.get_representations('hex')
print(representation
Color representations
Here we get the representations for all three spaces.
Code
from colors_accessibility import Color
color = Color('rgb', [120, 53, 89])
print(color)
representation = color.get_representations('all')
print(representation.rgb)
print(representation.hex)
print(representation.hsl)
Values to dictionary
We can format the color representation to a dictionary.
Code
from colors_accessibility import Color
color = Color('rgb', [120, 50, 0.3])
print(color.color_values)
values_dictionary_representations = color.to_dict()
print(values_dictionary_representations)
Accessibility processor
Contrast ratio
We can calculate contrast ratio of a color.
Code
from colors_accessibility import Color
rgb_color = Color('rgb', [120, 53, 89])
hex_color = Color('hex', '#783957')
processor = AccessibilityProcessor(rgb_color, hex_color)
print(processor)
rgb_luminance, hex_luminance = processor.get_luminance_values(
processor.foreground_color,
processor.background_color
)
print(rgb_luminance)
print(hex_luminance)
constrast = processor.calculate_contrast(rgb_luminance, hex_luminance)
print(contrast)
WCAG compliant colors representation
We can get the WCAG compliant colors representation - we get them by tweaking HSL color values till contrast between the two input colors are on adequate levels.
Code
from colors_accessibility import AccessibilityProcessor, Color
from pprint import pprint
rgb_color = Color('rgb', [120, 198, 73])
hex_color = Color('hex', '#783957')
processor = AccessibilityProcessor(rgb_color, hex_color)
wcag_compliant_colors = processor.get_all_wcag_compliant_color()
pprint(wcag_compliant_colors.get('lightness').get('background'))
Further improvements
Here are some of the planned further improvements:
- Add ability to get color representations for more color spaces
- Refactor the code
- Add methods to print color representations
- Add methods for easier getting well formatted color representations
- Add more tests to increase the coverage
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
Built Distribution
File details
Details for the file colors_accessibility-0.0.5.tar.gz
.
File metadata
- Download URL: colors_accessibility-0.0.5.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46e1d4b3d76dd178d0bb8d69c6f9f96d556cf8aeb63fda8ae8056ea19f6c08fd |
|
MD5 | b73245670ec9938eda07fea4e1e9a11d |
|
BLAKE2b-256 | 05b9b189057b60fac0f5f206eee6f77f5402019ed29d5935f76c8dd0673fa4d0 |
File details
Details for the file colors_accessibility-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: colors_accessibility-0.0.5-py3-none-any.whl
- Upload date:
- Size: 16.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af55618aa47fc96eaa397d60774d2e4f08bfa08da2807d47b4002d5bf3f944fd |
|
MD5 | 71b830136115af898167325cd78df95a |
|
BLAKE2b-256 | 4de70fe6a88c99d652505a62a95402c35ac519c3074c942869ad800487656b16 |