ColorContrast
Measures how well one colour reads on another, and moves a colour just far enough to read.
public static class ColorContrast
Remarks
Ratios run from 1 (the same colour) to 21 (black on white). The Web Content Accessibility Guidelines ask for 4.5 for text and 3 for glyphs, lines and large text. Transparency is ignored: each colour is measured as if it were opaque.
Members at a glance
EnsureRatio | The colour, made darker or lighter only as far as it takes to reach the ratio on the background - darker on a light background, lighter on a dark one. |
EnsureRatio | The colour, moved towards towards only as far as it takes to reach the ratio on the background. |
IsDark | Whether white text reads better on the colour than dark text does - that is, whether the colour is a dark one. |
Mix | A mix of two opaque colours. |
Ratio | The contrast ratio between two colours, from 1 (none) to 21 (black on white). The order of the two does not matter. |
ReadableTextOn | White or near-black, whichever reads better on the colour. |
RelativeLuminance | How light a colour looks, from 0 (black) to 1 (white). |
GraphicRatio | The ratio the guidelines ask for between a glyph or a line and the colour behind it. |
TextRatio | The ratio the guidelines ask for between text and the colour behind it. |
Methods
EnsureRatio
The colour, made darker or lighter only as far as it takes to reach the ratio on the background - darker on a light background, lighter on a dark one.
- foreground
- The colour wanted.
- background
- The colour it is drawn on.
- ratio
- The contrast it must reach.
Returns. foreground itself when it already reads; otherwise the nearest colour on the way to black or white that does.
EnsureRatio
The colour, moved towards towards only as far as it takes to reach the ratio on the background.
- foreground
- The colour wanted.
- background
- The colour it is drawn on.
- ratio
- The contrast it must reach.
- towards
- The colour to move towards.
Returns. The colour, or towards when nothing short of it reads.
IsDark
Whether white text reads better on the colour than dark text does - that is, whether the colour is a dark one.
- background
- The colour.
Returns. true for a dark colour.
Mix
A mix of two opaque colours.
- first
- The first colour.
- second
- The second colour.
- amount
- How much of the second: 0 gives the first, 1 the second.
Returns. The mix.
Ratio
The contrast ratio between two colours, from 1 (none) to 21 (black on white). The order of the two does not matter.
- first
- One colour.
- second
- The other.
Returns. The ratio.
ReadableTextOn
White or near-black, whichever reads better on the colour.
- background
- The colour the text is drawn on.
Returns. The text colour.
RelativeLuminance
How light a colour looks, from 0 (black) to 1 (white).
- color
- The colour.
Returns. Its relative luminance.
Fields
GraphicRatio
The ratio the guidelines ask for between a glyph or a line and the colour behind it.
TextRatio
The ratio the guidelines ask for between text and the colour behind it.