HypernovaDesktop Components · docs
Binarymission
WPF › Skinning › ColorContrast

ColorContrast

class.NET 6 / 8 / 10.NET Framework 4.8Binarymission.WPF.Skinning

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

EnsureRatioThe 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.
EnsureRatioThe colour, moved towards towards only as far as it takes to reach the ratio on the background.
IsDarkWhether white text reads better on the colour than dark text does - that is, whether the colour is a dark one.
MixA mix of two opaque colours.
RatioThe contrast ratio between two colours, from 1 (none) to 21 (black on white). The order of the two does not matter.
ReadableTextOnWhite or near-black, whichever reads better on the colour.
RelativeLuminanceHow light a colour looks, from 0 (black) to 1 (white).
GraphicRatioThe ratio the guidelines ask for between a glyph or a line and the colour behind it.
TextRatioThe ratio the guidelines ask for between text and the colour behind it.

Methods

EnsureRatio

public static Color EnsureRatio(Color foreground, Color background, double ratio)

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

public static Color EnsureRatio(Color foreground, Color background, double ratio, Color towards)

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

public static bool IsDark(Color background)

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

public static Color Mix(Color first, Color second, double amount)

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

public static double Ratio(Color first, Color second)

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

public static Color ReadableTextOn(Color background)

White or near-black, whichever reads better on the colour.

background
The colour the text is drawn on.

Returns. The text colour.

RelativeLuminance

public static double RelativeLuminance(Color color)

How light a colour looks, from 0 (black) to 1 (white).

color
The colour.

Returns. Its relative luminance.

Fields

GraphicRatio

public const double GraphicRatio

The ratio the guidelines ask for between a glyph or a line and the colour behind it.

TextRatio

public const double TextRatio

The ratio the guidelines ask for between text and the colour behind it.