You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- If color candidates are provided, algorithm is mandatory, otherwise a UA dependent algorithm is used (part of the resolution in #7361 )
- Reorganization of prose and examples
- Add keyword to specify algorithm without target contrast
Co-Authored-By: fantasai <725717+fantasai@users.noreply.github.com>
* <span class="swatch" style="--color: rgb(41.482% 7.941% 1.375%)"></span> color(display-p3 0.38 0.11 0.05) is <span class="swatch" style="--color: rgb(41.482% 7.941% 1.375%)"></span> color(xyz 0.06191 0.03568 0.00463) so the relative luminance is <b>0.03568</b>
124
-
* <span class="swatch" style="--color: yellow"></span> yellow is <span class="swatch" style="--color: yellow"></span> rgb(100% 100% 0%) which is <span class="swatch" style="--color: yellow"></span> color(xyz 0.76998 0.92781 0.13853) so the relative luminance is <b>0.92781</b>
125
-
* the contrast is (0.92781 + 0.05) / (0.03568 + 0.05) = <b>11.4123</b>
126
-
</div>
127
-
128
-
<h3 id="winner">
129
-
Finding the winning color
130
-
</h3>
131
-
132
-
It then selects from that list
133
-
the first color to meet or exceed the target contrast.
134
-
If no target is specified,
135
-
it selects the first color with the highest contrast
136
-
to the single color.
91
+
<h3 id="contrast-color-winner">
92
+
Finding the Winning Color</h3>
137
93
138
94
<!--
139
95
<wpt>
@@ -144,20 +100,14 @@ Computing a sufficiently contrasting color: the ''contrast-color()'' function {#
144
100
</wpt>
145
101
-->
146
102
147
-
<div class="example">
148
-
<pre class="lang-css">contrast-color(wheat vs tan, sienna, var(--myAccent), #d2691e)</pre>
149
-
150
-
The calculation is as follows:
151
-
* <span class="swatch" style="--color: wheat"></span> wheat (#f5deb3), the background, has relative luminance 0.749
152
-
* <span class="swatch" style="--color: tan"></span> tan (#d2b48c) has relative luminance 0.482 and contrast ratio <strong>1.501</strong>
153
-
* <span class="swatch" style="--color: sienna"></span> sienna (#a0522d) has relative luminance 0.137 and contrast ratio <strong>4.273</strong>
154
-
155
-
Suppose myAccent has the value <span class="swatch" style="--color: #b22222"></span> #b22222:
156
-
* #b22222 has relative luminance 0.107 and contrast ratio <strong>5.081</strong>
157
-
* <span class="swatch" style="--color: #d2691e"></span> #d2691e has relative luminance 0.305 and contrast ratio <strong>2.249</strong>
158
-
The highest contrast ratio is <strong>5.081</strong> so var(--myAccent) wins
103
+
<h4 id="contrast-color-target-winner">
104
+
If there is a target contrast</h4>
159
105
160
-
</div>
106
+
Candidate colors are tested sequentially,
107
+
starting with the first color in the list,
108
+
and ending with an automatically appended ''white, black''.
109
+
The first color to pass the specified level of contrast
@@ -169,14 +119,12 @@ Computing a sufficiently contrasting color: the ''contrast-color()'' function {#
169
119
* <span class="swatch" style="--color: wheat"></span> wheat (#f5deb3), the background, has relative luminance 0.749
170
120
* <span class="swatch" style="--color: bisque"></span> bisque (#ffe4c4) has relative luminance 0.807 and contrast ratio <strong>1.073</strong>
171
121
* <span class="swatch" style="--color: darkgoldenrod"></span> darkgoldenrod (#b8860b) has relative luminance 0.273 and contrast ratio <strong>2.477</strong>
172
-
* <span class="swatch" style="--color: olive"></span> olive (#808000) has relative luminance 0.200 and contrast ratio <strong>3.193</strong>
122
+
* <span class="swatch" style="--color: olive"></span> olive (#808000) has relative luminance 0.200 and contrast ratio <strong>3.193</strong>
173
123
* <span class="swatch" style="--color: sienna"></span> sienna (#a0522d) has relative luminance 0.137 and contrast ratio <strong>4.274</strong>
174
124
* <span class="swatch" style="--color: darkgreen"></span> darkgreen (#006400 ) has relative luminance 0.091 and contrast ratio <strong>5.662</strong>
175
-
* <span class="swatch" style="--color: maroon"></span> maroon (#800000 ) has relative luminance 0.046 and contrast ratio <strong>8.333</strong>
176
-
125
+
* <span class="swatch" style="--color: maroon"></span> maroon (#800000) has relative luminance 0.046 and contrast ratio <strong>8.333</strong>
177
126
178
127
The first color in the list which meets the desired contrast ratio of 4.5 is <span class="swatch" style="--color: darkgreen"></span> darkgreen.
179
-
180
128
</div>
181
129
182
130
<div class="example">
@@ -186,47 +134,56 @@ Computing a sufficiently contrasting color: the ''contrast-color()'' function {#
186
134
* the relative luminances and contrast ratios are the same as the previous example.
187
135
188
136
The first color in the list which meets the desired contrast ratio of 5.8 is <span class="swatch" style="--color: maroon"></span> maroon.
137
+
</div>
138
+
139
+
<div class="example">
140
+
<pre class="lang-css">contrast-color(wheat vs bisque, darkgoldenrod, olive to wcag2(AA))</pre>
189
141
142
+
The calculation is as follows:
143
+
* the relative luminances and contrast ratios are the same as the previous example.
144
+
145
+
No color in the list meets the desired contrast ratio of 4.5.
146
+
The contrast of <span class="swatch" style="--color: white"></span>''white'' against the
147
+
[=base color=] of <span class="swatch" style="--color: wheat"></span>''wheat'' is 1.314, which is smaller than 4.5 (AA),
148
+
and thus does not pass the target contrast.
149
+
Therefore, <span class="swatch" style="--color: black"></span>''black'' is returned, which has a contrast of 15.982 > 4.5.
190
150
</div>
191
151
192
-
The candidate colors are tested sequentially,
193
-
from left to right;
194
-
a color is the temporary winner
195
-
if it has the highest contrast of all those tested so far.
152
+
<h4 id="contrast-color-max-winner">
153
+
If no target contrast is specified</h4>
196
154
197
-
List traversal is terminated once the target contrast has been met or exceeded.
155
+
Candidate colors are tested sequentially,
156
+
starting with the first color in the list.
157
+
A color is the <var>temporary winner</var>
158
+
if it has the highest contrast against the [=base color=] of all those tested so far.
198
159
199
-
Once the end of the list is reached, if there is no target contrast,
200
-
the current temporary winner is the overall winner.
160
+
Once the end of the list is reached, the current <var>temporary winner</var> is the overall winner.
201
161
Thus, if two colors in the list happen to have the same contrast,
202
-
the earlier in the list wins
203
-
because the later one has the same contrast, not higher.
204
-
205
-
If there is a target contrast,
206
-
and the end of the list is reached without meeting that target,
207
-
either ''white'' or ''black'' is returned,
208
-
whichever has the higher contrast.
162
+
the earlier one wins.
209
163
210
164
<div class="example">
211
-
<pre class="lang-css">contrast-color(wheat vs bisque, darkgoldenrod, olive to wcag2(AA))</pre>
165
+
<pre class="lang-css">
166
+
--myAccent: #b22222;
167
+
color: contrast-color(wheat vs tan, sienna, var(--myAccent), #d2691e to wcag2)
168
+
</pre>
212
169
213
170
The calculation is as follows:
214
-
* the relative luminances and contrast ratios are the same as the previous example.
215
-
216
-
No color in the list meets the desired contrast ratio of 4.5,
217
-
so <span class="swatch" style="--color: black"></span> black
218
-
is returned as the contrast (15.982)
219
-
is higher than that of white (1.314).
171
+
* <span class="swatch" style="--color: wheat"></span> wheat (#f5deb3), the background, has relative luminance 0.749
172
+
* <span class="swatch" style="--color: tan"></span> tan (#d2b48c) has relative luminance 0.482 and contrast ratio <strong>1.501</strong>
173
+
* <span class="swatch" style="--color: sienna"></span> sienna (#a0522d) has relative luminance 0.137 and contrast ratio <strong>4.273</strong>
174
+
* <span class="swatch" style="--color: #b22222"></span> --myAccent (#b22222) has relative luminance 0.107 and contrast ratio <strong>5.081</strong>
175
+
* <span class="swatch" style="--color: #d2691e"></span> #d2691e has relative luminance 0.305 and contrast ratio <strong>2.249</strong>
220
176
177
+
The highest contrast ratio is <strong>5.081</strong> so ''var(--myAccent)'' wins
221
178
</div>
222
179
223
180
<div class="example">
224
181
<pre class="lang-css">
225
182
foo {
226
183
--bg: hsl(200 50% 80%);
227
184
--purple-in-hsl: hsl(300 100% 25%);
228
-
color: contrast-color(var(--bg) vs hsl(200 83% 23%), purple, var(--purple-in-hsl));
229
-
}
185
+
color: contrast-color(var(--bg) vs hsl(200 83% 23%), purple, var(--purple-in-hsl) to wcag2);
186
+
}
230
187
</pre>
231
188
232
189
The calculation is as follows:
@@ -241,6 +198,68 @@ Computing a sufficiently contrasting color: the ''contrast-color()'' function {#
241
198
<!-- great example to use in WPT -->
242
199
</div>
243
200
201
+
<h3 id="contrast-algorithms">
202
+
Contrast algorithms
203
+
</h3>
204
+
205
+
ISSUE: Currently only [[#luminance-contrast|WCAG 2.1]] is supported,
206
+
however this algorithm is known to have problems,
207
+
particularly on dark backgrounds.
208
+
Future revisions of this module will likely introduce additional contrast algorithms.
209
+
210
+
<h4 id="luminance-contrast">
211
+
WCAG 2.1: the ''wcag2'' keyword and ''wcag2()'' function
212
+
</h4>
213
+
214
+
The <dfn for="contrast-color()" value>wcag2</dfn> keyword and <dfn for="contrast-color()">wcag2()</dfn>[=functional notations=]
215
+
indicate use of the [[!WCAG21]]<a href="https://www.w3.org/TR/WCAG21/#contrast-minimum">luminance contrast</a> algorithm.
while the first [=candidate color=] in the list were
242
+
243
+
<pre class="lang-css">yellow</pre>
244
+
245
+
The calculation is as follows:
246
+
* <span class="swatch" style="--color: rgb(41.482% 7.941% 1.375%)"></span> color(display-p3 0.38 0.11 0.05) is <span class="swatch" style="--color: rgb(41.482% 7.941% 1.375%)"></span> color(xyz 0.06191 0.03568 0.00463) so the relative luminance is <b>0.03568</b>
247
+
* <span class="swatch" style="--color: yellow"></span> yellow is <span class="swatch" style="--color: yellow"></span> rgb(100% 100% 0%) which is <span class="swatch" style="--color: yellow"></span> color(xyz 0.76998 0.92781 0.13853) so the relative luminance is <b>0.92781</b>
248
+
* the contrast is (0.92781 + 0.05) / (0.03568 + 0.05) = <b>11.4123</b>
0 commit comments