From 4.10 to 7.18: how the contrast between text and background is calculated

Accessibility | Contrast ratio

This post started from a single row in a table.

I was going through a list of text colors that had been proposed for replacement in order to meet the contrast requirements. The list was produced by a contrast checking tool: a tool that takes a text color and a background color, and returns a number and a verdict for each pair. Each row showed the current color and the proposed color. One of the rows looked like this:

Color Text color Background color The number received What was written next to it
Current #FF0000 #081F40 4.10 Does not meet the threshold
Proposed #FF8A80 #081F40 7.18 Meets the threshold

This is how the two colors look on this background:

2 color pairs, each with its contrast ratio
  • #FF0000 on #081F40

    Contrast ratio 4.10:1

    • 3:1Passes
    • 4.5:1Fails
    • 7:1Fails
  • #FF8A80 on #081F40

    Contrast ratio 7.18:1

    • 3:1Passes
    • 4.5:1Passes
    • 7:1Passes

I could have approved the proposal, marked the problem as fixed and moved on.

But something made me curious.

What do the numbers 4.10 and 7.18 actually measure? Who decided that 4.10 is not enough? And how can it be that the strong red, the one that stands out most to the eye, is the one that failed?

From there, a single row in a table turned into a short inquiry, at the end of which I understood how the number is calculated.

I discovered that behind the number there is a fixed formula from an international standard, that it measures lightness and not hue, and that it can be calculated from the color code alone.

And I realized something I had not expected:

A color that looks prominent is not necessarily a light color, and the number the checking tool displays measures lightness, not prominence.

“I did not understand a word”

The first answer I received to my question was correct, and full of terms: contrast ratio, relative luminance, gamma correction, Level AA.

I replied with one sentence:

“I did not understand a word.”

That was not exactly true, of course. I am not starting from zero, and the terms were not entirely new to me. But I wanted an explanation that starts from the beginning and assumes nothing, and that sentence got it. The second explanation started without terms; only after it did I return to the terms, and by then they connected to one another.

I am writing this post as a record of learning, and so in the same order: first the simple explanation, then the formula, and only at the end the professional names.
You do not need to know the terms to start. And if a code like #FF8A80 is completely new — at the end of the post there is an appendix that starts from zero. You can jump to it and come back to the story.

Part one — what happened in the inquiry, step by step

1. One row, and a number I could not read

For each text color the checking tool displayed three things: the color code of the text and of the background, a number, and a short verdict — “meets the threshold” or “does not meet the threshold”.

These are the results I actually received: #FF0000 on #081F40 got 4.10 and did not meet the threshold; #FF8A80 on the same background got 7.18 and met it.

The two colors belong to the same family. The first is a full, strong red; the second is a light red-pink. The background of both is the same: a very dark blue. And yet one failed, and the other passed by a wide margin.

The question I asked was simple: what does each of the numbers mean, and what do they define?

2. The same number, simply put

The explanation I understood was short: the number says how easy it is to read text on its background.

It compares the lightness of the text with the lightness of the background. The bigger the difference between them, the higher the number and the more readable the text.

  • 1 is the lowest number: the text and the background are the same color, and nothing can be seen.
  • 21 is the highest number: black on white.

Normal text needs a number of at least 4.5.

With these three numbers my row started to make sense:

Text color The number Against 4.5 The explanation
#FF0000, strong red 4.10 Below the threshold The strong red stands out, but it is fairly dark; on a dark background the difference is too small
#FF8A80, light red-pink 7.18 Above the threshold It is much lighter, so the difference from the background is large

Here I discovered something fundamental: prominent and light are not the same thing. A full red catches the eye, but in this calculation it is closer to a dark color than to a light one.

🔍

What is the number called? The professional name is Contrast Ratio. It is usually written as a ratio, for example 4.10:1. It is calculated from another value, Relative Luminance, which is calculated for each color separately. Two colors in different hues with the same relative luminance will get, against the same background, exactly the same number.

3. Who sets the number?

My next question was: who sets the number, and how?

