-
Notifications
You must be signed in to change notification settings - Fork 50
/
Overview.bs
1552 lines (1256 loc) · 59.4 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class='metadata'>
Title: Compositing and Blending Level 2
Group: fxtf
Status: ED
Work Status: Exploring
ED: https://drafts.fxtf.org/compositing-2/
TR: https://www.w3.org/TR/compositing-2/
Previous Version:
Shortname: compositing
Level: 2
Link Defaults: css-color-3 (property) color, svg (property) mask
Deadline: 2014-08-17
Editor: Rik Cabanier, Adobe Systems Inc., [email protected], w3cid 106988
Editor: Chris Harrelson, Google, [email protected], w3cid 90243
Former Editor: Nikos Andronikos, Canon Information Systems Research Australia, [email protected]
Abstract: Compositing describes how shapes of different elements are combined into a single image. There are various possible approaches for compositing. Previous versions of SVG and CSS used <a href="https://www.w3.org/TR/SVG11/masking.html#SimpleAlphaBlending">Simple Alpha Compositing</a>. In this model, each element is rendered into its own buffer and is then merged with its <a>backdrop</a> using the Porter Duff ''source-over'' operator. This specification will define a new compositing model that expands upon the Simple Alpha Compositing model by offering:
Abstract: <ul><li>additional Porter Duff compositing operators</li><li>advanced blending modes which allow control of how colors mix in the areas where shapes overlap</li><li>compositing groups</li></ul>
Abstract: In addition, this specification will define CSS properties for blending and group isolation and the properties of the {{globalCompositeOperation}} attribute.
</pre>
<pre class="link-defaults">
spec: css2;
type: dfn; text: stacking context
spec: css-cascade-3
type: dfn; text: inherit
spec: css-position-3;
type: dfn; text: containing block
</pre>
<style type="text/css">
a[data-link-type=element]::before,span[data-link-type=element]::before {
content: '<';
}
a[data-link-type=element]::after,span[data-link-type=element]::after {
content: '>';
}
</style>
<h2 id="introduction">Introduction</h2>
<p><em>This subsection is non-normative.</em><br>
The first part of this document describes the properties used to control the compositing in CSS.
The second part will describe the algorithms of Porter Duff compositing and blending.
</p>
<h2 id="reading-this-document">Reading This Document</h2>
Each section of this document is normative unless otherwise specified.
<h3 id="module-interactions">Module interactions</h3>
This specification defines a set of CSS properties that affect the visual rendering of elements to which
those properties are applied; these effects are applied after elements have been sized and positioned according
to the [[css2#visual-model-intro|Visual formatting model]]. Some values of these properties result in the creation of a
[=containing block=],
and/or the creation of a [=stacking context=].
The 'background-blend-mode' property also builds upon the properties defined in the <a href="https://www.w3.org/TR/css3-background/#placement" title="Backgrounds and Borders">CSS Backgrounds and Borders</a> module [[!CSS3BG]].
This specification also enhances the rules as specified in <a href="https://www.w3.org/TR/2003/REC-SVG11-20030114/masking.html#SimpleAlphaBlending" title="simple alpha blending">Section 14.2 Simple alpha compositing</a> of [[SVG11]] and <a href="https://www.w3.org/TR/css3-color/#alpha" title="simple alpha compositing">simple alpha compositing</a> of [[!CSS3COLOR]].
This module also extends the
{{globalCompositeOperation}}.
<h3 id="values">Values</h3>
This specification follows the [[css2#property-defs|CSS property
definition conventions]]. Value types not defined in
this specification are defined in CSS Level 2 Revision 1 [[!CSS21]]. Other CSS
modules may expand the definitions of these value types: for example [[!CSS3COLOR]],
when combined with this module, expands the definition of the <color>
value type as used in this specification.
In addition to the property-specific values listed in their definitions,
all properties defined in this specification also accept the [=inherit=]
keyword as their property value. For readability it has not been repeated
explicitly.
<h2 id="csscompositingandblending">Specifying Blending in CSS </h2>
<h3 id="compositingandblendingorder">Order of graphical operations</h3>
The compositing model must follow the <a href="https://www.w3.org/TR/SVG11/render.html#Introduction">SVG compositing</a> model [[!SVG11]]: first any filter effect is applied, then any clipping, masking, blending and compositing.
<h3 id="csscompositingrules_CSS">Behavior specific to HTML</h3>
<!-- <p>
If an element specifies non-default blending or <a href="https://www.w3.org/TR/css3-color/#transparency">'opacity'</a>, its <a href="https://www.w3.org/TR/css3-2d-transforms/#transform-style-property">transform-style</a> [[CSS3-TRANSFORMS]] and that of all of its children must revert to 'flat'.<br>
The application of blending other than 'normal' to an element must also establish a [=stacking context=] the same way that CSS <a href="https://www.w3.org/TR/css3-color/#transparency">'opacity'</a> does. One of the consequences is that elements with z-index must not honor the depth of elements outside of the group.<br> -->
Everything in CSS that creates a [=stacking context=] must be considered an <a href="#isolatedgroups">‘isolated’ group</a>. HTML elements themselves should not create groups.
An element that has blending applied, must blend with all the underlying content of the [=stacking context=] that that element belongs to.
The root element for an HTML document is the <a href="https://dom.spec.whatwg.org/#document-element">document element</a>.
<h3 id="csscompositingrules_SVG">Behavior specific to SVG</h3>
By default, every element must create a <a href="#isolatedgroups">non-isolated group</a>.
<!-- SVG has the following <a href="https://www.w3.org/TR/SVG11/render.html#Introduction">compositing model</a>: first any filter effect is applied, then any clipping, masking, blending and compositing -->
However, certain operations in SVG will create <a href="#isolatedgroups">isolated groups</a>. If one of the following features is used, the group must become isolated:
<ul>
<li>opacity</li>
<li>filters</li>
<li>3D transforms (2D transforms must NOT cause isolation)</li>
<li>blending</li>
<li>masking</li>
</ul>
The root element for SVG is the <a href="https://www.w3.org/TR/SVG11/struct.html#NewDocument">SVG element</a>.
<h3 id="csskeywords">CSS Properties</h3>
<h4 id="mix-blend-mode">The 'mix-blend-mode' property</h4>
The blend mode defines the formula that must be used to mix the colors with the backdrop.
This behavior is described in more detail in <a href="#blending">Blending</a>.
<pre class='propdef'>
Name: mix-blend-mode
Value: <<blend-mode>> | <a>plus-lighter</a>
Initial: normal
Applies to: All elements. In SVG, it applies to <a href="https://www.w3.org/TR/SVG/intro.html#TermContainerElement">container elements</a>, <a href="https://www.w3.org/TR/SVG/intro.html#TermGraphicsElement">graphics elements</a> and <a href="https://www.w3.org/TR/2011/REC-SVG11-20110816/intro.html#TermGraphicsReferencingElement">graphics referencing elements</a>. [[SVG11]]
Inherited: no
Percentages: N/A
Computed value: as specified
Media: visual
Animation type: discrete
</pre>
The syntax of the property of <<blend-mode>> is given with:
<pre class="blendmode prod">
<dfn id="ltblendmodegt"><blend-mode></dfn> =
<a value>normal</a> |
<a value>darken</a> | <a value>multiply</a> | <a value>color-burn</a> |
<a value>lighten</a> | <a value>screen</a> | <a value>color-dodge</a> |
<a value>overlay</a> | <a value>soft-light</a> | <a value>hard-light</a> |
<a value>difference</a> | <a value>exclusion</a> |
<a value>hue</a> | <a value>saturation</a> | <a value>color</a> | <a value>luminosity</a>
</pre>
Note: Applying a blendmode other than <a value>normal</a> to the element must establish a new [=stacking context=]. This group must then be blended and composited with the [=stacking context=] that contains the element.
<div class="example">
<p>Given the following sample markup:</p>
<pre>
<body>
<img src="ducky.png"/>
</body>
</pre>
<p>And the following style rule:</p>
<pre>
body { background-color: lime; }
</pre>
<p>
... will produce the following result:
</p>
<div class="figure">
<img alt="example of an image of duck with the lime color of the document" src="examples/ducky_normal.png" style="width:30%">
<p class="caption">
Partially transparent image on a lime backdrop</p>
</div>
<p>If we change the style rule to include blending:</p>
<pre>
body { background-color: lime; }
img { mix-blend-mode: multiply; }
</pre>
<p>
... the output will be the image blending with the lime background of the <body> element.
</p>
<div class="figure">
<img alt="example of an image of duck blending with the lime color of the document" src="examples/ducky_multiply.png" style="width:30%">
<p class="caption">
Blending of a transparent image on a lime backdrop.
</p>
</div>
</div>
<div class="example">
<p>Given the following svg code:</p>
<pre>
<!-- --><svg>
<!-- --> <circle cx="40" cy="40" r="40" fill="red"/>
<!-- --> <circle cx="80" cy="40" r="40" fill="lime"/>
<!-- --> <circle cx="60" cy="80" r="40" fill="blue"/>
<!-- --></svg>
</pre>
<p>And the following style rule:</p>
<pre>
circle { mix-blend-mode: screen; }
</pre>
<p>
... the output will be blending of the 3 circles. Each circle is rendered from bottom to top. Where the elements overlap, the blend mode produces a change in color.
</p>
<div class="figure">
<img alt="example of 3 circles blending with a screen blend mode" src="examples/screen_example.svg" style="width:30%">
<p class="caption">
Example of 3 circles blending with a screen blend mode
</p>
</div>
</div>
<div class="example">
<p>In the following style sheet and document fragment:</p>
<pre>
body { background-color: lime; }
div { background-color: red; width: 200px; opacity: .95}
img { mix-blend-mode: difference; }
<!-- --><body>
<!-- --> <div>
<!-- --> <img src="ducky.png"/>
<!-- --> </div>
<!-- --></body>
</pre>
<p>
... the 'opacity' on the <div> element is causing the creation of a stacking context. This causes the creation of a new group so the image doesn't blend with the color of the <body>.
</p>
<div class="figure">
<img alt="example of an image of duck blending with the lime color of the document" src="examples/ducky_difference.png" style="width:30%">
<p class="caption">
Example of blending within a stacking context.</p>
</div>
<p>
Note how the image is not blending with the lime color.
</p>
</div>
<div class="example">
<p>Given the following sample markup:</p>
<pre>
<body>
<div>
<p>''overlay'' blending on text</p>
</div>
</body>
</pre>
<p>And the following style rule:</p>
<pre>
div { background-image: url('texture.png'); }
@font-face {
font-family: "Mythos Std";
src: url("http://myfontvendor.com/mythos.otf");
}
p {
mix-blend-mode: overlay;
font-family: "Mythos Std"
}
</pre>
<div class="figure">
<img alt="example of text that has an overlay blend on top of a texture" src="examples/overlay_text.png" style="width:75%">
<p class="caption">
Text with a blend overlay on top of an image.
</p>
</div>
</div>
<h4 id="isolation">The 'isolation' property</h4>
In SVG, this defines whether an element is isolated or not.<br>
For CSS, setting 'isolation' to ''isolation/isolate'' will turn the element into a stacking context.
By default, elements use the ''isolation/auto'' keyword which implies that they are not isolated. However operations that cause the creation of [=stacking context=] must cause a group to be isolated. These operations are described in <a href="#csscompositingrules_CSS">'behavior specific to HTML'</a> and <a href="#csscompositingrules_SVG">'behavior specific to SVG'</a>.
<pre class='propdef'>
Name: isolation
Value: <<isolation-mode>>
Initial: auto
Applies to: All elements. In SVG, it applies to <a href="https://www.w3.org/TR/SVG/intro.html#TermContainerElement">container elements</a>, <a href="https://www.w3.org/TR/SVG/intro.html#TermGraphicsElement">graphics elements</a> and <a href="https://www.w3.org/TR/2011/REC-SVG11-20110816/intro.html#TermGraphicsReferencingElement">graphics referencing elements</a>. [[SVG11]]
Inherited: no
Percentages: N/A
Computed value: as specified
Media: visual
Animation type: discrete
</pre>
The syntax of the property of <<isolation-mode>> is given with:
<pre class="isolated prod"><dfn id="isolated-propid"><isolation-mode></dfn> = auto | isolate</pre>
<p id="img_isolation">
In CSS, a background image or the content of an <a element>img</a> must always be rendered into an isolated group.<br>For instance, if you link to an SVG file through the <a element>img</a> tag, the artwork of that SVG will not blend with the backdrop of the content.</p>
In SVG, 'mask' always creates an isolated group.
<h4 id="background-blend-mode">The 'background-blend-mode' property</h4>
Defines the blending mode of each background layer.
Each background layer must blend with the element's background layer that is below it and the element's background color. Background layers must not blend with the content that is behind the element, instead they must act as if they are rendered into an isolated group.
The description of the 'background-blend-mode' property is as follows:
<pre class='propdef'>
Name: background-blend-mode
Value: <<'mix-blend-mode'>>#
Initial: normal
Applies to: All HTML elements
Inherited: no
Percentages: N/A
Computed value: as specified
Media: visual
Animation type: discrete
</pre>
The 'background-blend-mode' list must be applied in the same order as 'background-image' [[!CSS3BG]]. This means that the first element in the list will apply to the layer that is on top. If a property doesn't have enough comma-separated values to match the number of layers, the UA must calculate its used value by repeating the list of values until there are enough.</p>
If the 'background' [[!CSS3BG]] shorthand is used, the 'background-blend-mode'
property for that element must be reset to its initial value.
<div class="example">
<p>Given the following sample markup:</p>
<pre>
<!-- --><body>
<!-- --> <div></div>
<!-- --></body>
</pre>
<p>And the following style rule:</p>
<pre>
body { background-color: lime; }
div {
width: 200px;
height: 200px;
background-size: 200px 200px;
background-repeat:no-repeat;
background-image: linear-gradient(to right, #000000 0%,#ffffff 100%), url('ducky.png');
background-blend-mode: difference, normal;
}
</pre>
<div class="figure">
<img alt="example of div that has an image of a duck and a gradient that is blending" src="examples/ducky_background_blend.png" style="width:30%">
<p class="caption">
Blending of 2 background images.
</p>
</div>
<p>
Note that the gradient is not blending with the color of <a element>body</a>. Instead it retains its original color.
</p>
</div>
<h2 id="canvascompositingandblending">Specifying Compositing and Blending in Canvas 2D</h2>
The <a href="https://html.spec.whatwg.org/multipage/canvas.html#2dcontext">canvas 2d</a> context defines the {{globalCompositeOperation}} attribute that is used to set the current compositing and blending operator.
This property takes the following value:
<div class="propdef">
<dl>
<dt id='propdef-mix'><span class='propdef-title prop-name'>‘globalCompositeOperation’</span></dt>
<dd>
<table class="propinfo">
<tbody>
<tr>
<td><em>Value:</em> </td>
<td> <<blend-mode>> | <<composite-mode>> </td>
</tr>
<tr>
<td><em>Initial:</em> </td>
<td>source-over</td>
</tr>
</tbody>
</table>
</dd>
</dl>
</div>
The syntax of the property of <<composite-mode>> is given with:
<pre class="compositemode prod"><dfn id="compositemode"><composite-mode></dfn> = <a>clear</a> | <a>copy</a> | <a>source-over</a> | <a>destination-over</a> | <a>source-in</a> | <br><a>destination-in</a> | <a>source-out</a> | <a>destination-out</a> | <a>source-atop</a> | <br><a>destination-atop</a> | <a>xor</a> | <a>lighter</a> | <a>plus-darker</a> | <a>plus-lighter</a></pre>
<h2 id="whatiscompositing">Introduction to compositing</h2>
Compositing is the combining of a graphic element with its backdrop.
<!--
<p>
The figure below, gives a basic example of compositing. The graphic element is overlaid on top of the backdrop to produce a composite image.
Where the graphic element is opaque, it obscures the backdrop and where the graphic element is partially transparent, some of the backdrop can be seen through.
</p>
-->
In the model described in this specification there are two steps to the overall compositing operation - <a href="#advancedcompositing">Porter-Duff compositing</a> and <a href="#blending">blending</a>.
<!--Porter Duff compositing takes into account the overall shape of the graphic element and its opacity, as well as the opacity and shape of the backdrop, and
determines where the backdrop is visible, where the graphic element is visible and where one is visible through the other.-->
The blending step determines how the colors from the graphic element and the backdrop interact.
Typically, the blending step is performed first, followed by the Porter-Duff compositing step.
In the blending step, the resultant color from the mix of the element and the the <a>backdrop</a> is calculated. The graphic element's color is replaced with this resultant color.
The graphic element is then composited with the <a>backdrop</a> using the specified compositing operator.
Note: Shape is defined by the mathematical description of the shape. A particular point is either inside the shape or it is not. There are no gradations.
Note: Opacity is described using an alpha value, stored alongside the color value for each particular point. The alpha value is between 0 and 1, inclusive.
A value of 0 means that the pixel has no coverage at that point, and is therefore
transparent; i.e. there is no color contribution from any geometry because the geometry does not overlap this pixel. A value of 1 means that the pixel is fully opaque; the geometry completely overlaps the pixel.
<h3 id="simplealphacompositing">Simple alpha compositing</h3>
The formula for simple alpha compositing is
<pre>
co = Cs x αs + Cb x αb x (1 - αs)
</pre>
Where
<ul>
<li>co: the premultiplied pixel value after compositing
<li>Cs: the color value of the source graphic element being composited
<li>αs: the alpha value of the source graphic element being composited
<li>Cb: the color value of the <a>backdrop</a>
<li>αb: the alpha value of the <a>backdrop</a>
</ul>
Note: All values are between 0 and 1 inclusive.
The pixel value after compositing (co) is given by adding the contributions from the source graphic element [Cs x αs] and the backdrop [Cb x αb x (1 - αs)].
For both the graphic element and the backdrop, the color values are multiplied by the alpha to determine the amount of color that contributes.
With zero alpha meaning that the color does not contribute and partial alpha means that some percentage of the color contributes.
The contribution of the backdrop is further reduced based on the opacity of the graphic element.
Conceptually, (1 - αs) of the backdrop shows through the graphic element, meaning that if the graphic element is fully opaque (αs=1) then no backdrop shows through.
The simple alpha compositing formula listed above gives a resultant color which is the result of the
weighted average of the backdrop color and graphic element color, with the weighting determined by the backdrop and
graphic element alphas.
The resultant alpha value of the composite is simply the sum of the contributed alpha of the composited elements.
The formula for the resultant alpha of the composite is
<pre>
αo = αs + αb x (1 - αs)
</pre>
Where
<ul>
<li>αo: the alpha value of the composite
<li>αs: the alpha value of the graphic element being composited
<li>αb: the alpha value of the <a>backdrop</a>
</ul>
</p>
<div class="note">
<p>
Often, it can be more efficient to store a <code>pre-multiplied value</code> for the color and opacity.
The pre-multiplied value is given by
<pre>
cs = Cs x αs
</pre>
with
<ul>
<li>cs: the pre-multiplied value
<li>Cs: the color value
<li>αs: the alpha value
</ul>
<p>
Thus the formula for simple alpha compositing using pre-multiplied values becomes
<pre>
co = cs + cb x (1 - αs)
</pre>
</p>
</p>
<p>
To extract the color component of a pre-multiplied value, the formula is reversed:
<pre>
Co = co / αo
</pre>
</p>
</div>
<h4 id="simplealphacompositingexamples">Examples of simple alpha compositing</h4>
<div class="example">
<div class="figure">
<img src="examples/simple_box.svg" alt="Simple box showing alpha compositing">
<p class="caption"></p>
</div>
<p>
This describes the most basic case. It consists of 1 shape that is filled with a solid color (α = 1).
The shape is composited with an empty background. The empty background has no effect on the resultant composite.
<pre>
Cs = RGB(1,0,0)
αs = 1
Cb = RGB(0,0,0)
αb = 0
co = Cs x αs + Cb x αb x (1 - αs)
co = RGB(1,0,0) x 1 + RGB(0,0,0) x 0 x (1 - 1)
co = RGB(1,0,0) x 1
co = RGB(1,0,0)
</pre>
</p>
</div>
<div class="example">
<div class="figure">
<img src="examples/two_box.svg" alt="simple shape">
<p class="caption"></p>
</div>
<p>
This is a more complex example. There is no transparency, but the 2 shapes intersect.
</p>
<p>
Applying the compositing formula in the area of intersection, gives:
<pre>
Cs = RGB(0,0,1)
αs = 1
Cb = RGB(1,0,0)
αb = 1
co = Cs x αs + Cb x αb x (1 - αs)
co = RGB(0,0,1) x 1 + RGB(1,0,0) x 1 x (1 - 1)
co = RGB(0,0,1) x 1 + RGB(1,0,0) x 1 x 0
co = RGB(0,0,1) x 1
co = RGB(0,0,1)
</pre>
Calculating the alpha of the resultant composite
<pre>
αo = αs + αb x (1 - αs)
αo = 1 + 1 x (1 - 1)
αo = 1
</pre>
Calculating the color component of the resultant composite
<pre>
Co = co / αo
Co = RGB(0, 0, 1) / 1
Co = RGB(0, 0, 1)
</pre>
</p>
</div>
<div class="example">
<div class="figure">
<img src="examples/two_box_transparency.svg" alt="interaction of a solid box with a transparent box on top">
<p class="caption"></p>
</div>
<p>
This is an example where the shape has some transparency, but the <a>backdrop</a> is fully opaque.
</p>
<p>
Applying the compositing formula in the area of intersection, gives:
<pre>
Cs = RGB(0,0,1)
αs = 0.5
Cb = RGB(1,0,0)
αb = 1
co = Cs x αs + Cb x αb x (1 - αs)
co = RGB(0,0,1) x 0.5 + RGB(1,0,0) x 1 x (1 - 0.5)
co = RGB(0,0,1) x 0.5 + RGB(1,0,0) x 0.5
co = RGB(0.5,0,0.5)
</pre>
Calculating the alpha of the resultant composite
<pre>
αo = αs + αb x (1 - αs)
αo = 0.5 + 1 x (1 - 0.5)
αo = 1
</pre>
Calculating the color component of the resultant composite
<pre>
Co = co / αo
Co = RGB(0.5, 0, 0.5) / 1
Co = RGB(0.5, 0, 0.5)
</pre>
</p>
</div>
<div class="example">
<div class="figure">
<img src="examples/two_box_transparency_both.svg" alt="interaction of 2 transparent boxes">
<p class="caption"></p>
</div>
<p>
Figure 4 shows an example where both the shape and the <a>backdrop</a> are transparent.
</p>
<p>
Applying the compositing formula in the area of intersection, gives:
<pre>
Cs = RGB(0,0,1)
αs = 0.5
Cb = RGB(1,0,0)
αb = 0.5
co = Cs x αs + Cb x αb x (1 - αs)
co = RGB(0,0,1) x 0.5 + RGB(1,0,0) x 0.5 x (1 - 0.5)
co = RGB(0,0,1) x 0.5 + RGB(1,0,0) x 0.25
co = RGB(0.25, 0, 0.5)
</pre>
Calculating the alpha of the resultant composite
<pre>
αo = αs + αb x (1 - αs)
αo = 0.5 + 0.5 x (1 - 0.5)
αo = 0.75
</pre>
Calculating the color component of the resultant composite
<pre>
Co = co / αo
Co = RGB(0.25, 0, 0.5) / 0.75
Co = RGB(0.33, 0, 0.66)
</pre>
</p>
</div>
<h2 id="generalformula">General Formula for Compositing and Blending</h2>
The general formula for compositing and blending which allows for selection of the compositing operator and blending function comprises two steps.
The terms used in these functions will be described in detail in the following sections.
Apply the blend in place
<pre>
Cs = (1 - αb) x Cs + αb x B(Cb, Cs)
</pre>
Composite
<pre>
Co = αs x Fa x Cs + αb x Fb x Cb
</pre>
Where:
<ul>
<li><strong>Cs</strong>: is the source color
<li><strong>Cb</strong>: is the backdrop color
<li><strong>αs</strong>: is the source alpha
<li><strong>αb</strong>: is the backdrop alpha
<li><strong>B(Cb, Cs)</strong>: is the mixing function
<li><strong>Fa</strong>: is defined by the Porter Duff operator in use
<li><strong>Fb</strong>: is defined by the Porter Duff operator in use
</ul>
<h2 id="backdropCalc">Backdrop calculation</h2>
The <dfn export>backdrop</dfn> is the content behind the element and is what the element is composited with.
This means that the backdrop is the result of compositing all previous elements.
<h3 id="backdropexamples">Examples of backdrop calculation</h3>
<div class="example">
<div class="figure">
<img src="examples/simple_backdrop.svg" alt="example of a simple backdrop calculation">
<p class="caption"></p>
</div>
<p>
This example has 2 simple shapes. The backdrop for the blue shape includes the bottom right corner of the red shape .
The dotted line shows the area that is examined during compositing of the blue shape.
</p>
</div>
<div class="example">
<div class="figure">
<img src="examples/simple_backdrop_alpha.svg" alt="example of a backdrop with alpha">
<p class="caption"></p>
</div>
<p>
The shape in the backdrop has an alpha value. The alpha value of the backdrop shape is preserved when the backdrop is calculated.
</p>
</div>
<h2 id="groups">Compositing Groups</h2>
Compositing groups allow more control over the interaction of compositing with the backdrop. Groups can be used to specify how a compositing effect
within a group will interact with the content that is already in the scene (the backdrop).
Compositing groups may be made up of any number of elements, and may contain other compositing groups.
The default properties of a compositing group shall cause no visual difference compared to having no group. See <a href="#groupinvariance">Group Invariance</a>.
A compositing group is rendered by first compositing the elements of the group onto the initial backdrop. The result of this is a single element containing
color and alpha information. This element is then composited onto the group backdrop.
Steps shall be taken to ensure the group backdrop makes only a single contribution to the final composite.
<dl>
<dt id="initialbackdrop">initial backdrop</dt>
<dd>
The initial backdrop is the backdrop used for compositing the group's first element. This will be the same as the group backdrop in a non-isolated
group, or a fully transparent backdrop for an isolated group.
</dd>
<dt id="groupbackdrop">group backdrop</dt>
<dd>
The group backdrop is the result of compositing all elements up to but not including the first element in the group.
</dd>
</dt>
</dl>
<h3 id="groupinvariance">Group invariance</h3>
An important property of simple alpha compositing is its group invariance. This behavior is preserved in the more complex model described in this specification.
Adding or removing grouping with default attributes shall not show visual differences.
<pre>so: A + B + C = A + (B + C) = (A + B) + C</pre>
When adding attributes to the group such as <!--'knockout', --> ''isolation/isolate'', blending modes other than ''normal'' or Porter Duff compositing operators other than ''source-over'', groups may no longer be invariant.
<h3 id="isolatedgroups">Isolated Groups</h3>
In an isolated group, the initial backdrop shall be black and fully transparent.
In this instance, the initial backdrop is different than the group backdrop. The only interaction with the group backdrop shall occur when the group's
computed color, shape and alpha are composited with it.
See '<a href="#groupcompositing">Isolated groups and Porter Duff modes</a>' for a description of the effect of isolated groups on compositing.
See '<a href="#isolationblending">Effect of group isolation on blending</a>' for a description of the effect of isolated groups on blending.
<!--
<h3 id="knockoutgroups">Knockout Groups</h3>
<p>
In a knockout group, each individual element shall be composited with the initial backdrop rather than with the stack of preceding elements in the group.
When calculating the <a>backdrop</a> for an element inside a knockout group, the elements of the group are ignored. Instead, only the elements that are behind the knockout group are included in the backdrop.
</p>
<div class="example">
<p style="text-align:center"><img src="examples/knockout.png" alt="example of a knockout group"></p>
</div>
<p>
The above example demonstrates two versions of a group containing three squares (red, green, blue) that are 50% opaque. The group is composited over a grey striped background.<br>
On the left, the group has the 'knock-out' property activated. On the right, the group has the 'knock-out' property disabled'.<br>
If the 'knock-out' property is disabled, each element within the group is only composited with the elements underneath the group.
</p>
-->
<h3 id="pagebackdrop">The Root Element Group</h3>
The <a href="#isolatedgroups">isolated group</a> for the root element is the
root element group. All other elements and groups are composited into this group. The background of the root element (if specified) is painted into the
root element group, and any filter, clip-path, mask and and opacity is then
applied, before compositing into the <a href="#rootgroup">root group</a>,
if present.
<h3 id="rootgroup">The Root Group</h3>
The root group encompasses the entire canvas and contains (or is below) the root element group of the root element of a web page.
<p class="note">Browsers often use an infinite <a href="https://www.w3.org/TR/css-color-4/#sample">white, 100% opaque</a> root group,
for final compositing, but this is not required.
<h2 id="advancedcompositing">Advanced compositing features</h2>
<a href="#simplealphacompositing">Simple alpha compositing</a> uses the [=source-over=] Porter Duff compositing operator. <!-- Additional compositing operators exist and may be specified with the <a href="#propdef-mix-composite">mix-composite property</a>.
The additional compositing operators allow for more complex interactions between the shapes of elements being composited. The compositing operators are described in the <a href="#porterduffcompositingoperators">Porter Duff compositing operators</a>.
The operators that applies to an element or group is selected using the <a href="#propdef-mix-composite">mix-composite</a> property.-->
Porter Duff compositing is based on a model of a pixel in which two shapes (source and destination) may contribute to the final color of the pixel. The pixel is divided into 4 sub-pixel regions and each region represents a possible combination of source and destination. [[PORTERDUFF]]</p>
The four regions are:
<dl>
<dt>Source Only</dt><dd>Where only the source contributes to the pixel color</dd>
<dt>Destination only</dt><dd>where only the destination contributes to the pixel color</dd>
<dt>Both</dt><dd>Source and Destination – where both the source and destination may combine to define the pixel color</dd>
<dt>None</dt><dd>No source or Destination – where neither make a contribution to the final pixel color</dd>
</dl>
Note: Destination is synonymous with backdrop. The term destination is used in this section as this is considered the standard when working with Porter Duff compositing. Additionally, the compositing operators use ''destination'' in their names.
<div class="figure">
<p style="text-align:center"><img src="examples/PD_regions.svg" alt="overview of the regions affected by porter duff"></p>
<p class="caption"></p>
</div>
The contribution from each region to the final pixel color is defined by the coverage of the shape at that pixel, and the operator in use.
Coverage is specified in terms of alpha. Full alpha (1) implies full coverage, while zero alpha (0) implies no coverage.
This means that the area of each region within the sub-pixel is dependent on the coverage of each shape contributing to the pixel.
The area of each region can be calculated with the following equations:
<div class="data">
<table>
<tr>
<th>Both</th>
<th>αs x αb</th>
</tr>
<tr>
<th>Source only</th>
<th>αs x (1 – αb)</th>
</tr>
<tr>
<th>Destination only</th>
<th>αb x (1 – αs)</th>
</tr>
<tr>
<th>None</th>
<th>(1 – αs) x (1 – αb)</th>
</tr>
</table>
</div>
The figure above represents coverage of 0.5 for both source and destination.
<pre>
Both = 0.5 x 0.5 = 0.25
Source Only = 0.5 (1 – 0.5) = 0.25
Destination Only = 0.5(1 – 0.5) = 0.25
None = (1 – 0.5)(1 – 0.5) = 0.25
</pre>
Therefore, the coverage of each region is 0.25 in this example.
<h3 id="porterduffcompositingoperators">The Porter Duff Compositing Operators</h3>
The landmark paper by Thomas Porter and Tom Duff, who worked for Lucasfilm, defined the algebra of compositing and developed the twelve "Porter Duff" operators. These operators control the results of mixing the four sub-pixel regions formed by the overlapping of graphical objects that have an alpha or pixel coverage channel/value. The operators use all practical combinations of the four regions.
There are 12 basic Porter Duff operators, satisfying all possible combinations of source and destination.
From the geometric representation of each operator, the contribution of each shape can be seen to be expressed as a fraction of the total coverage of the output.
For example, in source over, the possible contribution of source is full (1) and the possible contribution of destination is whatever is remaining (1 – αs). This is modified by the coverage of source and destination to give the equation for the final coverage of the pixel:
<pre>
αo = αs x 1 + αb x (1 – αs)
</pre>
The fractional terms Fa (1 in this example) and Fb (1 – αs in this example) are defined for each operator and specify the fraction of the shapes that may contribute to the final pixel value.
The general form of the equation for coverage is:
<pre>
αs x Fa + αb x Fb
</pre>
and incorporating color gives the general Porter Duff equation
<pre>
co = αs x Fa x Cs + αb x Fb x Cb
</pre>
Where:
<ul>
<li>co is the output color pre-multiplied with the output alpha [0 <= co <= 1]
<li>αs is the coverage of the source Fa is defined by the operator and controls inclusion of the source Cs is the color of the source (not multiplied by alpha)
<li>αb is the coverage of the destination Fb is defined by the operator and controls inclusion of the destination Cb is the color of the destination (not multiplied by alpha)
</ul>
<h4 id="porterduffcompositingoperators_clear">Clear</h4>
With the <dfn>clear</dfn> compositing operator, no regions are enabled.
<div class="figure">
<img src="examples/PD_clr.svg" alt="example of porter duff clear">
<p class="caption"></p>
</div>
<pre>
Fa = 0; Fb = 0
co = 0
αo = 0
</pre>
<h4 id="porterduffcompositingoperators_src">Copy</h4>
With the <dfn>copy</dfn> compositing operator, only the source will be present.
<div class="figure">
<img src="examples/PD_src.svg" alt="example of porter duff copy">
<p class="caption"></p>
</div>
<pre>
Fa = 1; Fb = 0
co = αs x Cs
αo = αs
</pre>
<h4 id="porterduffcompositingoperators_dst">Destination</h4>
With the <dfn>destination</dfn> compositing operator, only the destination will be present.
<div class="figure">
<img src="examples/PD_dst.svg" alt="example of porter duff destination">
<p class="caption"></p>
</div>
<pre>
Fa = 0; Fb = 1
co = αb x Cb
αo = αb
</pre>
<h4 id="porterduffcompositingoperators_srcover">Source Over</h4>
With the <dfn>source-over</dfn> compositing operator, the source is placed over the destination.
<div class="figure">
<img src="examples/PD_src-over.svg" alt="example of porter duff source over">
<p class="caption"></p>
</div>
<pre>
Fa = 1; Fb = 1 – αs
co = αs x Cs + αb x Cb x (1 – αs)
αo = αs + αb x (1 – αs)
</pre>
<h4 id="porterduffcompositingoperators_dstover">Destination Over</h4>
With the <dfn>destination-over</dfn> compositing operator, Destination is placed over the source.
<div class="figure">
<img src="examples/PD_dst-over.svg" alt="example of porter duff destination over">
<p class="caption"></p>
</div>
<pre>
Fa = 1 – αb; Fb = 1
co = αs x Cs x (1 – αb) + αb x Cb
αo = αs x (1 – αb) + αb
</pre>
<h4 id="porterduffcompositingoperators_srcin">Source In</h4>
With the <dfn>source-in</dfn> compositing operator, the parts of the source that overlap with the desitnation are placed.
<div class="figure">
<img src="examples/PD_src-in.svg" alt="example of porter duff source in">
<p class="caption"></p>
</div>
<pre>
Fa = αb; Fb = 0
co = αs x Cs x αb
αo = αs x αb
</pre>
<h4 id="porterduffcompositingoperators_dstin">Destination In</h4>
With the <dfn>destination-in</dfn> compositing operator, the parts of the destination that overlap with the source are placed.
<div class="figure">
<img src="examples/PD_dst-in.svg" alt="example of porter duff destination in ">
<p class="caption"></p>
</div>
<pre>
Fa = 0; Fb = αs
co = αb x Cb x αs
αo = αb x αs
</pre>
<h4 id="porterduffcompositingoperators_srcout">Source Out</h4>
With the <dfn>source-out</dfn> compositing operator, the parts of the source that fall outside of the destination are placed.
<div class="figure">
<img src="examples/PD_src-out.svg" alt="example of porter duff source out">
<p class="caption"></p>
</div>
<pre>
Fa = 1 – αb; Fb = 0
co = αs x Cs x (1 – αb)
αo = αs x (1 – αb)
</pre>
<h4 id="porterduffcompositingoperators_dstout">Destination Out</h4>
With the <dfn>destination-out</dfn> compositing operator, the parts of the destination that fall outside of the source are placed.
<div class="figure">
<img src="examples/PD_dst-out.svg" alt="example of porter duff destination out">
<p class="caption"></p>
</div>
<pre>
Fa = 0; Fb = 1 – αs
co = αb x Cb x (1 – αs)
αo = αb x (1 – αs)
</pre>
<h4 id="porterduffcompositingoperators_srcatop">Source Atop</h4>
With the <dfn>source-atop</dfn> compositing operator, the parts of the source which overlap the destination replace the destination. The destination is placed everywhere else.
<div class="figure">
<img src="examples/PD_src-atop.svg" alt="example of porter duff source atop">
<p class="caption"></p>
</div>
<pre>
Fa = αb; Fb = 1 – αs
co = αs x Cs x αb + αb x Cb x (1 – αs)
αo = αs x αb + αb x (1 – αs)
</pre>
<h4 id="porterduffcompositingoperators_dstatop">Destination Atop</h4>
With the <dfn>destination-atop</dfn> compositing operator, the parts of the destination which overlaps the source replace the source. The source is placed everywhere else.
<div class="figure">
<img src="examples/PD_dst-atop.svg" alt="example of porter duff destination atop">
<p class="caption"></p>
</div>
<pre>
Fa = 1 - αb; Fb = αs
co = αs x Cs x (1 - αb) + αb x Cb x αs
αo = αs x (1 - αb) + αb x αs
</pre>
<h4 id="porterduffcompositingoperators_xor">XOR</h4>
With the <dfn>xor</dfn> compositing operator, the non-overlapping regions of source and destination are combined.
<div class="figure">
<img src="examples/PD_xor.svg" alt="example of porter duff xor">
<p class="caption"></p>
</div>
<pre>
Fa = 1 - αb; Fb = 1 – αs
co = αs x Cs x (1 - αb) + αb x Cb x (1 – αs)
αo = αs x (1 - αb) + αb x (1 – αs)
</pre>
<h4 id="porterduffcompositingoperators_plus">Lighter</h4>
With the <dfn>lighter</dfn> compositing operator, the sum of the source image and destination image is displayed. It is defined in the Porter Duff paper as the ''plus'' operator [[PORTERDUFF]].
<pre>
Fa = 1; Fb = 1
co = αs x Cs + αb x Cb;
αo = αs + αb
</pre>
<h4 id="porterduffcompositingoperators_plus_darker">Plus-darker</h4>