Basics

Select font characteristics and colors for both the background and the text to provide enough contrast for readability

Use tools to evaluate font size, font stroke width, background color, font color, and nearby colors and adjust the attribute and color of those elements to achieve good visual contrast.

Status

Platform

Web

Programming Language

How It Solves User Need

All users need adaquate luminance difference between background and text colors in order to perceive and read the text easily.

Related Guidelines

Detailed Description

Select font characteristics and colors for both the background and the text to provide enough contrast for readability

Understanding Contrast Perception

As used on this page, Visual Contrast means the perceived difference between two colors displayed on a computer monitor, such as the color of the text against the color of the background. Our perception of contrast is a function of the Human Visual System (HVS) which includes the eye, optic nerve, and several areas of the brain that are either dedicated to processing vision, or that share vision processing with other senses such as hearing.

The eye consists of an optical portion including a lens, and a light sensitive portion known as the retina. Muscles in the eye adjust the shape of the lens to adjust the focus of incoming light, so that an image of the world around us is focused onto the retina. Light sensitive cells on the retina then convert light into electrical impulses that are sent to the brain's vision processing center, called the visual cortex.

Our perception of contrast and ultimately the readability of text is affected by many interdependant factors, including:

  • The user's personal vision quality in terms of sharpness, glare, and contrast capability
  • Font weight or stroke width, and font size
  • The lightness or darkness difference between the background and the text
  • The overall lightness or darkness of the screen and the light in the room as this affect the eye's light or dark adaptation to the environment
  • The padding and spacing around the text which affects the eye's local adaptation to the text area
  • The hue or color difference between the background and text, not including the lightness difference mentioned above

All of these interdependant factors affect the perception of contrast and readability. As we are concerned with readability on computer screens, we can made some assumptions about a range of brightness, and a typical viewing environment. This provides a baseline standard for all users for best readability of content, or a minimum level of legibility for non-content. Then, we can set how different impairments require different adjustments or user customization to accomodate the neads of each individual user.

For instance, a user with poor "acuity", that is, poor sharpness where they cannot focus on small text, will need to be able to zoom the text larger. A user with poor contrast sensitivity may have good sharpness, but unable to perceive colors that are too similar in lightness. Yet another user with a glare problem may need lower contrast, and/or a reduction in blue, to reduce glare or halos interfereing with readability. Those with a color vision problem (sometimes called 'color blind') may have difficulty detecting contrast or light for certain colors, such as deep red.

Predicted Contrast

Predicted contrast is reported as a percentage using the methods in this guideline, based on the CSS color values in sRGB colorspace, and with device default antialiasing**. The TESTS section has a lookup table for specific font and contrast combinations, but as a general guide:

  • 25% is the point of invisibility (i.e., no perceptable contrast) for many people with contrast related impairments. Designers should assume that contrsts lower than this may be invisible to some users.
  • 60% for Large, Bold Headlines where the major stroke width is at least 8px (6pt), or Non-text elements that are at least a solid 8x8px square such as buttons.
  • 60% For Bold Text no less than 16px (12pt) or non-text elements no less than 3px in the thinnest dimension.
  • 80% Normal Weight Text no less than 16px (12pt) or non-text with a minimum stroke of 2px.
  • 100% 300 Weight Text no less than 16px (12pt) or non-text with a minimum stroke of 1px.

Note: **These values require that "Webkit-Font-Smoothing" be at the device or user agent default, such that added antialiasing is not enabled.

Related Methods

Use default fonts and colors.

Alternate Meanings

It's useful to point out that the term “Visual Contrast” can also be used to describe the differences of other visual perceptions: Contrast of size difference, contrast of position, contrast of speed or motion, etc. but these are not covered in this guideline at this time.

Code Samples

Select font characteristics and colors for both the background and the text to provide enough contrast for readability

Example Code for Test Tool

APCA GitHub Repository


////////////////////////////////////////////////////////////////////////////////
/////	Functions to parse color values and determine SAPC contrast
/////	
/////	Please see the APCA GitHub repository for the latest code
/////   and methods, at: https://github.com/Myndex/SAPC-APCA
/////
////////////////////////////////////////////////////////////////////////////////

Tests

Select font characteristics and colors for both the background and the text to provide enough contrast for readability

Procedure

  1. Using source code inspection of the text and background elements, obtain representaive sRGB values for the forground text (sRGBtxt) and the background color (sRGBbg).
    • If an “eye dropper” type tool is used it must report the true CSS value of the color, not the displayed value.
    • Steps #2 and #3 refer to an automated contrast prediction tool.
    • Note: Since this is a new formula, we have provided a link to a tool for users to comment on this new algorithm. sRGB Advanced Perceptual Contrast (SAPC) Visual Contrast Demo
  2. Enter the background sRGBbg and text sRGBtxt CSS color values in the background and text input fields of the contrast tool. Important: do not swap the background or text colors in the tool entry fields.
  3. Calculate the predicted contrast percentage of sRGBtxt to background sRGBbg.
  4. Compare this calculated value against the lookup table "Accessible Contrast by Font Size and Weight".
  5. Check that the absolute value of the predicted contrast percentage meets or exceeds the required value for the font weight and size.

