How Color Contrast Is Calculated
Understand how WCAG calculates color contrast, how relative luminance works and how to evaluate accessible color combinations.
Color contrast is a measurable relationship between two colors that determines how easily text, icons and other visual elements can be distinguished from their background. In web accessibility, contrast is especially important because insufficient contrast can make content difficult to read for people with low vision, color vision differences or changing viewing conditions.
Modern accessibility guidelines use a mathematical contrast ratio rather than subjective judgments about whether two colors look different enough. Understanding how that ratio is calculated helps developers choose colors deliberately, interpret accessibility checker results and build interfaces that remain readable across different screens and environments.
What Is Color Contrast?
Color contrast describes the difference in perceived brightness between two colors. For accessibility purposes, the relevant calculation is based on relative luminance rather than directly comparing RGB channel values.
A contrast ratio compares the relative luminance of the lighter color with the relative luminance of the darker color. The result ranges from 1:1, when the two colors have the same luminance, to 21:1, which is the maximum possible contrast between black and white.
Relative Luminance
Relative luminance is a normalized measurement of how bright a color appears under the mathematical model used by WCAG. It is calculated from the red, green and blue components of a color after converting the sRGB channel values from their encoded form into linear light values.
The important point is that RGB numbers cannot simply be averaged or compared directly. For example, a color with RGB components of 200, 200 and 200 is not twice as bright as a color with components of 100, 100 and 100. The calculation must first account for the nonlinear encoding used by sRGB.
The sRGB Conversion
For an sRGB channel value normalized to a range from 0 to 1, the WCAG luminance calculation applies a transfer function. If the normalized channel value is at or below 0.04045, it is divided by 12.92. Otherwise, the value is transformed using a power function.
c_linear = ((c_sRGB + 0.055) / 1.055) ^ 2.4The same conversion is applied independently to the red, green and blue channels. This process is called linearization because it converts the encoded sRGB values into values that can be combined meaningfully for the luminance calculation.
Calculating Relative Luminance
After the RGB channels have been linearized, they are combined using weighted coefficients. The coefficients reflect the contribution of each color channel to the relative luminance model used by WCAG.
L = 0.2126R + 0.7152G + 0.0722BGreen contributes the most to the luminance value, followed by red and then blue. This is why equal numerical changes in different RGB channels do not necessarily produce the same change in perceived brightness.
Calculating the Contrast Ratio
Once the relative luminance of both colors has been calculated, the contrast ratio is found by comparing the lighter color with the darker color.
Contrast Ratio = (L1 + 0.05) / (L2 + 0.05)L1 represents the higher relative luminance and L2 represents the lower relative luminance. The lighter color is always placed in the numerator, so the resulting ratio is never below 1:1.
The constant 0.05 is part of the WCAG contrast calculation. It prevents extremely dark luminance values from producing an unsuitable mathematical relationship and ensures that the ratio behaves consistently across the full luminance range.
Example: Black and White
Black has a relative luminance of 0, while white has a relative luminance of 1. Applying the contrast formula produces the maximum possible contrast ratio.
(1 + 0.05) / (0 + 0.05) = 21Therefore, black text on a white background has a contrast ratio of 21:1. White text on black has the same ratio because contrast is a relationship between the two colors and the lighter luminance is always used as L1.
Why RGB Values Are Not Enough
RGB is useful for describing colors in digital interfaces, but raw RGB differences do not directly represent accessibility contrast. A difference of 50 in the blue channel does not have the same effect on luminance as a difference of 50 in the green channel.
For example, changing a color from RGB 50, 50, 50 to RGB 100, 100, 100 changes every channel by the same numerical amount, but the resulting luminance relationship is nonlinear. Accessibility calculations account for this by converting the channels into linear values before combining them.
Why Hue Is Not the Main Factor
Two colors can have completely different hues while still having poor contrast if their luminance values are similar. Conversely, two colors with similar hues can have excellent contrast if one is substantially lighter than the other.
Changing blue text to purple text does not automatically improve accessibility. The important question is whether the resulting foreground and background have enough luminance separation.
Saturation also does not guarantee sufficient contrast. Bright, saturated colors can still fail against particular backgrounds, while relatively muted colors can sometimes provide excellent contrast when their luminance values are sufficiently separated.
WCAG Contrast Requirements
The Web Content Accessibility Guidelines define minimum contrast requirements for different types of content. For normal text, WCAG 2 generally uses a minimum contrast ratio of 4.5:1 for Level AA. Large text has a lower threshold of 3:1.
For enhanced Level AAA requirements, normal text generally needs 7:1 contrast, while large text needs 4.5:1. These thresholds are intended to make text sufficiently distinguishable for a wide range of users and viewing conditions.
| Content Type | WCAG AA | WCAG AAA |
|---|---|---|
| Normal text | 4.5:1 | 7:1 |
| Large text | 3:1 | 4.5:1 |
| User interface components | 3:1 | 3:1 |
Large Text
The large-text threshold exists because larger characters are generally easier to distinguish even when the contrast is lower. Under WCAG 2 definitions, large text is generally at least 18 point regular or 14 point bold, with CSS pixel equivalents commonly used for web content being approximately 24px regular or 18.67px bold.
Font size and weight therefore matter when evaluating contrast. A color combination that passes for a large heading may fail when the same color is used for normal body text.
Text and User Interface Components
Contrast requirements are not limited to paragraphs and headings. WCAG also addresses meaningful graphical objects and user interface components. A control boundary, icon or state indicator may need sufficient contrast against adjacent colors when its appearance is necessary to identify or operate the component.
This is especially important for buttons, form controls, input boundaries, icons, focus indicators and other interactive elements. Developers should evaluate the visual states users actually encounter rather than checking only static text.
Foreground and Background
Contrast is evaluated between two colors in relation to their visual roles. Usually one color is the foreground, such as text or an icon, and the other is the background behind it.
For an opaque foreground over an opaque background, the calculation is straightforward. Modern interfaces, however, often use transparency, gradients, images and layered surfaces. In those situations, the effective contrast can depend on what is actually visible behind the foreground.
Transparency and Alpha
A semi-transparent color does not have one fixed contrast ratio against every possible background. Its final displayed color depends on compositing with the background beneath it.
For example, white text with reduced opacity may appear acceptable over a dark background but become difficult to read when the same component is placed over a lighter surface. Accessibility testing should therefore consider the rendered result rather than relying only on the declared CSS color.
Gradients and Images
A gradient does not have one universal contrast ratio because its background luminance can change across the area occupied by the text. Text placed over one part of a gradient may pass while text over another part may fail.
Images create a similar problem. A text label placed over a photograph can encounter both dark and light regions as the image changes. Designers may need to add an overlay, use a solid surface behind the text, adjust the text color or otherwise control the visual context.
Contrast and Anti-Aliasing
Thin or small text can appear lighter than its declared color because browsers render glyph edges using anti-aliasing. This can make borderline contrast combinations more difficult to read in practice.
For this reason, it is better not to design important text around a ratio that barely passes. A reasonable margin provides greater resilience across different rendering environments, display technologies and viewing conditions.
Contrast for Icons
Icons can communicate information, identify actions or represent controls. When an icon is essential for understanding or operating an interface, its contrast should be considered alongside other meaningful graphical content.
A decorative icon does not necessarily have the same accessibility requirements as an informative or interactive icon. Developers should first determine whether the graphic conveys information or functionality before deciding how it should be evaluated.
Focus Indicators
Keyboard focus indicators are particularly important because users need to see which control currently has focus. A focus indicator that blends into the surrounding interface can make keyboard navigation difficult even when the text itself has excellent contrast.
When designing focus styles, consider contrast against both the component and the adjacent page background. A visible focus state should remain recognizable across the different surfaces where the component can appear.
Contrast in Dark Mode
Dark interfaces do not automatically have good contrast. A dark-gray background with slightly lighter gray text can produce a weak ratio even though the interface may look intentionally subtle.
The same luminance calculation applies to dark mode as to light mode. Designers should test the actual foreground and background colors rather than assuming that a dark theme is accessible simply because it uses light text.
Brand Colors
Brand palettes are often created for visual identity rather than accessibility. A primary brand color may work well for large headings or decorative elements but fail when used as body text on a light background.
Instead of abandoning a brand color, teams can define accessible variants for different roles. A brighter brand color can remain available for backgrounds and decorative elements while a darker companion shade is used for text.
Color Contrast and Color Vision
Contrast ratios are not a complete model of accessibility for every visual condition. People with color vision deficiencies may have difficulty distinguishing colors that differ mainly by hue even when the luminance relationship is acceptable.
Interfaces should therefore avoid communicating important information through color alone. Text labels, icons, patterns, borders and other visual cues can provide additional information when color is used to represent status, categories or meaning.
Contrast in Design Systems
A design system should define accessible color relationships instead of leaving contrast decisions to individual components. Semantic color tokens such as text-primary, text-secondary, surface-primary, surface-muted and border-focus can be tested systematically.
This makes accessibility easier to maintain as an interface grows. When a shared color token changes, teams can check all affected combinations instead of manually reviewing every page.
Choosing Colors by Role
Instead of choosing foreground and background colors independently, define them as semantic roles or tested pairs. A text color should have a verified relationship with each background where it can appear.
For example, a secondary text token may pass on a primary surface but fail on a tinted card background. A design system should document the combinations that are intended to be used together.
Automated Contrast Checking
A contrast checker can calculate the ratio quickly and report whether a color pair meets selected WCAG thresholds. Automated tools are useful during development because they eliminate manual arithmetic and allow many combinations to be tested consistently.
Automated contrast checking does not replace accessibility testing. A numerical result cannot fully determine whether color communicates meaning by itself, whether a gradient remains readable everywhere or whether an interface is usable under different visual conditions.
Using a Color Contrast Checker
A Color Contrast Checker can be used to enter a foreground and background color and calculate their contrast ratio. This is useful when testing text colors, buttons, borders, icons and other visual relationships.
A WCAG Contrast Checker can focus the evaluation on accessibility thresholds and help determine whether a color pair meets common AA or AAA requirements. A Gradient Contrast Checker is useful when contrast changes across a gradient and a single solid-color calculation is not sufficient.
Improving a Failing Color Pair
If a color combination fails, the simplest solution is often to change the foreground color, background color or both. Darkening text on a light background or lightening text on a dark background usually increases the contrast ratio.
Changing hue may help, but only when the change produces enough luminance separation. Small color adjustments should always be retested because the relationship between RGB values and luminance is nonlinear.
It is also useful to preserve the intended visual hierarchy. A secondary text color can be adjusted to pass accessibility requirements while still remaining visually less prominent than primary text.
Color Conversion and Contrast
Color conversion tools can help developers move between HEX, RGB, HSL and other representations while refining a palette. However, converting a color from one representation to another does not automatically improve accessibility.
The important step is to evaluate the resulting colors using their relative luminance and contrast ratio. A color can look different when represented in another color space without becoming more accessible.
Common Contrast Mistakes
Several recurring mistakes make accessibility testing less reliable. Most come from treating contrast as a simple visual property instead of a calculated relationship between rendered colors.
- Using raw RGB differences instead of luminance-based contrast calculations.
- Testing only the default state while ignoring hover, active and focus states.
- Assuming visually different hues automatically provide strong contrast.
- Checking a gradient as if it were a single solid color.
- Ignoring transparency and the background beneath semi-transparent elements.
- Using the same muted text color on every surface without testing each combination.
- Relying on color alone to communicate status, errors or categories.
- Designing text colors exactly at the minimum threshold without a reasonable safety margin.
Best Practices
- Calculate contrast using relative luminance rather than raw RGB differences.
- Target comfortable contrast margins instead of barely meeting the minimum.
- Test every important foreground and background combination used by a component.
- Check normal text and large text against the appropriate WCAG thresholds.
- Test interactive states, including hover, focus and active states.
- Evaluate gradients, images and transparent layers using their rendered appearance.
- Do not communicate important information through color alone.
- Include contrast checks in the design-system and component-development workflow.
- Recheck shared color tokens whenever their values change.
- Test interfaces in both light and dark themes.
Frequently Asked Questions
What is the contrast ratio between black and white?
The contrast ratio between black and white is 21:1, which is the maximum contrast ratio defined by the WCAG calculation.
Why is WCAG contrast not based directly on RGB values?
RGB channel values use nonlinear sRGB encoding and do not directly represent luminance. WCAG converts the channels into linear values and calculates relative luminance before determining the contrast ratio.
What contrast ratio is required for normal text?
For WCAG 2 Level AA, normal text generally requires a contrast ratio of at least 4.5:1. Level AAA generally requires 7:1.
What contrast ratio is required for large text?
Large text generally requires at least 3:1 for WCAG 2 Level AA and 4.5:1 for Level AAA.
Can two different colors have poor contrast?
Yes. Different hues can have similar relative luminance, resulting in a low contrast ratio. A visible difference in hue does not guarantee sufficient luminance contrast.
Does a higher contrast ratio always mean a better design?
A higher ratio provides greater luminance separation, but accessibility also depends on typography, state visibility, visual hierarchy and how information is communicated. Contrast should be considered as part of the overall interface design.
Do gradients have a single contrast ratio?
Not necessarily. A gradient can contain different luminance values across its area, so text placed over it may have different contrast ratios at different positions.
Is color contrast enough for accessibility?
No. Contrast is important, but accessible design also requires considerations such as keyboard navigation, focus visibility, text alternatives, semantic structure and avoiding color-only communication.
Helpful Color Tools
A Color Contrast Checker calculates the contrast ratio between foreground and background colors, a WCAG Contrast Checker helps evaluate color pairs against common accessibility thresholds, a Gradient Contrast Checker helps inspect contrast where backgrounds vary across a gradient, an Accessible Color Palette Generator can create palette combinations designed around accessibility requirements, and a Color Converter helps convert colors between formats while refining a design system.
Conclusion
Color contrast is calculated by converting sRGB color channels into linear values, combining them into relative luminance and comparing the lighter and darker luminance values with the WCAG contrast formula. The resulting ratio provides an objective way to evaluate whether foreground and background colors have enough luminance separation for common accessibility requirements.
For developers, the most important lesson is that contrast is not determined by raw RGB differences or by how different two colors appear at first glance. Luminance, typography, component states, transparency, gradients and visual context all matter. Using contrast checkers during design and development makes it easier to build interfaces that remain readable, recognizable and usable across a wider range of users and environments.