iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙌

[WCAG 2.2] Learning How to Calculate Contrast Ratios

に公開

Introduction

One of the factors that greatly affects text readability is the contrast between the text and its background. While I have used various tools to check contrast ratios in the past, I didn't understand the specific calculation method.

Therefore, in this article, I would like to introduce what I have learned about calculating contrast ratios, referring to the Web Content Accessibility Guidelines (WCAG) 2.2.

What is Contrast in Web Accessibility?

In general, the word "contrast" means comparison or opposition. Specifically, in the context of web accessibility, the word contrast often refers to the contrast (comparison) between text and its background.

Many different visual impairments can substantially impact contrast sensitivity, requiring more light-dark contrast, regardless of color (hue). For people who are not able to distinguish certain shades of color (often referred to as color blindness) hue and saturation have minimal or no effect on legibility as assessed by reading performance. Further, the inability to distinguish certain shades of color does not negatively affect light-dark contrast perception.

Reference: Understanding Success Criterion 1.4.3: Contrast (Minimum) | WAI | W3C

According to the above, many different visual impairments can significantly impact sensitivity to light-dark contrast, regardless of color (hue). For people who cannot distinguish between certain colors (often referred to as color blindness), hue and saturation have little to no effect on readability as assessed by reading performance. Furthermore, the inability to distinguish certain colors does not negatively affect the perception of light-dark contrast.

For these reasons, WCAG 2.2 recommendations calculate contrast in a way that does not use color (hue) as the primary factor.

How to Calculate Contrast Ratio

The formula for the contrast ratio is described in WCAG 2.2. The formula is as follows:

(L{\sixptsize 1} + 0.05) / (L{\sixptsize 2} + 0.05)

Here, L{\sixptsize 1} represents the relative luminance of the lighter color, and L{\sixptsize 2} represents the relative luminance of the darker color. We will discuss relative luminance later, but as the formula suggests, the contrast ratio represents the ratio between the lighter and darker colors. Also, the contrast ratio ranges from 1 to 21 (usually written as 1:1 to 21:1).

How to Calculate Relative Luminance

The term relative luminance, which we haven't seen before, appeared above. This is also defined in WCAG 2.2.

the relative brightness of any point in a colorspace, normalized to 0 for darkest black and 1 for lightest white

Relative luminance is the relative brightness of any point in a colorspace, normalized to 0 for the darkest black and 1 for the lightest white.

The method for calculating relative luminance (L) is defined as follows:

L = 0.2126 * R + 0.7152 * G + 0.0722 * B

In this case, R, G, and B are calculated for each of R{\sixptsize sRGB}, G{\sixptsize sRGB}, and B{\sixptsize sRGB} under the following conditions:

  • If the value is 0.04045 or less: {\text{value}} / {12.92}
  • If the value is greater than 0.04045: ((\text{value} + 0.055) / {1.055}) ^ {2.4}

This can be expressed as follows:

\text{if~} R{\sixptsize sRGB} <= 0.04045 \text{~then~} R = {\cfrac{R{\sixptsize sRGB}}{12.92}} \text{~else~} R = \Bigr({\cfrac{R{\sixptsize sRGB} + 0.055}{1.055}}\Bigr) ^ {2.4}
\text{if~} G{\sixptsize sRGB} <= 0.04045 \text{~then~} G = {\cfrac{G{\sixptsize sRGB}}{12.92}} \text{~else~} G = \Bigr({\cfrac{G{\sixptsize sRGB} + 0.055}{1.055}}\Bigr) ^ {2.4}
\text{if~} B{\sixptsize sRGB} <= 0.04045 \text{~then~} B = {\cfrac{B{\sixptsize sRGB}}{12.92}} \text{~else~} B = \Bigr({\cfrac{B{\sixptsize sRGB} + 0.055}{1.055}}\Bigr) ^ {2.4}

Additionally, R{\sixptsize sRGB}, G{\sixptsize sRGB}, and B{\sixptsize sRGB} are the values obtained by normalizing the 8-bit RGB values of each color to a range of 0 to 1, as follows:

R{\sixptsize sRGB} = {\cfrac{R{\sixptsize 8bit}}{255}} \\
G{\sixptsize sRGB} = {\cfrac{G{\sixptsize 8bit}}{255}} \\
B{\sixptsize sRGB} = {\cfrac{B{\sixptsize 8bit}}{255}} \\

By calculating the above, you can obtain the relative luminance (L).

Calculating the Contrast Ratio of White and Black

As an example, let's calculate the contrast ratio of #FFFFFF (white) and #000000 (black).

Calculating the Relative Luminance of White

First, let's find the relative luminance of white. The 8-bit RGB value for white is (R, G, B)=(255, 255, 255).

Therefore, R{\sixptsize sRGB}, G{\sixptsize sRGB}, and B{\sixptsize sRGB} are as follows:

