tituli.color¶
Colours, WCAG contrast, and the ink-for-this-background decision.
Everything here is pure arithmetic on (r, g, b[, a]) tuples in 0–255, so it
costs nothing to import and is easy to test.
>>> contrast_ratio((255, 255, 255), (0, 0, 0))
21.0
>>> ink_for(luminance=0.9) # light background -> dark ink
(17, 17, 17)
>>> ink_for(luminance=0.1) # dark background -> light ink
(255, 255, 255)
Functions
|
Contrast of |
|
WCAG contrast ratio between two colours (1..21). |
|
Pick the ink (light or dark) with more contrast against |
|
Mean relative luminance of a pixel sample. |
|
Linear blend, |
|
Whether |
|
Accept |
|
WCAG relative luminance in 0..1. |
|
Same colour with opacity |
- tituli.color.contrast_from_luminance(ink, luminance)[source]¶
Contrast of
inkover a background of known relative luminance.- Return type:
- tituli.color.contrast_ratio(a, b)[source]¶
WCAG contrast ratio between two colours (1..21).
- Return type:
>>> round(contrast_ratio("#777", "#fff"), 2) 4.48
- tituli.color.ink_for(*, luminance, light=(255, 255, 255), dark=(17, 17, 17))[source]¶
Pick the ink (light or dark) with more contrast against
luminance.
- tituli.color.mean_luminance(pixels)[source]¶
Mean relative luminance of a pixel sample.
- Return type:
- tituli.color.needs_scrim(ink, luminance, *, minimum=4.5)[source]¶
Whether
inkover that background falls short ofminimumcontrast.- Return type:
>>> needs_scrim((255, 255, 255), luminance=0.5) True >>> needs_scrim((255, 255, 255), luminance=0.02) False
- tituli.color.parse_color(color)[source]¶
Accept
"#rgb","#rrggbb","#rrggbbaa", a Pillow name or a tuple.>>> parse_color("#fff") (255, 255, 255, 255) >>> parse_color((10, 20, 30)) (10, 20, 30, 255)