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.
Web
All users need adaquate luminance difference between background and text colors in order to perceive and read the text easily.
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:
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 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:
Note: **These values require that "Webkit-Font-Smoothing" be at the device or user agent default, such that added antialiasing is not enabled.
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.
////////////////////////////////////////////////////////////////////////////////
///// 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
/////
////////////////////////////////////////////////////////////////////////////////
Lookup Table by Font Size (left-most column) and Weight (top-most row). Directions:
FontSize | 100 | 200 | 300 | Normal(400) | 500 | 600 | Bold(700) | 800 | 900 |
---|---|---|---|---|---|---|---|---|---|
12 | • | • | • | 100 | 90 | 85 | 80 | 75 | 70 |
14 | • | • | • | 90 | 85 | 80 | 75 | 70 | 65 |
16 | • | • | • | 85 | 80 | 75 | 70 | 65 | 60 |
18 | • | • | 100 | 80 | 75 | 70 | 65 | 60 | 58 |
20 | • | • | 90 | 77 | 70 | 65 | 60 | 58 | 57 |
24 | • | 100 | 80 | 73 | 67 | 60 | 58 | 57 | 56 |
26 | x | 97 | 75 | 70 | 63 | 58 | 55 | 58 | 55 |
28 | • | 93 | 70 | 67 | 60 | 56 | 54 | 56 | 52 |
30 | • | 90 | 65 | 63 | 57 | 54 | 53 | 52 | 51 |
32 | • | 87 | 60 | 60 | 55 | 52 | 50 | 49 | 48 |
34 | • | 83 | 59 | 57 | 53 | 50 | 49 | 48 | 46 |
36 | 120 | 80 | 58 | 53 | 50 | 48 | 47 | 46 | 44 |
40 | 110 | 75 | 56 | 50 | 47 | 46 | 45 | 44 | 43 |
48 | 100 | 70 | 54 | 47 | 46 | 45 | 44 | 43 | 42 |
56 | 90 | 65 | 52 | 43 | 43 | 42 | 42 | 41 | 41 |
64 | 80 | 60 | 50 | 40 | 40 | 40 | 40 | 40 | 40 |
Please see the APCA GitHub Repository for the latest code and methods, at https://github.com/Myndex/SAPC-APCA.
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
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"; }
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.