R{\sixptsize sRGB} = {\cfrac{255}{255}} = 1
G{\sixptsize sRGB} = {\cfrac{255}{255}} = 1
B{\sixptsize sRGB} = {\cfrac{255}{255}} = 1

Since each value is greater than 0.04045, R, G, and B are calculated as:

R = \Bigr({\cfrac{1 + 0.055}{1.055}}\Bigr) ^ {2.4} = 1
G = \Bigr({\cfrac{1 + 0.055}{1.055}}\Bigr) ^ {2.4} = 1
B = \Bigr({\cfrac{1 + 0.055}{1.055}}\Bigr) ^ {2.4} = 1

Finally, calculate the relative luminance:

L{\sixptsize 1} = 0.2126 * 1 + 0.7152 * 1 + 0.0722 * 1 = 1

Calculating the Relative Luminance of Black

Next, let's find the relative luminance of black. The 8-bit RGB value for black is (R, G, B)=(0, 0, 0).

Therefore, R{\sixptsize sRGB}, G{\sixptsize sRGB}, and B{\sixptsize sRGB} are as follows:

R{\sixptsize sRGB} = {\cfrac{0}{255}} = 0
G{\sixptsize sRGB} = {\cfrac{0}{255}} = 0
B{\sixptsize sRGB} = {\cfrac{0}{255}} = 0

Since each value is less than 0.04045, R, G, and B are as follows:

R = {\cfrac{0}{12.92}} = 0
G = {\cfrac{0}{12.92}} = 0
B = {\cfrac{0}{12.92}} = 0

Finally, calculate the relative luminance:

L{\sixptsize 2} = 0.2126 * 0 + 0.7152 * 0 + 0.0722 * 0 = 0

Calculating the Contrast Ratio

Finally, calculate the contrast ratio:

(L{\sixptsize 1} + 0.05) / (L{\sixptsize 2} + 0.05) = (1 + 0.05) / (0 + 0.05) = 21

Therefore, the contrast ratio between white and black is 21:1.

This is the case with the highest possible contrast ratio, which is intuitively easy to understand.

Success Criteria for Contrast in Text

In WCAG 2.2, there are success criteria for text contrast: "Contrast (Minimum)" and "Contrast (Enhanced)".

Contrast (Minimum)

Success Criterion 1.4.3: Contrast (Minimum) contains the following description. This is a Level AA success criterion.

The visual presentation of text and images of text has a contrast ratio of at least 4.5:1

According to the above, the contrast ratio of text and images of text must be at least 4.5:1.

However, the following cases are exceptions:

  • Large Text
    If the font size is at least 18pt (24px), or at least 14pt (18.5px) and bold, a contrast ratio of at least 3:1 is required.
  • Incidental
    There is no contrast requirement for inactive user interface components, pure decoration, text that is not visible to anyone, etc.
  • Logotypes
    There is no contrast requirement for text that is part of a logo or brand name.

Contrast (Enhanced)

Success Criterion 1.4.6: Contrast (Enhanced) contains the following description. This is a Level AAA success criterion.

The visual presentation of text and images of text has a contrast ratio of at least 7:1

According to the above, the contrast ratio of text and images of text must be at least 7:1.

However, the following cases are exceptions:

  • Large Text
    If the font size is at least 18pt (24px), or at least 14pt (18.5px) and bold, a contrast ratio of at least 4.5:1 is required.
  • Incidental
    There is no contrast requirement for inactive user interface components, pure decoration, text that is not visible to anyone, etc.
  • Logotypes
    There is no contrast requirement for text that is part of a logo or brand name.

Summary

The two success criteria mentioned above can be summarized as follows:

Type of Content Minimum (Level AA) Enhanced (Level AAA)
Normal Text 4.5:1 or higher 7:1 or higher
Large Text 3:1 or higher 4.5:1 or higher
Incidental No requirement No requirement
Logotype No requirement No requirement

Conclusion

In this article, we learned how to calculate contrast ratios. By learning the specific calculation method, I feel that my understanding of contrast, which was previously vague, has deepened slightly. While meeting WCAG success criteria may not be everything, I believe it is very important knowledge to have. I hope the content of this article is helpful.

References

https://www.w3.org/TR/WCAG22/
https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum.html
https://www.w3.org/WAI/WCAG22/Understanding/contrast-enhanced.html
https://developer.mozilla.org/en-US/docs/Web/Accessibility/Understanding_WCAG/Perceivable/Color_contrast
https://developer.mozilla.org/ja/docs/Web/Accessibility/Understanding_Colors_and_Luminance
https://www.digital.go.jp/resources/introduction-to-web-accessibility-guidebook
https://web.dev/learn/accessibility/color-contrast?hl=ja
https://amzn.asia/d/cJMizDI
https://amzn.asia/d/9ERhFUi

Discussion