The answer surprised me: no one sets it. It is calculated. The formula is written in an international standard called WCAG, and the standard is published by W3C, the organization that publishes the Web standards. The number is derived from the color code alone, so any tool that implements the formula should arrive at the same result.

The 4.5 threshold also comes from the same standard. It is not the personal taste of whoever wrote the tool.

4. Three steps, from the color code to the number

Step one: split the color into three channels. Every color code is made of three parts: red, green and blue. Each part is a number between 0 and 255.

Color code Red Green Blue
#FF0000 255 0 0
#FF8A80 255 138 128
#081F40 8 31 64

Step two: calculate a lightness for each color, a number between 0 and 1. 0 is black and 1 is white. The number describes how much light the screen emits for this color: when all three channels are off there is no light at all, and when all three are fully on the result is 1. This calculation has two corrections:

  • A correction for the screen: the value in the color code, a number between 0 and 255, is not yet the amount of light. It is an instruction to the screen, and the screen does not turn it into light in direct proportion: the value 128 is half of 255, but the light it produces is only about a fifth, not “half as light”. That is why each channel first goes through a fixed conversion, which turns the value in the code into the amount of light: a number between 0 and 1.
  • A correction for the channel: the three channels do not contribute to lightness to the same degree. Green gets the largest weight, about 72%; red about 21%; and blue about 7%. The three weights add up to exactly 1, which is why white, where all three are fully on, comes out as 1.
Text
L = 0.2126 × R + 0.7152 × G + 0.0722 × B

In the formula, R, G and B are the three channels after the conversion, and L is the lightness. In my case:

  • #FF0000: only the red is on, at full strength. The lightness is 0.2126, exactly the weight of red.
  • #081F40: all three channels are low. The lightness is 0.01402.
  • #FF8A80: the red is full, and green and blue have been added to it. The lightness is 0.40995.

Step three: divide the light one by the dark one.

Text
(L1 + 0.05) / (L2 + 0.05)

L1 is the lightness of the lighter of the two colors, and L2 that of the darker one. It does not matter which of them is the text and which is the background.

Text
#FF0000 / #081F40:  (0.21260 + 0.05) / (0.01402 + 0.05) = 4.10
#FF8A80 / #081F40:  (0.40995 + 0.05) / (0.01402 + 0.05) = 7.18

The calculation gives exactly the two numbers the tool displayed.

And what is the 0.05 doing there? According to the standard's explanatory document, it represents the light from the surroundings, which is added to what you see on the screen. The document calls it Viewing Flare. It also has a practical consequence: without it, absolute black would lead to a division by zero.

And why did #FF8A80 jump to 7.18? Besides the full red, it has green (138) and blue (128). Green is the channel with the largest weight, so the lightness almost doubled: from 0.2126 to 0.40995. The denominator, the dark background, did not change.

5. Does the formula have a name?

After I understood the calculation I wanted to know what it is called. I asked whether it was “the WSAG formula”.

First of all, a correction: the name is WCAG, the initials of Web Content Accessibility Guidelines. I got one letter wrong, and that too is part of learning.

And to the question itself: there is no official name such as “the WCAG formula”. The calculation has two parts, and each has a name of its own:

  • Relative Luminance: the calculation for one color, the number between 0 and 1. This is the part with the weights of red, green and blue.
  • Contrast Ratio: the division of one lightness by another, the one that gave 4.10 and 7.18.

And where did the weights come from? WCAG did not invent them. The definition itself states that the relative luminance formula is taken from sRGB, the standard that defines the color space of screens.

6. What was checked against the standard — and what remains open

I received the explanations in a conversation. Before publication they were compared, sentence by sentence, with W3C's own documents: the text of the WCAG 2.2 standard, the criterion's explanatory document, and the description of the calculation steps.