Accessible Contrast by Font Size and Weight

Lookup Table by Font Size (left-most column) and Weight (top-most row). Directions:

Font
Size
100200300Normal
(400)
500600Bold
(700)
800900
121009085807570
14908580757065
16858075706560
18100807570656058
2090777065605857
2410080736760585756
26x9775706358555855
289370676056545652
309065635754535251
328760605552504948
348359575350494846
361208058535048474644
401107556504746454443
481007054474645444342
56906552434342424141
64806050404040404040

Expected Results

Resources

Select font characteristics and colors for both the background and the text to provide enough contrast for readability

SAPC Math

SAPC is the sRGB Advanced Perceptual Contrast prediction algorithm. The math assumes the use of the web standard sRGB colorspace.

The simplified steps are:

  • Convert the sRGB background and text colors to luminance Ybg and Ytxt
    • Convert from 8 bit integer to decimal 0.0-1.0
    • Linearize (remove gamma) by applying a ^2.218 exponent
    • Apply sRGB coefficients and sum to Y

      Image Version:


      MathML version
      Y = R l i n 0.2126 + G l i n 0.7152 + B l i n 0.0722

      SVG Version
  • Determine which is brighter for contrast polarity
  • Apply pre-process modules (Black level soft clamp)
  • Apply power curve exponents for perceptual lightness
    • For dark text on a light background, use ^0.43 for the text and ^0.39 for the background.
    • For light text on a dark background, use ^0.43 for the text and ^0.5 for the background.
  • Find difference between text and background then multiply by phi (1.618) to scale output contrast value.

Please see the APCA GitHub Repository for the latest code and methods, at https://github.com/Myndex/SAPC-APCA.

Basic SAPC Math Pseudocode

In the sRGB colorspace, using CSS color values as integers, with a background color sRGBbg and a text color sRGBtxt convert each channel to decimal 0.0-1.0 by dividing by 255, then linearize the gamma encoded RGB channels by applying a simple exponent.

	Rlinbg = (sRbg/255.0) ^ 2.218
	Glinbg = (sGbg/255.0) ^ 2.218
	Blinbg = (sBbg/255.0) ^ 2.218
	Rlintxt = (sRtxt/255.0) ^ 2.218
	Glintxt = (sGtxt/255.0) ^ 2.218
	Blintxt = (sBtxt/255.0) ^ 2.218

Then find the relative luminance (Y) of each color by applying the sRGB/Rec709 spectral coefficients and summing together.

	Ybg = 0.2126 * Rlinbg + 0.7156 * Glinbg + 0.0722 * Blinbg
	Ytxt = 0.2126 * Rlintxt + 0.7156 * Glintxt + 0.0722 * Blintxt 

Predicted Contrast

The Predicted Visual Contrast (SAPC) between a foreground color and a background color is expressed as a percentange and is calculated by:

//  Constants for Basic SAPC Version:

sRGBtrc = 2.218;	// Linearization exponent**

normBGExp = 0.38;	// Constants for APCA Power Curve Exponents.
normTXTExp = 0.43;	// One pair for normal text, dark text on light BG
revBGExp = 0.5;		// and one for reverse, light text on dark BG
revTXTExp = 0.43;

scale = 1.618;          // Scale output for easy to remember levels

blkThrs = 0.02;		// Level that triggers the soft black clamp
blkClmp = 1.33;		// Exponent for the soft black clamp curve

//  Calculate Predicted Contrast and return a string for the result

if Ybg > Ytxt then {
    Ytxt = (Ytxt > blkThrs) ? Ytxt : Ytxt + abs(Ytxt - blkThrs) ^ blkClmp;
    SAPC = ( Ybg ^ normBGExp - Ytxt ^ normTXTExp ) * scale;
    return (SAPC < 0.12 ) ? "LOW" : str(SAPC * 100) + " Lc";
  } else {
    Ybgg = (Ybg > blkThrs) ? Ybg : Ybg + abs(Ybg - blkThrs) ^ blkClmp;
    SAPC = ( Ybg ^ revBGExp - Ytxt ^ revTXTExp ) * scale;
    return (SAPC > -0.12 ) ? "-LOW" : str(SAPC * 100) + " Lc";
}  
    
Notes:

Predicted contrast less than 12% is clamped to zero to simplyfy the math.

**We will use the simple exponent, and not the piecewise sRGB transfer curve, as we are emulating display gamma and not performing image processing.

The “^” character is the exponentiation operator.


Glossary

Changelog

Select font characteristics and colors for both the background and the text to provide enough contrast for readability