Skip to content

Commit d8b0747

Browse files
author
Chris Lilley
committed
[css-color-5] second mix example with semi-transarent colors and an alpha multiplier. w3c#6833
1 parent 68d1b7a commit d8b0747

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

css-color-5/Overview.bs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,55 @@ must not be omitted.
390390
This is a <em>huge</em> difference; the ΔE200 between the correct and incorrect results is 30.7!
391391
</div>
392392

393+
When the percentage ormalization gnerates an alpha multiplier,
394+
the calculation is the same except for an additional last step.
395+
396+
<div class="example" id="ex-premultiply-srgb-2">
397+
This example is similar to the previous one,
398+
25% semi-opaque red
399+
and 75% semi-opaque green.
400+
mixed in sRGB.
401+
402+
However in this case the percentages are specified as
403+
20% of the first color and 60% of the second.
404+
This adds to 80% so the alpha multiplier is 0.8.
405+
406+
The mix percentages are then scaled
407+
by a factor of 100/80:<br>
408+
20% * 100/80 = 25%<br>
409+
60% * 100/80 = 75%<br>
410+
giving the same final mix percentages as the previous example.
411+
412+
<pre class="lang-css">
413+
color-mix(in srgb, rgb(100% 0% 0% / 0.7) 20%, rgb(0% 100% 0% / 0.2) 60%);
414+
</pre>
415+
416+
The calcuation is as follows:
417+
* <span class="swatch" style="--color: rgb(100% 0% 0% / 0.7)"></span> rgb(100% 0% 0% / 0.7)
418+
when premultiplied, is [0.7, 0, 0]
419+
* <span class="swatch" style="--color: rgb(0% 100% 0% / 0.2)"></span> rgb(0% 100% 0% / 0.2)
420+
when premultiplied, is [0, 0.2, 0]
421+
* the premultiplied, interpolated result is
422+
[0.7 * 0.25 + 0 * (1 - 0.25), 0 * 0.25 + 0.2 * (1 - 0.25), 0 * 0.25 + 0 * (1 - 0.25)]
423+
which is [0.175, 0.150, 0]
424+
* the interpolated alpha is 0.7 * 0.25 + 0.2 * (1 - 0.25) = 0.325
425+
* the un-premultiplied result is
426+
[0.175 / 0.325, 0.150 / 0.325, 0 / 0.325]
427+
which is [0.53846, 0.46154, 0]
428+
* so the mixed color would be
429+
<span class="swatch" style="--color: rgba(53.846%, 46.154%, 0%, 0.325)"></span>
430+
color(srgb 0.53846 0.46154 0 / 0.325)
431+
* there is a 0.8 alpha multiplier,
432+
so the alpha of the mixed result is actually 0.325 * 0.8 = 0.260
433+
so the mixed color is actually <span class="swatch" style="--color: rgba(53.846%, 46.154%, 0%, 0.260)"></span>
434+
color(srgb 0.53846 0.46154 0 / 0.260)
435+
</div>
436+
437+
Note: do not multiply the interpolated alpha by the alpha multiplier
438+
and then use that to undo premultiplication.
439+
That would be correct if the mix percentages were not scaled to sum to 100%,
440+
but they are, so doing it this way would adjust the mixed color <em>twice</em>.
441+
393442
<!--
394443
Let's move this example to the appropriate section when we have the new syntax for making these low-level adjustments
395444
<div class="example">

0 commit comments

Comments
 (0)