1. Introduction
This section is not normative.
This module adds two one function: color-contrast().
2. Selecting the most contrasting color: the color-contrast() function
In only one current engine.
Opera?EdgeNone
Edge (Legacy)?IENone
Firefox for Android?iOS Safari?Chrome for Android?Android WebView?Samsung Internet?Opera Mobile?
This function takes, firstly, a single color (typically a background, but not necessarily), secondly, a list of two or more colors, and thirdly, an optional target luminance contrast [WCAG21].
It returns the first color in the list to meet or exceed the specified target contrast or, if no target is given, the color in the list with the greatest contrast.
The single color is separated from the list with the keyword vs and the target contrast, if present, is separated from the list with the keyword to.
color-contrast() = color-contrast( <color> vs <color>#{2,} [ to [<number> | AA | AA-large | AAA | AAA-large]]? )
The keyword AA is equivalent to 4.5, AA-large is equivalent to 3, AAA is equivalent to 7, and AAA-large is equivalent to 4.5 .
2.1. Calculating luminance and WCAG 2.1 contrast
For each color in the list, the CIE Luminance (Y) is calculated, relative to a D65 whitepoint.
For each pair of colors, the WCAG 2.1 contrast is calculated: contrast = (Yl + 0.05) / (Yd + 0.05) where Yd is the luminance of the darker color in the pair and Yl is the luminance of the lighter color. The factor 0.05 represents the luminance contribution of the viewing flare.
color ( display-p30.38 0.11 0.05 )
while the first color in the list was
yellow
The calculation is as follows:
-
color(display-p3 0.38 0.11 0.05) is color(xyz 0.06191 0.03568 0.00463) so the relative luminance is 0.03568
-
yellow is rgb(100% 100% 0%) which is color(xyz 0.76998 0.92781 0.13853) so the relative luminance is 0.92781
-
the contrast is (0.92781 + 0.05) / (0.03568 + 0.05) = 11.4123
2.2. Finding the winning color
It then selects from that list the first color to meet or exceed the target contrast. If no target is specified, it selects the first color with the highest contrast to the single color.
color-contrast ( wheat vs tan, sienna, var ( --myAccent), #d2691e)
The calculation is as follows:
-
wheat (#f5deb3), the background, has relative luminance 0.749
-
tan (#d2b48c) has relative luminance 0.482 and contrast ratio 1.501
-
sienna (#a0522d) has relative luminance 0.137 and contrast ratio 4.273
Suppose myAccent has the value #b22222:
-
#b22222 has relative luminance 0.107 and contrast ratio 5.081
-
#d2691e has relative luminance 0.305 and contrast ratio 2.249
color-contrast ( wheat vs bisque, darkgoldenrod, olive, sienna, darkgreen, maroon to AA)
The calculation is as follows:
-
wheat (#f5deb3), the background, has relative luminance 0.749
-
bisque (#ffe4c4) has relative luminance 0.807 and contrast ratio 1.073
-
darkgoldenrod (#b8860b) has relative luminance 0.273 and contrast ratio 2.477
-
olive (#808000 ) has relative luminance 0.200 and contrast ratio 3.193
-
sienna (#a0522d) has relative luminance 0.137 and contrast ratio 4.274
-
darkgreen (#006400 ) has relative luminance 0.091 and contrast ratio 5.662
-
maroon (#800000 ) has relative luminance 0.046 and contrast ratio 8.333
The first color in the list which meets the desired contrast ratio of 4.5 is darkgreen.
color-contrast ( wheat vs bisque, darkgoldenrod, olive, sienna, darkgreen, maroon to5.8 )
The calculation is as follows:
-
the relative luminances and contrast ratios are the same as the previous example.
The first color in the list which meets the desired contrast ratio of 5.8 is maroon.
The colors in the list (after the keyword vs) are tested sequentially, from left to right; a color is the temporary winner if it has the highest contrast of all those tested so far.
List traversal is terminated once the target contrast has been met or exceeded.
Once the end of the list is reached, if there is no target contrast, the current temporary winner is the overall winner. Thus, if two colors in the list happen to have the same contrast, the earlier in the list wins because the later one has the same contrast, not higher.
If there is a target contrast, and the end of the list is reached without meeting that target, either white or black is returned, whichever has the higher contrast.
color-contrast ( wheat vs bisque, darkgoldenrod, olive to AA)
The calculation is as follows:
-
the relative luminances and contrast ratios are the same as the previous example.
No color in the list meets the desired contrast ratio of 4.5, so black is returned as the contrast (15.982) is higher than that of white (1.314).
foo{ --bg : hsl ( 200 50 % 80 % ); --purple-in-hsl : hsl ( 300 100 % 25 % ); color : color-contrast ( var ( --bg) vshsl ( 200 83 % 23 % ), purple, var ( --purple-in-hsl)); }
The calculation is as follows:
-
--bg is rgb(179 213 230) which has relative luminance 0.628835
-
hsl(200 83% 23%) is rgb(10 75 107) which has relative luminance 0.061575 and contrast ratio 6.08409
-
purple is rgb(128 0 128) which has relative luminance 0.061487 and contrast ratio 6.08889
-
--purple-in-hsl is also rgb(128 0 128) which has relative luminance 0.061487 and contrast ratio 6.08889. This is not greater than the contrast for purple, so purple wins.
The calculated values here are shown to six significant figures, to demonstrate that early rounding to a lower precision would have given the wrong result (0.061575 is very close to 0.061487; 6.08409 is very close to 6.08889).
3. Resolving <color> Values
3.1. Resolving color-contrast() values
If all <color> parameters resolve to the corresponding colors in their respective color spaces, the computed value is the winning color resolved according to CSS Color 4 § 14 Resolving <color> Values. Otherwise (if currentColor was used in the function), the computed value is the color-contrast() function with each <color> parameter resolved according to CSS Color 4 § 14 Resolving <color> Values, and the keywords AA, AA-large, AAA, AAA-large replaced with their corresponding numeric value, thus preserving inheritance into child elements.
color-contrast ( rgb ( 179 213 230 ) vs cadetblue, hsl ( 200 83 % 23 % ) to AA)
the contrast with cadetblue is 1.97 while the contrast with hsl(200 83% 23%) is 6.09 which exceeds 4.5, the value for AA; so it has the computed value of the resolved hsl function:
rgb ( 10 75 107 )
For example, given a current color value of rgb(179 213 230), the value
color-contrast ( currentColor vshsl ( 200 83 % 23 % ), purple to AA)
has the computed value
color-contrast ( currentColor vsrgb ( 10 75 107 ), rgb ( 128 0 128 ) to4.5 )
4. Serialization
This section extends CSS Color 4 § 15 Serializing <color> Values to add serialization of the results of the color-contrast() function.
In this section, the strings used in the specification and the corresponding characters are as follows.
String | Character |
---|---|
" " | U+0020 SPACE |
"," | U+002C COMMA |
"-" | U+002D HYPHEN-MINUS |
"." | U+002E FULL STOP |
"/" | U+002F SOLIDUS |
The string "." shall be used as a decimal separator, regardless of locale, and there shall be no thousands separator.
As usual, if the alpha of the result is exactly 1, it is omitted from the serialization; an implicit value of 1 (fully opaque) is the default.
4.1. Serializing color-contrast()
The serialization of the result of a color-contrast() function is a <color>, as defined in CSS Color 4 § 15 Serializing <color> Values. The form used is the same as that used to specify the winning color.
The minimum precision for round-tripping is the same as that specified in CSS Color 4 § 15 Serializing <color> Values.
color-contrast(wheat vs olive, sienna, maroon)
is maroon, so the result is serialized as "rgb(128 0 0)".
While the winner of
color-contrast(wheat vs color(prophoto-rgb 0.4 0.4 0.2), color(display-p3 0.45 0.08 0.05))
is color(display-p3 0.45 0.08 0.05), so the result is serialized as "color(display-p3 0.45 0.08 0.05)".
5. Security Considerations
No new security considerations have been reported on this specification.
6. Privacy Considerations
No new privacy considerations have been reported on this specification.
7. Accessibility Considerations
This specification introduces a new feature to help stylesheet authors write stylesheets which conform to WCAG 2.1 section 1.4.3 Contrast (Minimum).
8. Changes
8.1. Changes from Colors 5
The new color-contrast() function allows one of a list of colors to be chosen, based on the WCAG 2.1 contrast with a specified color.