contrast function

colourettu.contrast(colour_1, colour_2)[source]

Determines the contrast between two colours.


Contrast the difference in (perceived) brightness between colours. Values vary between 1:1 (a given colour on itself) and 21:1 (white on black).

To compute contrast, two colours are required.

>>> colourettu.contrast("#FFF", "#FFF") # white on white
>>> colourettu.contrast(c1, "#000") # black on white
>>> colourettu.contrast(c4, c5)

contrast can also be called on an already existing colour, but a second colour needs to be provided:

>>> c4.contrast(c5)


Uses the formula:

\[contrast = \frac{lum_1 + 0.05}{lum_2 + 0.05}\]

Use of Contrast

For Basic readability, the ANSI standard is a contrast of 3:1 between the text and it’s background. The W3C proposes this as a minimum accessibility standard for regular text under 18pt and bold text under 14pt. This is referred to as the A standard. The W3C defines a higher AA standard with a minimum contrast of 4.5:1. This is approximately equivalent to 20/40 vision, and is common for those over 80. The W3C define an even higher AAA standard with a 7:1 minimum contrast. This would be equivalent to 20/80 vision. Generally, it is assumed that those with vision beyond this would access the web with the use of assistive technologies.

If needed, these constants are stored in the library.

>>> colourettu.A_contrast
>>> colourettu.AA_contrast
>>> colourettu.AAA_contrast

I’ve also found mention that if the contrast is too great, this can also cause readability problems when reading longer passages. This is confirmed by personal experience, but I have been (yet) unable to find any quantitative research to this effect.