What I was told What was found in the documents
The two formulas and the range from 1 to 21 Written in the standard word for word
The thresholds 4.5, 3 and 7 Written in the standard, each in its own criterion
The 0.05 represents light from the surroundings Written in the explanatory document
The lightness formula is taken from sRGB Written in the definition itself
WCAG added the division, that is, the ratio Not accurate: according to the explanatory document the ratio is also based on earlier standards for readability on screens. The thresholds 4.5 and 7 are WCAG's
The weights were first defined in a television broadcasting standard Not found in the documents that were checked
The eye is more sensitive to green than to red and blue An explanation I received; the documents that were checked do not explain the weights

What was verified? The formulas, the range, the thresholds and the source of the formula. The calculation gave exactly 4.10 and 7.18.

What was not verified? Where the weights themselves came from, and why these particular ones. These are explanations I received, not sentences I found in the standard.

The check also revealed a detail that did not come up in the conversation. The conversion of each channel has a boundary number, 0.04045, and until May 2021 the standard gave a different value: 0.03928. The standard itself notes that the change has no practical effect on the calculation. The reason: none of the 256 possible values of a channel falls between the two numbers.

And one question remained open. From the row itself you cannot tell which threshold the tool checked against, or what text size is involved. That matters: for large text the threshold is 3, and then 4.10 passes too. So “does not meet the threshold” is correct for normal text, and not necessarily for every use of this color.

Part two — the professional summary

How does a color code become a single number?

  1. Color code of the text and of the background — leads to: Three channels: red, green, blue
  2. Three channels: red, green, blue — leads to: Conversion of each channel to a number between 0 and 1
  3. Conversion of each channel to a number between 0 and 1 — leads to: Weighting the channels: relative luminance
  4. Weighting the channels: relative luminance — leads to: Dividing the light by the dark: contrast ratio
  5. Dividing the light by the dark: contrast ratio — leads to: Is the ratio greater than or equal to the threshold?
  6. Is the ratio greater than or equal to the threshold? — leads to: Meets the threshold; Does not meet the threshold
  7. Meets the threshold
  8. Does not meet the threshold
Source code of the diagram (Mermaid)
Mermaid
flowchart TD
 A[Color code of the text and of the background] --> B[Three channels: red, green, blue]
 B --> C[Conversion of each channel to a number between 0 and 1]
 C --> D[Weighting the channels: relative luminance]
 D --> E[Dividing the light by the dark: contrast ratio]
 E --> F{Is the ratio greater than or equal to the threshold?}
 F --> G[Meets the threshold]
 F --> H[Does not meet the threshold]

The first two steps are done for each color separately. Only at the division step do the two colors meet, and that is why a single color has no contrast ratio.

Breaking down the formula: each component and its role

Component Its role
c = channel / 255 Moves the channel from the range 0 to 255 to the range 0 to 1
c / 12.92 The conversion of a very dark channel, when c is not greater than 0.04045
((c + 0.055) / 1.055) ^ 2.4 The conversion of every other channel
0.2126, 0.7152, 0.0722 The weights of red, of green and of blue
L The relative luminance of one color: 0 for black, 1 for white
L1, L2 The lightness of the lighter color and of the darker color
+ 0.05 The light from the surroundings, which is added to both colors
(L1 + 0.05) / (L2 + 0.05) The contrast ratio: from 1 to 21

The full calculation of my three colors

Color code The channels After the conversion Relative luminance The ratio against #081F40
#081F40 8, 31, 64 0.0024, 0.0137, 0.0513 0.01402 1.00
#FF0000 255, 0, 0 1, 0, 0 0.21260 4.10
#FF8A80 255, 138, 128 1, 0.2542, 0.2159 0.40995 7.18

The same intensity, three channels

To see the weights in action, here are three colors on the same background, #081F40. In each of them one channel is fully on, and the others are off:

