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: