Skip to main content

Colour related functions

Project description

Colourettu is a small collection of colour functions in Python, that can be used to determine the (relative) luminosity of a colour and the contrast between two colours.

Installation

pip install colourettu

Note on Spelling

I have used the Canadian/British spelling of colour through this and the code.

License

Colourettu is licensed under the MIT license.

Colour Class

Colours are created by calling the colour class. Colour values can be provided via 3 or 6 digit hex notation, or providing a list or a tuple of the Red, Green, and Blue values (as integers).

import colourettu

c1 = colourettu.colour()        # defaults to #FFF
c2 = colourettu.colour("#eee")  # equivalent to #EEEEEE
c3 = colourettu.colour("#456bda")
c4 = colourettu.colour([3, 56, 129])
c5 = colourettu.colour((63, 199, 233))

The value of each channel can be pulled out:

>>> c4.red()
3
>>> c4.green()
56
>>> c4.blue()
129

You can also get the colour back as either a hex value, or a rgb tuple:

>>> c2.hex()
'#EEEEEE'
>>> c2.rgb()
(238, 238, 238)

(Relative) Luminance

Luminance is a measure of how ‘bright’ a colour is. Values are normalized so that the Luminance of White is 1 and the Luminance of Black is 0. That is to say:

>>> colourettu.luminance("#FFF")    # white
0.9999999999999999
>>> colourettu.luminance("#000")    # black
0.0

luminance can also be called on an already existing colour:

>>> c3.luminance()
0.2641668488934239
>>> colourettu.luminance(c4)
0.08007571268096524

Contrast

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
1.0
>>> colourettu.contrast(c1, "#000") # black on white
20.999999999999996
>>> colourettu.contrast(c4, c5)
4.363552233203198

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

>>> c4.contrast(c5)
4.363552233203198

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
3.0
>>> colourettu.AA_contrast
4.5
>>> colourettu.AAA_contrast
7.0

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.

Changelog

Changelog for Colourettu

1.0.0 – unreleased

  • documentation is now online at minchin.ca/colourettu

  • convert Readme and Changelog from Markdown to ReStructured Text

  • colourettu.color (note, no u) no longer an alais for colourettu.colour (with the u)

0.1.1 – December 11, 2014

  • include extra files so module can install off of pip

0.1.0 – December 11, 2014

  • first working version

  • includes base colour class, and (relative) luminance and contrast functions

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

colourettu-1.0.0.zip (13.0 kB view hashes)

Uploaded Source

Built Distribution

colourettu-1.0.0-py3-none-any.whl (8.9 kB view hashes)

Uploaded Python 3

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