Skip to content

Most visited

Recently visited

navigation

PorterDuff.Mode

public static final enum PorterDuff.Mode
extends Enum<PorterDuff.Mode>

java.lang.Object
   ↳ java.lang.Enum<android.graphics.PorterDuff.Mode>
     ↳ android.graphics.PorterDuff.Mode


Porter-Duff

The name of the parent class is an homage to the work of Thomas Porter and Tom Duff, presented in their seminal 1984 paper titled "Compositing Digital Images". In this paper, the authors describe 12 compositing operators that govern how to compute the color resulting of the composition of a source (the graphics object to render) with a destination (the content of the render target).

"Compositing Digital Images" was published in Computer Graphics Volume 18, Number 3 dated July 1984.

Because the work of Porter and Duff focuses solely on the effects of the alpha channel of the source and destination, the 12 operators described in the original paper are called alpha compositing modes here.

For convenience, this class also provides several blending modes, which similarly define the result of compositing a source and a destination but without being constrained to the alpha channel. These blending modes are not defined by Porter and Duff but have been included in this class for convenience purposes.

Diagrams

All the example diagrams presented below use the same source and destination images:

Source image
Destination image

The order of drawing operations used to generate each diagram is shown in the following code snippet:

 Paint paint = new Paint();
 canvas.drawBitmap(destinationImage, 0, 0, paint);

 PorterDuff.Mode mode = // choose a mode
 paint.setXfermode(new PorterDuffXfermode(mode));

 canvas.drawBitmap(sourceImage, 0, 0, paint);
 

Alpha compositing modes

Source
Source Over
Source In
Source Atop
Destination
Destination Over
Destination In
Destination Atop
Clear
Source Out
Destination Out
Exclusive Or

Blending modes

Darken
Lighten
Multiply
Screen
Overlay

Compositing equations

The documentation of each individual alpha compositing or blending mode below provides the exact equation used to compute alpha and color value of the result of the composition of a source and destination.

The result (or output) alpha value is noted \(\alpha_{out}\). The result (or output) color value is noted \(C_{out}\).

Summary

Enum values

PorterDuff.Mode  ADD

Adds the source pixels to the destination pixels and saturates the result. 

PorterDuff.Mode  CLEAR

Destination pixels covered by the source are cleared to 0. 

PorterDuff.Mode  DARKEN

Retains the smallest component of the source and destination pixels. 

PorterDuff.Mode  DST

The source pixels are discarded, leaving the destination intact. 

PorterDuff.Mode  DST_ATOP

Discards the destination pixels that are not covered by source pixels. 

PorterDuff.Mode  DST_IN

Keeps the destination pixels that cover source pixels, discards the remaining source and destination pixels. 

PorterDuff.Mode  DST_OUT

Keeps the destination pixels that are not covered by source pixels. 

PorterDuff.Mode  DST_OVER

The source pixels are drawn behind the destination pixels. 

PorterDuff.Mode  LIGHTEN

Retains the largest component of the source and destination pixel. 

PorterDuff.Mode  MULTIPLY

Multiplies the source and destination pixels. 

PorterDuff.Mode  OVERLAY

Multiplies or screens the source and destination depending on the destination color. 

PorterDuff.Mode  SCREEN

Adds the source and destination pixels, then subtracts the source pixels multiplied by the destination. 

PorterDuff.Mode  SRC

The source pixels replace the destination pixels. 

PorterDuff.Mode  SRC_ATOP

Discards the source pixels that do not cover destination pixels. 

PorterDuff.Mode  SRC_IN

Keeps the source pixels that cover the destination pixels, discards the remaining source and destination pixels. 

PorterDuff.Mode  SRC_OUT

Keeps the source pixels that do not cover destination pixels. 

PorterDuff.Mode  SRC_OVER

The source pixels are drawn over the destination pixels. 

PorterDuff.Mode  XOR

Discards the source and destination pixels where source pixels cover destination pixels. 

Public methods

static PorterDuff.Mode valueOf(String name)
static final Mode[] values()

Inherited methods

From class java.lang.Enum
From class java.lang.Object
From interface java.lang.Comparable

Enum values

ADD

added in API level 1
PorterDuff.Mode ADD

Adds the source pixels to the destination pixels and saturates the result.

\(\alpha_{out} = max(0, min(\alpha_{src} + \alpha_{dst}, 1))\)

\(C_{out} = max(0, min(C_{src} + C_{dst}, 1))\)

CLEAR

added in API level 1
PorterDuff.Mode CLEAR

Destination pixels covered by the source are cleared to 0.

\(\alpha_{out} = 0\)

\(C_{out} = 0\)

DARKEN

added in API level 1
PorterDuff.Mode DARKEN

Retains the smallest component of the source and destination pixels.

\(\alpha_{out} = \alpha_{src} + \alpha_{dst} - \alpha_{src} * \alpha_{dst}\)

\(C_{out} = (1 - \alpha_{dst}) * C_{src} + (1 - \alpha_{src}) * C_{dst} + min(C_{src}, C_{dst})\)

DST

added in API level 1
PorterDuff.Mode DST

The source pixels are discarded, leaving the destination intact.

\(\alpha_{out} = \alpha_{dst}\)

\(C_{out} = C_{dst}\)

