W3C

Compositing and Blending 1.0

22 February 2012

Editors:
Rik Cabanier, Adobe Systems,
Authors:
The authors of this specification are the participants of the W3C CSS and SVG Working Groups.

Abstract

Compositing describes how shapes of different elements are combined into a single image. Conceptually, each element is rendered into its own buffer and is then merged with its backdrop. The most widely used compositing operation is simple alpha compositing. (see Simple Alpha Compositing) This spec will introduce additional compositing operators that will enable advanced graphical effects.

Blending (also known as transparency) describes how colors are ‘blended’ together. Typically, the color of an element and the color of its backdrop are combined to create a new color. This new color replaces the old color and is then composited with the backdrop using the specified compositing mode.

Status of This Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This document is the first public working draft of this specification.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

The (archived) public mailing list public-fx@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “Compositing” in the subject, preferably like this: “[Compositing] …summary of comment…

This document was produced by the CSS Working Group (part of the Style Activity) and the SVG Working Group (part of the Graphics Activity)

This document was produced by groups operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures (CSS) and a public list of any patent disclosures (SVG) made in connection with the deliverables of each group; these pages also include instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

The list of changes made to this specification is available.

Table of contents

1. Introduction

At the basic level, compositing describes how shapes interact while blending describes how colors interact. Both idioms use a rendered version of the current element (ie a shape or a group) and mix it with the backdrop.

This document will describe the algorithms of compositing and blending in the first part. The second part describes how they are specified in CSS.

2. What is compositing

Compositing is the process by which graphical objects are combined. Alpha compositing uses the alpha values, or channel (bit mask) to represent the coverage of each pixel. The alpha channel is often said to represent the ‘opacity’. This coverage information is used to control the compositing of colors.

Simple alpha compositing, like that found by default in SVG 1.1 and in GDI+ and many other graphics engines, composites each object onto the background image using a simplistic formula that has the effect of overlaying the object over the background. Where the objects overlap and coverage is not complete the color of the background may show through the object that has just been rendered.

2.1. simple alpha blending

In computer graphics, alpha compositing is the process of combining an image with a background to create the appearance of partial or full transparency. The alpha value is stored alongside the color and has a value between 0 and 1. A value of 0 means that the pixel does not have any coverage information and is transparent; i.e. there was no color contribution from any geometry because the geometry did not overlap this pixel. A value of 1 means that the pixel is opaque because the geometry completely overlapped the pixel.

A shape's contribution to a composition is described by the multiplication of its color (Ca) and its alpha (αa). This is also called the premultiplied alpha value. We will call this the ‘pixel’ from here on.

ca = Ca x αa

with
ca: the pixel value
Ca: the color value
αa: the alpha value

Conceptually, ‘alpha’ means ‘allow (1 - alpha) from behind to show’. With this in mind, we can easily determine the pixel after compositing.
Take the foreground pixel and add (1 - alpha) of the background pixel. Thus, the formula becomes:

co = ca + cb x (1 - αa)

with
co: the pixel after compositing
ca: the pixel of the shape
cb: the pixel of the backdrop
αa: the alpha value of the shape


Figure 1

There are circumstances that we have to know the alpha value of the pixel. For example in figure 1, shape 2 and 3 have opacity. They are composited together because they are in a group. This group is then composited on top of shape1. Note how the alpha of shape 2 and 3 is still in effect and is letting the shape 1 shine through. Without knowing αa of the group, we would not be able to calculate this.
Luckily, the alpha value is determined the same way as the pixel value:
take the alpha of the shape and add (1-alpha) of the backdrop. So:

αo = αa + αb x (1 - αa)

with
αo: the alpha after compositing
αa: the alpha of the shape
αb: the alpha of the backdrop

If you want to know the color of a shape and not just its pixel value, you can reverse the formula for premultiplied alpha:

Co = co / αo

By replacing co with the simple alpha compositing formula, you can directly use the color values that the user specified:

Co = 1/αo x (Ca x αa + Cb x αb x (1 - αa))

with
αo: the alpha after compositing
Co: the color after compositing
Ca: the color of the shape
Cb: the color of the backdrop
αa: the alpha of the shape
αb: the alpha of the backdrop

2.2. Examples of simple alpha blending

Next we will run through a couple of scenarios that show how alpha compositing works.


Figure 1

Figure 1 describes the most basic case. It consists of 1 shape that is filled with a solid color so no compositing operation is required.


Figure 2

Figure 2 is a more complex example. There is still no alpha but there are 2 shapes that intersect each other.