Color code The channel that is on Relative luminance The ratio against #081F40
#FF0000 Red 0.2126 4.10
#00FF00 Green 0.7152 11.95
#0000FF Blue 0.0722 1.90
3 color pairs, each with its contrast ratio
  • #FF0000 on #081F40

    Contrast ratio 4.10:1

    • 3:1Passes
    • 4.5:1Fails
    • 7:1Fails
  • #00FF00 on #081F40

    Contrast ratio 11.95:1

    • 3:1Passes
    • 4.5:1Passes
    • 7:1Passes
  • #0000FF on #081F40

    Contrast ratio 1.90:1

    • 3:1Fails
    • 4.5:1Fails
    • 7:1Fails

Three colors that are equally “full”, and three completely different numbers. The green passes every threshold; the blue is almost invisible.

The same calculation, as code

Here is the formula as short JavaScript functions:

JavaScriptCode was run
// WCAG 2.2 contrast ratio
function channel(c8) {
  const c = c8 / 255;
  return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
}

function luminance(hex) {
  const n = parseInt(hex.slice(1), 16);
  const [r, g, b] = [n >> 16, (n >> 8) & 255, n & 255].map(channel);
  return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}

function contrast(a, b) {
  const [dark, light] = [luminance(a), luminance(b)].sort((x, y) => x - y);
  return (light + 0.05) / (dark + 0.05);
}

const background = '#081F40';
for (const text of ['#FF0000', '#FF8A80']) {
  const ratio = contrast(text, background);
  const shown = (Math.floor(ratio * 100) / 100).toFixed(2);
  console.log(text, 'on', background, shown, ratio >= 4.5 ? 'pass' : 'fail');
}

And the output received when it was run in Node.js:

TextOutput actually received
#FF0000 on #081F40 4.10 fail
#FF8A80 on #081F40 7.18 pass

The comparison with the threshold is done on the full value, ratio. The printed number is cut to two decimal places and is not rounded, so that 4.499 is never displayed as 4.50. This is how all the ratios in this post are displayed.

The thresholds — the ones I met and the ones that are important to know

My row was checked against one threshold. The standard has several, and they depend on what is being checked. Not all the thresholds in the table applied to my check — some of them are here to complete the picture.

Required ratio What it applies to Level The criterion in the standard
4.5:1 Normal text AA 1.4.3 Contrast (Minimum)
3:1 Large text AA 1.4.3 Contrast (Minimum)
3:1 User interface components and parts of graphics that are required to understand the content AA 1.4.11 Non-text Contrast
7:1 Normal text AAA 1.4.6 Contrast (Enhanced)
4.5:1 Large text AAA 1.4.6 Contrast (Enhanced)
No requirement Text in an inactive component, pure decoration, text that is not visible to anyone, text that is part of a picture with significant other visual content, a logo — The exceptions of 1.4.3 and of 1.4.6

Where did 4.5 come from? According to the explanatory document, 3:1 is the minimum that earlier standards recommend for normal text and normal vision. To compensate for a loss of contrast sensitivity, the number was multiplied by 1.5, and that is how 4.5 was arrived at. The threshold 7 is meant to compensate for a greater loss.

A useful pointer for checking: each of these numbers is a Threshold, and you do not round to it. The explanatory document gives the example explicitly: 4.499 does not meet 4.5. And the difference can be very fine: on a white background, the gray #767676 gets 4.54 and passes, and the gray #777777, its close neighbor, gets 4.47 and fails.

How do you go from here to the real world?

Once you understand what the number measures, you can make the check a regular part of the work on an interface:

  1. Check pairs, not colors: a single color has no contrast ratio. The same #FF0000 gets 4.10 on my dark blue, and 3.99 on white.

  2. Define a text color and a background color together: according to the standard, a text color that is defined without a background color is a failure, because there is no way to know on which background it will be shown.

  3. Determine which threshold applies: normal text, large text, or a user interface component. The same number can pass in one use and fail in another.

  4. Compare the full value: without rounding, and without settling for the number that is displayed with two digits after the decimal point.

  5. Fix the lightness, not the hue: to stay in the same color family, make the text lighter on a dark background, or darker on a light background, until the ratio passes the threshold. That is how you get from #FF0000 to #FF8A80.

  6. Keep the checked pairs in one place: color variables that are defined together with the background they were checked on, so that there is no need to recalculate on every screen.

  7. Check again after every color change: including user interface components and their states, and not only text.