DST_ATOP

added in API level 1
PorterDuff.Mode DST_ATOP

Discards the destination pixels that are not covered by source pixels. Draws remaining destination pixels over source pixels.

\(\alpha_{out} = \alpha_{src}\)

\(C_{out} = \alpha_{src} * C_{dst} + (1 - \alpha_{dst}) * C_{src}\)

DST_IN

added in API level 1
PorterDuff.Mode DST_IN

Keeps the destination pixels that cover source pixels, discards the remaining source and destination pixels.

\(\alpha_{out} = \alpha_{src} * \alpha_{dst}\)

\(C_{out} = C_{dst} * \alpha_{src}\)

DST_OUT

added in API level 1
PorterDuff.Mode DST_OUT

Keeps the destination pixels that are not covered by source pixels. Discards destination pixels that are covered by source pixels. Discards all source pixels.

\(\alpha_{out} = (1 - \alpha_{src}) * \alpha_{dst}\)

\(C_{out} = (1 - \alpha_{src}) * C_{dst}\)

DST_OVER

added in API level 1
PorterDuff.Mode DST_OVER

The source pixels are drawn behind the destination pixels.

\(\alpha_{out} = \alpha_{dst} + (1 - \alpha_{dst}) * \alpha_{src}\)

\(C_{out} = C_{dst} + (1 - \alpha_{dst}) * C_{src}\)

LIGHTEN

added in API level 1
PorterDuff.Mode LIGHTEN

Retains the largest component of the source and destination pixel.

\(\alpha_{out} = \alpha_{src} + \alpha_{dst} - \alpha_{src} * \alpha_{dst}\)

\(C_{out} = (1 - \alpha_{dst}) * C_{src} + (1 - \alpha_{src}) * C_{dst} + max(C_{src}, C_{dst})\)

MULTIPLY

added in API level 1
PorterDuff.Mode MULTIPLY

Multiplies the source and destination pixels.

\(\alpha_{out} = \alpha_{src} * \alpha_{dst}\)

\(C_{out} = C_{src} * C_{dst}\)

OVERLAY

added in API level 1
PorterDuff.Mode OVERLAY

Multiplies or screens the source and destination depending on the destination color.

\(\alpha_{out} = \alpha_{src} + \alpha_{dst} - \alpha_{src} * \alpha_{dst}\)

\(\begin{equation} C_{out} = \begin{cases} 2 * C_{src} * C_{dst} & 2 * C_{dst} \lt \alpha_{dst} \\ \alpha_{src} * \alpha_{dst} - 2 (\alpha_{dst} - C_{src}) (\alpha_{src} - C_{dst}) & otherwise \end{cases} \end{equation}\)

SCREEN

added in API level 1
PorterDuff.Mode SCREEN

Adds the source and destination pixels, then subtracts the source pixels multiplied by the destination.

\(\alpha_{out} = \alpha_{src} + \alpha_{dst} - \alpha_{src} * \alpha_{dst}\)

\(C_{out} = C_{src} + C_{dst} - C_{src} * C_{dst}\)

SRC

added in API level 1
PorterDuff.Mode SRC

The source pixels replace the destination pixels.

\(\alpha_{out} = \alpha_{src}\)

\(C_{out} = C_{src}\)

SRC_ATOP

added in API level 1
PorterDuff.Mode SRC_ATOP

Discards the source pixels that do not cover destination pixels. Draws remaining source pixels over destination pixels.

\(\alpha_{out} = \alpha_{dst}\)

\(C_{out} = \alpha_{dst} * C_{src} + (1 - \alpha_{src}) * C_{dst}\)

SRC_IN

added in API level 1
PorterDuff.Mode SRC_IN

Keeps the source pixels that cover the destination pixels, discards the remaining source and destination pixels.

\(\alpha_{out} = \alpha_{src} * \alpha_{dst}\)

\(C_{out} = C_{src} * \alpha_{dst}\)

SRC_OUT

added in API level 1
PorterDuff.Mode SRC_OUT

Keeps the source pixels that do not cover destination pixels. Discards source pixels that cover destination pixels. Discards all destination pixels.

\(\alpha_{out} = (1 - \alpha_{dst}) * \alpha_{src}\)

\(C_{out} = (1 - \alpha_{dst}) * C_{src}\)

SRC_OVER

added in API level 1
PorterDuff.Mode SRC_OVER

The source pixels are drawn over the destination pixels.

\(\alpha_{out} = \alpha_{src} + (1 - \alpha_{src}) * \alpha_{dst}\)

\(C_{out} = C_{src} + (1 - \alpha_{src}) * C_{dst}\)

XOR

added in API level 1
PorterDuff.Mode XOR

Discards the source and destination pixels where source pixels cover destination pixels. Draws remaining source pixels.

\(\alpha_{out} = (1 - \alpha_{dst}) * \alpha_{src} + (1 - \alpha_{src}) * \alpha_{dst}\)

\(C_{out} = (1 - \alpha_{dst}) * C_{src} + (1 - \alpha_{src}) * C_{dst}\)

Public methods

valueOf

added in API level 1
PorterDuff.Mode valueOf (String name)

Parameters
name String

Returns
PorterDuff.Mode

values

added in API level 1
Mode[] values ()

Returns
Mode[]

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)