Applying the compositing value, you get:

αo = 1 + 1 x (1 - 1) = 1
co = RGB(0, 0, 255) x 1 + RGB(255, 0, 0) x 1 x (1 - 1)
co = RGB(0, 0, 255)
Co = 1/1 * RGB(0, 0, 255) = RGB(0, 0, 255)


Figure 3

Figure 3 is an example where the shape has alpha but the backdrop is opaque.

Applying the compositing value, you get:

αo = .5 + 1 x (1 - .5) = 1
co = RGB(0, 0, 255) * .5 + RGB(255, 0, 0) * 1 * (1 - .5)
co = RGB(0, 0, 127) + RGB(127, 0, 0)
co = RGB(127, 0, 127)
Co = 1/1 * RGB(127, 0, 127) = RGB(127, 0, 127)


Figure 4

Figure 4 is an example where both the shape and the backdrop have alpha.

Applying the compositing value, you get:

αo = .5 + .5 x (1 - .5) = .75
co = RGB(0, 0, 255) * .5 + RGB(255, 0, 0) * .5 * (1 - .5)
co = RGB(0, 0, 127) + RGB(127, 0, 0)
co = RGB(63, 0, 127)
Co = 1/.75 * RGB(63, 0, 127) = RGB(84, 0, 169)

2.3. Group invariance

An important behavior of simple alpha compositing is its group invariance. Adding or removing grouping with default attributes should not show visual differences.
so: a + b + c = a + (b + c) = (a + b)+c
When adding attributes to the group such as knockout, isolate, blending or Port-Duff compositing, groups are no longer be invariant.

2.4. Group alpha and blending

Conceptually each group creates a new context. The content of a group is calculated first followed by blending and compositing. If there is alpha on the group, it is multipied with the alpha value of the group's content.

2.5. Matting

The end result of alpha compositing can contain alpha. For instance, a solid red shape with an opacity value of .5 will end up with a RGB(255, 0, 0, .5) color. Since monitors and printers can't display opacity, we have to remove this alpha value by compositing it with solid white. This process is called ‘matting’.

Applying the compositing formula, you get:

co = RGB(255, 0, 255) * .5 + RGB(255, 255, 255) * 1 * (1 - .5)
co = RGB(0, 0, 127) + RGB(127, 127, 127)
co = RGB(127, 127, 255)

3. Background calculation

Until now, we haven't discussed how the backgkdrop is calculated. A first assumption would be that it consists of everything that was drawn earlier.
The problem with that is how you would deal with parent groups that have opacity or non-default blending or compositing modes since the opacity and blending on a group is applied at a later stage.
It turns out that these non-default attributes can be ignored when calculating the background. So, if you are an element in a group that has opacity, the opacity is ignored when calculating the backdrop.

3.1. Examples of backdrop calculation


Figure 5

Figure 5 has 2 simple shapes. The backdrop for the blue shape is the bottom right corner of the red shape. The dotted line shows the area that is examined during compositing of the blue shape.


Figure 6

In figure 6, the shape in the background has an alpha value. The alpha value of the background shape is preserved when the backdrop is calculated.


Figure 7

In figure 7, the blue and green shapes are in a group that has alpha. When calculating the backdrop for the blue shape, the red and green shapes are combined with simple alpha compositing and the group alpha is ignored. If the group had blending, compositing or filtering, those would also be ignored.

3.2. Establishing a new background context

Under certain circumstances, you might want to limit the background calculation upto a certain parent group. Compositing and blending each have an attribute (or TBD the same attribute) that can be set on the group where you want this to happen.
See the sections on compositing and blending on how this affects rendering.

4. Advanced compositing features

4.1. The porter-duff blending modes

