Skip to content

Commit e77e91e

Browse files
committed
[css-color-5] add target contrast ratio, fix w3c#4749
1 parent 674158e commit e77e91e

1 file changed

Lines changed: 75 additions & 13 deletions

File tree

css-color-5/Overview.bs

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ Mixing colors: the ''color-mix()'' function {#color-mix}
132132
4. Otherwise, if <var>p1</var> is omitted, it becomes 100% - <var>p2</var>
133133
5. If the percentages sum to zero <em>do something, tbd</em>
134134
6. Otherwise, if both are provided but do not add up to 100%,
135-
they are scaled accordingly so that they add up to 100%.
135+
they are scaled accordingly so that they add up to 100%.
136+
136137
This means that
137138
<var>p1</var> becomes <var>p1</var> / (<var>p1</var> + <var>p2</var>)
138139
and
@@ -143,10 +144,11 @@ Issue: Should we handle p1 + p2 &lt; 100% differently? E.g. a mix with `transpar
143144
After normalizing both percentages, the result is produced via the following algorithm:
144145

145146
1. Both colors are converted to the specified [=colorspace=].
146-
If the specified colorspace has a smaller gamut
147-
than the one in which the color to be adjusted is specified,
148-
gamut mapping will occur.
149-
2. Colors are then <a href="../css-color-4/#interpolation">interpolated in the specified colorspace</a>.
147+
If the specified colorspace has a smaller gamut
148+
than the one in which the color to be adjusted is specified,
149+
gamut mapping will occur.
150+
2. Colors are then <a href="../css-color-4/#interpolation">interpolated in the specified colorspace</a>.
151+
150152
The result of mixing is the color at the specified percentage along the progression
151153
of the second color to the first color.
152154

@@ -462,17 +464,23 @@ Selecting the most contrasting color: the ''color-contrast()'' function {#colorc
462464

463465
This function takes, firstly, a single color
464466
(typically a background, but not necessarily),
465-
and then second, a list of two or more colors;
466-
it selects from that list
467-
the color with highest <a href="https://www.w3.org/TR/WCAG21/#contrast-minimum">luminance contrast</a> [[!WCAG21]]
467+
secondly, a list of two or more colors,
468+
and thirdly, an optional target <a href="https://www.w3.org/TR/WCAG21/#contrast-minimum">luminance contrast</a> [[!WCAG21]].
469+
It selects from that list
470+
the first color color to meet or exceed the target contrast.
471+
If no target is specified,
472+
it selects the first color with the highest contrast
468473
to the single color.
469474

475+
The single color is separated from the list
476+
with the keyword 'vs'
477+
and the target contrast, if present, is separated from the list
478+
with the keyword 'to'.
479+
470480
<pre class='prod'>
471-
<dfn>color-contrast()</dfn> = color-contrast( <<color>> vs <<color>>#{2,} )
481+
<dfn>color-contrast()</dfn> = color-contrast( <<color>> vs <<color>>#{2,} ( to [<<number>> | AA | AA-large])? )
472482
</pre>
473483

474-
Issue(#4749): <a href="https://github.com/w3c/csswg-drafts/issues/4749">add target contrast ratio to color-contrast?</a>
475-
476484
<div class="example">
477485
<pre class="lang-css">color-contrast(wheat vs tan, sienna, var(--myAccent), #d2691e)</pre>
478486

@@ -488,15 +496,69 @@ Selecting the most contrasting color: the ''color-contrast()'' function {#colorc
488496

489497
</div>
490498

499+
The keyword 'AA' is equivalent to 4.5, and the keyword 'AA-large' is equivalent to 3.
500+
501+
<!-- live example
502+
https://colorjs.io/notebook/?storage=https%3A%2F%2Fgist.github.com%2Fsvgeesus%2Fec249f376fcecbaa8794f75dbfc1dacf
503+
-->
504+
<div class="example">
505+
<pre class="lang-css">color-contrast(wheat vs bisque, darkgoldenrod, olive, sienna, darkgreen, maroon to AA)</pre>
506+
507+
The calculation is as follows:
508+
* <span class="swatch" style="--color: wheat"></span> wheat (#f5deb3), the background, has relative luminance 0.749
509+
* <span class="swatch" style="--color: bisque"></span> bisque (#ffe4c4) has relative luminance 0.807 and contrast ratio <strong>1.073</strong>
510+
* <span class="swatch" style="--color: darkgoldenrod"></span> darkgoldenrod (#b8860b) has relative luminance 0.273 and contrast ratio <strong>2.477</strong>
511+
* <span class="swatch" style="--color: olive"></span> olive (#808000 ) has relative luminance 0.200 and contrast ratio <strong>3.193</strong>
512+
* <span class="swatch" style="--color: sienna"></span> sienna (#a0522d) has relative luminance 0.137 and contrast ratio <strong>4.274</strong>
513+
* <span class="swatch" style="--color: darkgreen"></span> darkgreen (#006400 ) has relative luminance 0.091 and contrast ratio <strong>5.662</strong>
514+
* <span class="swatch" style="--color: maroon"></span> maroon (#800000 ) has relative luminance 0.046 and contrast ratio <strong>8.333</strong>
515+
516+
517+
The first color in the list which meets the desired contrast ratio of 4.5 is <span class="swatch" style="--color: darkgreen"></span> darkgreen.
518+
519+
</div>
520+
521+
<div class="example">
522+
<pre class="lang-css">color-contrast(wheat vs bisque, darkgoldenrod, olive, sienna, darkgreen, maroon to 5.8)</pre>
523+
524+
The calculation is as follows:
525+
* the relative luminances and contrast ratios are the same as the previous example.
526+
527+
The first color in the list which meets the desired contrast ratio of 5.8 is <span class="swatch" style="--color: maroon"></span> maroon.
528+
529+
</div>
530+
491531
The colors in the list (after the keyword vs) are tested sequentially,
492532
from left to right;
493533
a color is the temporary winner
494-
if it has the highest contrast of all those tested so far,
495-
and once the end of the list is reached, the current temporary winner is the overall winner.
534+
if it has the highest contrast of all those tested so far.
535+
536+
List traversal is terminated once the target contrast has been meet or exceeded.
537+
538+
Once the end of the list is reached, if there is no target contrast,
539+
the current temporary winner is the overall winner.
496540
Thus, if two colors in the list happen to have the same contrast,
497541
the earlier in the list wins
498542
because the later one has the same contrast, not higher.
499543

544+
If there is a target contrast,
545+
and the end of the list is reached without meeting that target,
546+
either 'white' or 'black' is returned,
547+
whichever has the higher contrast.
548+
549+
<div class="example">
550+
<pre class="lang-css">color-contrast(wheat vs bisque, darkgoldenrod, olive to AA)</pre>
551+
552+
The calculation is as follows:
553+
* the relative luminances and contrast ratios are the same as the previous example.
554+
555+
No color in the list meets the desired contrast ratio of 4.5,
556+
so <span class="swatch" style="--color: black"></span> black
557+
is returned as the contrast (15.982)
558+
is higher than that of white (1.314).
559+
560+
</div>
561+
500562
<div class="example">
501563
<pre class="lang-css">
502564
foo {

0 commit comments

Comments
 (0)