Mode
class Mode
|
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
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 αout. The result (or output) color value is noted Cout.
Summary
Enum values | |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Enum values
ADD
enum val ADD : PorterDuff.Mode
Adds the source pixels to the destination pixels and saturates the result.
αout=max(0,min(αsrc+αdst,1))
Cout=max(0,min(Csrc+Cdst,1))
CLEAR
enum val CLEAR : PorterDuff.Mode
Destination pixels covered by the source are cleared to 0.
αout=0
Cout=0
DARKEN
enum val DARKEN : PorterDuff.Mode
Retains the smallest component of the source and destination pixels.
αout=αsrc+αdst−αsrc∗αdst
Cout=(1−αdst)∗Csrc+(1−αsrc)∗Cdst+min(Csrc,Cdst)
DST
enum val DST : PorterDuff.Mode
The source pixels are discarded, leaving the destination intact.
αout=αdst
Cout=Cdst
DST_ATOP
enum val DST_ATOP : PorterDuff.Mode
Discards the destination pixels that are not covered by source pixels. Draws remaining destination pixels over source pixels.
αout=αsrc
Cout=αsrc∗Cdst+(1−αdst)∗Csrc
DST_IN
enum val DST_IN : PorterDuff.Mode
Keeps the destination pixels that cover source pixels, discards the remaining source and destination pixels.
αout=αsrc∗αdst
Cout=Cdst∗αsrc
DST_OUT
enum val DST_OUT : PorterDuff.Mode
Keeps the destination pixels that are not covered by source pixels. Discards destination pixels that are covered by source pixels. Discards all source pixels.
αout=(1−αsrc)∗αdst
Cout=(1−αsrc)∗Cdst
DST_OVER
enum val DST_OVER : PorterDuff.Mode
The source pixels are drawn behind the destination pixels.
αout=αdst+(1−αdst)∗αsrc
Cout=Cdst+(1−αdst)∗Csrc
LIGHTEN
enum val LIGHTEN : PorterDuff.Mode
Retains the largest component of the source and destination pixel.
αout=αsrc+αdst−αsrc∗αdst
Cout=(1−αdst)∗Csrc+(1−αsrc)∗Cdst+max(Csrc,Cdst)
MULTIPLY
enum val MULTIPLY : PorterDuff.Mode
Multiplies the source and destination pixels.
αout=αsrc∗αdst
Cout=Csrc∗Cdst
OVERLAY
enum val OVERLAY : PorterDuff.Mode
Multiplies or screens the source and destination depending on the destination color.
αout=αsrc+αdst−αsrc∗αdst
Cout={2∗Csrc∗Cdst2∗Cdst<αdstαsrc∗αdst−2(αdst−Csrc)(αsrc−Cdst)otherwise
SCREEN
enum val SCREEN : PorterDuff.Mode
Adds the source and destination pixels, then subtracts the source pixels multiplied by the destination.
αout=αsrc+αdst−αsrc∗αdst
Cout=Csrc+Cdst−Csrc∗Cdst
SRC
enum val SRC : PorterDuff.Mode
The source pixels replace the destination pixels.
αout=αsrc
Cout=Csrc
SRC_ATOP
enum val SRC_ATOP : PorterDuff.Mode
Discards the source pixels that do not cover destination pixels. Draws remaining source pixels over destination pixels.
αout=αdst
Cout=αdst∗Csrc+(1−αsrc)∗Cdst
SRC_IN
enum val SRC_IN : PorterDuff.Mode
Keeps the source pixels that cover the destination pixels, discards the remaining source and destination pixels.
αout=αsrc∗αdst
Cout=Csrc∗αdst
SRC_OUT
enum val SRC_OUT : PorterDuff.Mode
Keeps the source pixels that do not cover destination pixels. Discards source pixels that cover destination pixels. Discards all destination pixels.
αout=(1−αdst)∗αsrc
Cout=(1−αdst)∗Csrc
SRC_OVER
enum val SRC_OVER : PorterDuff.Mode
The source pixels are drawn over the destination pixels.
αout=αsrc+(1−αsrc)∗αdst
Cout=Csrc+(1−αsrc)∗Cdst
XOR
enum val XOR : PorterDuff.Mode
Discards the source and destination pixels where source pixels cover destination pixels. Draws remaining source pixels.
αout=(1−αdst)∗αsrc+(1−αsrc)∗αdst
Cout=(1−αdst)∗Csrc+(1−αsrc)∗Cdst