NOT FINISHED The landmark 1984 paper [3] 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. The list of PD operators is: [add list] The following diagram shows the four different regions of a single pixel that are considered when compositing: [show diagram] The ‘source over’ compositing operator corresponds with the default simple alpha compositing rules. The diagram below shows an example of the ‘source over’ operator with two 50% opaque shapes. Firstly, it shows the two shapes that will be composited together using the ‘source over’ operator. The next section demonstrates a subpixel view of the region where the two objects overlap. It shows the coverage of the colors input to the compositing operation. The third section indicates which colors from this sub-pixel view are selected by the operator to contribute to the resultant color of the overlapping region. The resulting opacity of the overlapping region of the two shapes will be 75% as all of the sub-pixel regions contribute color and opacity. The calculation of resultant opacities will be described later. The last section of the diagram demonstrates the combination of the two objects using the ‘source over’ operator. [show diagram] A Porter Duff operator such as ‘source atop’, used extensively in upcoming examples, contains contributions from only two of the sub-pixel regions. The ‘source atop’(src-atop) operator selects the source color from the overlapping region and the destination color not obscured by the source to produce the resultant color. The resultant opacity is the combined opacity from these two regions. The diagram below shows an example of the ‘source atop’ operator with two 50% opaque shapes. It is similar to the previous diagram for ‘source over’. Firstly, it shows the two shapes that will be composited together using the ‘source atop’ operator. The next section demonstrates a subpixel view of the region where the two objects overlap. It shows the coverage of the colors input to the compositing operation. The third section of the diagram indicates which colors from this sub-pixel view are selected by the operator to contribute to the resultant color of the overlapping region. The resulting opacity of the overlapping region of the two shapes will be 50%, as only two of the sub-pixel regions contribute color and opacity. The calculation of resultant opacities will be described later. The last section of the diagram demonstrates the combination of the two objects using the ‘source atop’ operator. The region where only source color was present is not rendered as the ‘source atop’ only select source color that overlaps destination color. The twelve porter duff operators are shown below for opaque and partially transparent objects. The blue rectangle is the source and the orange circle is the destination.

4.2. The porter-duff blending modes

5. Blending

Blending takes the colors of the source element and mixes them with the backdrop. The resulting blended element then replaces the original source. Note that the opacity of the source element is ignored during blending. The backdrop will also be matted to remove all alpha.

We will describe the ‘mixing’ formula as:

	Cr = B(Cb, Cs)

with:
Cr: the result color
B: the formula that does the blending
Cb: the backdrop color
Cs: the source color
If the result of the mixing formula falls outside of the color range, it will be clamped.

5.1. Separable blend modes

A blend mode is termed separable if each component of the result color is completely determined by the corresponding components of the constituent backdrop and source colors—that is, if the blend mode function B is applied separately to each set of corresponding components.

Each of the following blend modes will apply the blending function B(Cb, Cs) on each of the color components.

5.1.1. normal’ blend mode

This is the default attribute which specifies no blending. The blending formula simply select the source color.

    B(Cb, Cs) = Cs

5.1.2. plus’ blend mode

The source is added to the backdrop.

    B(Cb, Cs) = Cb + Cs

5.1.3. multiply’ blend mode

The source color is multiplied by the destination color and replaces the destination.

The resultant color is always at least as dark as either the source or destination color. Multiplying any color with black results in black. Multiplying any color with white preserves the original color.

    B(Cb, Cs) = Cb x Cs

5.1.4. screen’ blend mode

Multiplies the complements of the backdrop and source color values, then complements the result.

The result color is always at least as light as either of the two constituent colors. Screening any color with white produces white; screening with black leaves the original color unchanged. The effect is similar to projecting multiple photographic slides simultaneously onto a single screen.

    B(Cb, Cs) = 1 - [(1 - Cb) x (1 - Cs)]
              = Cb + Cs -(Cb x Cs)

5.1.5. overlay’ blend mode

Multiplies or screens the colors, depending on the backdrop color value.

Source colors overlay the backdrop while preserving its highlights and shadows. The backdrop color is not replaced but is mixed with the source color to reflect the lightness or darkness of the backdrop.

    B(Cb, Cs) = HardLight(Cs, Cb)

Overlay is the inverse of the ‘hardlight’ blend mode. See the definition of ‘hardlight’ for the formula.

5.1.6. darken’ blend mode

Selects the darker of the backdrop and source colors.

The backdrop is replaced with the source where the source is darker; otherwise, it is left unchanged.

    B(Cb, Cs) = min(Cb, Cs)

5.1.7. lighten’ blend mode

Selects the lighter of the backdrop and source colors.

The backdrop is replaced with the source where the source is lighter; otherwise, it is left unchanged.

    B(Cb, Cs) = max(Cb, Cs)

5.1.8. color-dodge’ blend mode

Brightens the backdrop color to reflect the source color. Painting with black produces no changes.

    if(Cs < 1)
        B(Cb, Cs) = min(1, Cb / (1 - Cs))
    else
        B(Cb, Cs) = 1	

5.1.9. color-burn’ blend mode

Darkens the backdrop color to reflect the source color. Painting with white produces no change.

    if(Cs > 0)
        B(Cb, Cs) = 1 - min(1, (1 - Cb) / Cs)
    else
        B(Cb, Cs) = 0	