What not to do: do not rely on “it looks prominent”; do not round 4.49 to 4.5; do not check a text color without its background; and do not assume that a number that passed for large text will also pass for normal text.

A conceptual example from the development world

CSS
:root {
  --surface-dark: #081F40;
  --danger-on-dark: #FF8A80; /* 7.18:1 on --surface-dark */
}

This is the point I take from the inquiry to my work: a text color is always chosen together with its background, and the number is part of the definition.

Review questions — with answers

8 questions. Select a question to open its answer.

Question 1: What does the contrast ratio measure?

Answer: The difference in lightness between two colors, as a number between 1 and 21. It does not measure the hue, and it does not measure how prominent the color is.

Question 2: Why does #FF0000 fail on a dark background, even though it is very prominent?

Answer: Because its relative luminance is only 0.2126. On the background #081F40 a lightness of at least 0.2381 is needed to reach 4.5.

Question 3: What is calculated first, contrast ratio or relative luminance?

Answer: Relative luminance first, for each color separately. Only then do you divide the lighter one by the darker one and get the ratio.

Question 4: Why does full green get a much higher number than full blue?

Answer: Because of the weights in the formula: 0.7152 for green versus 0.0722 for blue. On the background #081F40 full green gets 11.95, and full blue 1.90.

Question 5: Does it matter which of the two is the text and which is the background?

Answer: No. In the formula L1 is always the lighter color and L2 the darker one, so the ratio is the same in both directions.

Question 6: The tool displays 4.50. Does the color necessarily meet the threshold?

Answer: Not necessarily. If the full value is 4.499 and it was displayed rounded, the color does not meet 4.5. Compare the full value.

Question 7: When is a ratio of 3 enough?

Answer: At Level AA: for large text, and for user interface components and parts of graphics that are required to understand the content.

Question 8: Does a high ratio guarantee that the text is readable?

Answer: No. The ratio checks one condition: the difference in lightness. The standard itself notes that a font with very thin strokes is harder to read, especially at low contrast.

Appendix for anyone meeting color codes for the first time

There is no shame at all in arriving here without knowing what is hiding behind #FF8A80. Here is the explanation that lets you continue the story without opening ten other tabs.

What is hiding in a code like #FF8A80?

The # sign, followed by six digits. Each pair of digits is one channel: the first pair is red, the second green, the third blue. The digits are written in base 16, in which the letters A to F come after 9. 00 is 0, and FF is 255.

  1. #FF8A80 — leads to: FF · 8A · 80
  2. FF · 8A · 80 — leads to: 255 · 138 · 128
  3. 255 · 138 · 128 — leads to: Full red, medium green, medium blue
  4. Full red, medium green, medium blue
The diagram as text
Text
#FF8A80
     |
FF · 8A · 80
     |
255 · 138 · 128
     |
Full red, medium green, medium blue

Together, the three channels are called RGB, and the notation with the # sign is called Hex Color. RGB is the color model of screens, which emit light. Print uses a different model, CMYK, which is based on ink colors; this post deals with screen colors only.

Why is 128 not “half as light”?

The value in the color code is not the amount of light the screen produces. The relationship between the two is not a straight line, and it is called Gamma. The lightness formula corrects it before it weights the channels.

The value in the color code After the conversion
0 0
64 0.0513
128 0.2159
188 0.5029
255 1

According to the formula, “half as light” is about 188, not 128. The value 128 is worth about a fifth.

What is the difference between A, AA and AAA?

WCAG divides its requirements into three levels: A is the basic one, AA the middle one and AAA the strictest. Each requirement in the standard is called a Success Criterion, and each one is assigned a level. The 4.5 threshold for normal text belongs to Level AA, and the 7 threshold to Level AAA.

What counts as “large text”?

Large Text is text of at least 18 points, or of at least 14 points when it is bold. According to the explanatory document, one point equals 1.333 CSS pixels, so this is about 24 pixels, or 18.5 pixels in bold. The size that counts is the one the page serves, before any enlargement the user makes.

