Package: colors-accessibility
Table of Contents:IntroductionOverviewInstallationUsageColorInput valuesConversionsRelative luminanceSingle space representationColor representationsValues to dictionaryAccessibility processorContrast ratioWCAG compliant colors representationFurther improvementsIntroductionThis library help to handle different color values inputs in three color spaces:RGB,HSLandHEX.
The main functionality is ability to check the contrast between two colors as well adjusting those colors to fit
WCAG 2.1 specification.OverviewThis library provides two main features:A classColorthat represents a color in many
possible color spaces. This class provides following functionalities:Validating color values for each color space - usingValidatorclass.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 classAccessibilityProcessorthat provides following
functionalities:Calculating contrast ratio between two colors.Checking if the contrast between two colors has ratio big enough for levelsAAandAAA,
with ration4.5:1and7:1respectively.Calculating and getting color that isAAorAAAlevel of contrast with the given color - by changing
eitherforegroundcolor,backgroundcolor or both.InstallationTo installcolors-accessibilitylibrary, run the following command:pip install colors-accessibilitycolor-accessibilitylibrary does not have any dependencies.UsageColorColorclass currently handles three color spaces:rgb,hsl, andhex.
This class can process different type of input values. To initialize a color, following inputs are valid:Input valuesForrgbcolor space:Codefrom 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
]Forhexcolor space:Codefrom 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
]Forhslcolor space:Codefrom 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
]ConversionsColorclass provides functionality of converting betweenrgb,hslandhexcolor spaces.
We can both calculate color values in different color space anCodefrom 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 luminanceWe also have an ability to calculaterelative luminanceof a color.Codefrom colors_accessibility import Color


color = Color('rgb', {'red': 170, 'green': 0.23, 'blue': 0})

relative_luminance = color.relative_luminance()
print(relative_luminance)Single space representationWe can get color representations for the specified color. We can choose from:rgb,hsl,hexandall. Withallwe get representation for all three color spaces.Codefrom colors_accessibility import Color


color = Color('rgb', [120, 53, 89])
print(color)

representation = color.get_representations('hex')
print(representationColor representationsHere we get the representations for all three spaces.Codefrom 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 dictionaryWe can format the color representation to a dictionary.Codefrom 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 processorContrast ratioWe can calculate contrast ratio of a color.Codefrom 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 representationWe 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.Codefrom 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 improvementsHere are some of the planned further improvements:Add ability to get color representations for more color spacesRefactor the codeAdd methods to print color representationsAdd methods for easier getting well formatted color representationsAdd more tests to increase the coverage