5.1.10. hard-light’ blend mode

Multiplies or screens the colors, depending on the source color value. The effect is similar to shining a harsh spotlight on the backdrop.

    if(Cs <= 0.5)
        B(Cb, Cs) = Multiply(Cb, 2 x Cs)
    else
        B(Cb, Cs) = Screen(Cb, 2 x Cs -1)	

See the definition of ‘multiply’ and ‘screen’ for their formulas.

5.1.11. soft-light’ blend mode

Darkens or lightens the colors, depending on the source color value. The effect is similar to shining a diffused spotlight on the backdrop

    if(Cs <= 0.5)
        B(Cb, Cs) = Cb - (1 - 2 x Cs) x Cb x (1 - Cb)
    else
        B(Cb, Cs) = Cb + (2 x Cs - 1) x (D(Cb) - Cb)
with
    if(Cb <= 0.25)
        D(Cb) = ((16 * Cb - 12) x Cb + 4) x Cb
    else
        D(Cb) = sqrt(Cb)

5.1.12. difference’ blend mode

Subtracts the darker of the two constituent colors from the lighter color.

Painting with white inverts the backdrop color; painting with black produces no change.

    B(Cb, Cs) = | Cb - Cs |

5.1.13. exclusion’ blend mode

Produces an effect similar to that of the Difference mode but lower in contrast. Painting with white inverts the backdrop color; painting with black produces no change

    B(Cb, Cs) = Cb + Cs - 2 x Cb x Cs

5.2. Non-separable blend modes

Nonseparable blend modes consider all color components in combination as opposed to the seperable ones that looks at each component individually.
All of these blend modes conceptually entail the following steps:
a) Convert the backdrop and source colors from the blending color space to an intermediate HSL (hue-saturation-luminosity) representation.
b) Create a new color from some combination of hue, saturation, and luminosity components selected from the backdrop and source colors.
c) Convert the result back to the original color space.