And what about things that are not text?

The border of an input field, an icon or a part of a chart: they too have a requirement, a ratio of 3:1 against the color next to them. The requirement is called Non-text Contrast. An inactive component is exempt from it.

Glossary of the post

Term Short explanation Why remember it
WCAG International guidelines for the accessibility of Web content The source of the formula and of the thresholds
W3C The organization that publishes the Web standards, including WCAG Who stands behind the standard
Contrast Ratio A number between 1 and 21 that compares the lightness of two colors The number the checking tool displays
Relative Luminance A number between 0 and 1 that describes the lightness of a single color The raw material of the contrast ratio
RGB The three channels that make up a color on a screen: red, green and blue Each channel contributes to lightness with a different weight
Hex Color A color code like #FF8A80: two digits for each channel Where the calculation starts
sRGB The standard that defines the color space of screens The source of the lightness formula
Gamma The non-linear relationship between the value in the color code and the light the screen produces The reason 128 is not “half as light”
Success Criterion A single requirement in the standard, which can be checked for whether it is met Each threshold has a criterion of its own
Level AA The middle level of the standard's three levels The 4.5 threshold belongs to it
Level AAA The strictest level in the standard The 7 threshold belongs to it
Large Text Text of at least 18 points, or 14 points in bold A lower ratio is enough for it
Non-text Contrast A contrast requirement for user interface components and parts of graphics A ratio of 3 also for things that are not text
Viewing Flare Light from the surroundings that is added to what you see on the screen The source of the 0.05 in the formula
Threshold A threshold value: you either meet it or not, without rounding 4.499 is not 4.5

Where to next?

This inquiry left me with a habit I want to keep: before changing a color — calculate. And before trusting the number — check which threshold applies.

My next step is to go through the rest of the color pairs in the interface with the same question, and to check also what is not text: borders, icons and component states.

Sources for further reading and checking

Contrast ratio calculator

Here you can choose a text color and a background color, and see the full calculation of the contrast ratio, step by step. When the pair does not meet the 4.5 threshold, the calculator suggests the closest color that does.

Contrast ratio calculator
Text

Three or six digits, for example #FF8A80

Background

Three or six digits, for example #FF8A80

#FF0000 on #081F40

Contrast ratio 4.10:1

Does not meet the threshold 4.5:1

The closest color that meets the 4.5 threshold

#FF3426 on #081F40

Contrast ratio 4.50:1 Meets the threshold

The calculation, step by step

  1. The relative luminance of each color

    Text color #FF0000

    R Red
    255After the conversion1.0000
    G Green
    0After the conversion0.0000
    B Blue
    0After the conversion0.0000

    Relative luminance L = 0.2126 × 1.0000 + 0.7152 × 0.0000 + 0.0722 × 0.0000 = 0.21260

    Background color #081F40

    R Red
    8After the conversion0.0024
    G Green
    31After the conversion0.0137
    B Blue
    64After the conversion0.0513

    Relative luminance L = 0.2126 × 0.0024 + 0.7152 × 0.0137 + 0.0722 × 0.0513 = 0.01402

  2. Dividing the lighter by the darker

    Lighter: L1 = 0.21260 (Text color)
    Darker: L2 = 0.01402 (Background color)

    (0.21260 + 0.05) / (0.01402 + 0.05) = 4.10

  3. Against the thresholds

    • Normal text, AA4.5:1Fails
    • Large text, AA3:1Passes
    • Normal text, AAA7:1Fails

The values in the calculation are shown shortened, to four or five decimal places. The calculation itself uses the full values.

In this post

From 4.10 to 7.18: how the contrast between text and background is calculated

Glossary term

Why remember it

Where it appears in the post

    Open in the glossary

    Saved in this browser only

    My pinned terms

    No pinned terms yet. You can pin a term from its explanation, or from the glossary page.

      Pinned terms on the glossary page

      Search and commands