The nonseparable blend mode formulas make use of several auxiliary functions:

    Lum(C) = 0.3 x Cred + 0.59 x Cgreen + 0.11 x Cblue
    
    ClipColor(C)
        L = Lum(C)
        n = min(Cred, Cgreen, Cblue)
        x = max(Cred, Cgreen, Cblue)
        if(n < 0)
            C = L + (((C - L) * L) / (L - n))
                      
        if(x > 1)
            C = l + (((Cred - L) * (1 - L) / (x - L))
    
    SetLum(C, l)
        d = l - Lum(C)
        Cred = Cred + d
        Cgreen = Cgreen + d
        Cblue = Cblue + d
        return ClipColor(C)
        
The subscripts min, mid, and max in the next function refer to the color
components having the minimum, middle, and maximum values upon entry to the function.

    SetSat(C, s)
        if(Cmax > Cmin)
            Cmid = (((Cmid - Cmin) x s) / (Cmax - Cmin))
            Cmax = s
        else
            Cmid = Cmax = 0
            Cmin = 0
        return C;

5.2.1. hue’ blend mode

Creates a color with the hue of the source color and the saturation and luminosity of the backdrop color.

	B(Cb, Cs) = SetLum(SetSat(Cs, Sat(Cb)), Lum(Cb))

5.2.2. saturation’ blend mode

Creates a color with the saturation of the source color and the hue and luminosity of the backdrop color. Painting with this mode in an area of the backdrop that is a pure gray (no saturation) produces no change.

	B(Cb, Cs) = SetLum(SetSat(Cb, Sat(Cs)), Lum(Cb))

5.2.3. color’ blend mode

Creates a color with the hue and saturation of the source color and the luminosity of the backdrop color. This preserves the gray levels of the backdrop and is useful for colouring monochrome images or tinting colour images.

	B(Cb, Cs) = SetLum(Cs, Lum(Cb))

5.2.4. luminosity’ blend mode

Creates a color with the luminosity of the source color and the hue and saturation of the backdrop color. This produces an inverse effect to that of the Color mode.

	B(Cb, Cs) = SetLum(Cb, Lum(Cs))

5.3. Effect of group isolation on blending

In the following example, the red and green rectangles are in a group and the green rectangle has a multiply blend mode.

The background that is available for blending, is the blue and red rectangle and is shown on the right.
Now, if we made the group isolated, only the red rectangle is in the background and the result becomes as follows:

The result is that the green rectangle doesn't blend with the blue rectangle.

6. Specifying Compositing and Blending in CSS

6.1. Behavior specific to CSS

If an element specifies non-default blending, compositing or opacity, its renderstyle will revert to ‘flat’. This means that elements with z-index will be not honor the depth of elements outside of the group.

Order: first blending is applied, followed by filtering and then compositing.

6.2. Compositing

6.2.1. The alpha-compositing property

The description of the alpha-compositing property is as follows:

alpha-compositing
Value:   clear | src | dst | src-over | dst-over | src-in | dst-in | src-out | dst-out | src-atop | dst-atop | xor | inherit
Initial:   src-over
Applies to:   All elements In SVG 1.1 it applies only to "container elements (except ‘mask’) and graphics elements"
Inherited:   no
Percentages:   N/A
Media:   visual
Animatable:   yes

6.2.2. The enable-background property

The description of the enable-background property is as follows:

enable-background
Value:   accumulate | new | inherit
Initial:   accumulate
Applies to:   All elements In SVG 1.1 it applies only to "container elements (except ‘mask’) and graphics elements"
Inherited:   yes
Percentages:   N/A
Media:   visual
Animatable:   yes

6.3. Blending

6.3.1. The blend-mode property

The description of the blend-mode property is as follows:

blend-mode
Value:   normal | plus | multiply | screen | overlay | darken | lighten | color-dodge | color-burn | hard-light | soft-light | difference | exclusion | hue | saturation | color | luminosity | inherit
Initial:   normal
Applies to:   All elements In SVG 1.1 it applies only to "container elements (except ‘mask’) and graphics elements"
Inherited:   no
Percentages:   N/A
Media:   visual
Animatable:   yes

6.3.2. The isolation property

The description of the isolation property is as follows:

isolation
Value:   accumulate | isolate | inherit
Initial:   accumulate
Applies to:   All elements In SVG 1.1 it applies only to "container elements (except ‘mask’) and graphics elements"
Inherited:   yes
Percentages:   N/A
Media:   visual
Animatable:   yes

6.3.3. The knock-out property

The description of the knock-out property is as follows:

knock-out
Value:   preserve | knock-out | inherit
Initial:   preserve
Applies to:   All elements In SVG 1.1 it applies only to "container elements (except ‘mask’) and graphics elements"
Inherited:   yes
Percentages:   N/A
Media:   visual
Animatable:   yes

7. References

7.1. Normative References

[CSS21]
Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification, Bert Bos, Tantek Çelik, Ian Hickson, Håkon Wium Lie, eds., W3C, 23 April 2009, (Candidate Recommendation)
[NVDL]
Document Schema Definition Languages (DSDL) — Part 4: Namespace-based Validation Dispatching Language — NVDL. ISO/IEC FCD 19757-4, See http://www.asahi-net.or.jp/~eb2m-mrt/dsdl/
[PORTERDUFF]
Compositing Digital Images, T. Porter, T. Duff, SIGGRAPH ‘84 Conference Proceedings, Association for Computing Machinery, Volume 18, Number 3, July 1984.
[SVG-COMPOSITING]
SVG Compositing Specification, A. Grasso, ed. World Wide Web Consortium, 30 April 2009.
The latest edition of SVG Compositing is available at http://www.w3.org/TR/SVGCompositing/.
[RelaxNG]
Document Schema Definition Languages (DSDL) — Part 2: Regular grammar- based validation — RELAX NG. ISO/IEC FDIS 19757-2:2002(E), J. Clark, 村田 真 (Murata M.), eds., 12 December 2002. See http://www.y12.doe.gov/sgml/sc34/document/0362_files/relaxng-is.pdf
[Schema2]
XML Schema Part 2: Datatypes Second Edition, P. Biron, A. Malhotra, eds. W3C, 28 October 2004 (Recommendation). Latest version available at http://www.w3.org/TR/xmlschema-2/. See also Processing XML 1.1 documents with XML Schema 1.0 processors.
[SVG11]
Scalable Vector Graphics (SVG) 1.1 Specification, Dean Jackson editor, W3C, 14 January 2003 (Recommendation). See http://www.w3.org/TR/2003/REC-SVG11-20030114/
[SVGT12]
Scalable Vector Graphics (SVG) Tiny 1.2 Specification, Dean Jackson editor, W3C, 22 December 2008 (Recommendation). See http://www.w3.org/TR/2008/REC-SVGTiny12-20081222/

7.2. Informative References

[HTML5]
HTML5, Ian Hickson editor, Google, 10 June 2008 (Working Draft). See http://www.w3.org/TR/2008/WD-html5-20080610/