Blend mode
The blend-mode data type defines how colors should blend when elements overlap.
The Maths
The behind-the-scene math on how to perform color blend can be seen in the compositing and blending spec (created by Adobe people) and in the Adobe blend modes specification. Here, I summarize the key points.
Blending
Blending is how we mix colors. The blending function determines how we get the result color from the background/bottom color and source/top color :
A blending mode is separable if we can calculate each RGB component of the result color separately (note that we use lowercase to denote a component of ):
Composting
Composting is how we "merge" the layers into the final image (taking into account the alpha channels and the blend-color result). The method used is called Porter–Duff composting, which will be out of our discussion.
Consequently, we will also not discuss about alpha channels here.
Color
A color is comprised of red, green, and blue components. Each component has a value between 0 and 1 (inclusive):
From individual RGB values, we can calculate the saturation and luminosity using these functions:
Types of Blending Mode
normal
Takes the top color.
darken
Takes the darkest color for each RGB component.
lighten
Takes the lightest color for each RGB component.
multiply
Multiplies the color value for each RGB component.
- Multiplying with white results in the initial color (akin to multiplying with 1)
- Multiplying with black results in a black color (akin to multiplying with 0)
screen
Like projecting an image/light (the source color) to a screen (the background color). Mathematically, it inverts the colors, multiplies them, then inverts the result.
- Screening with white results in a white color ("projecting a bright white light on any screen color results in a white color")
- Screening with black results in the initial color ("projecting nothing to a screen results in the screen color")
overlay
Overlays the top color to the bottom color, preserving its hightlights and shadows. Equivalent to hard-light, but with the layers swapped.
color-dodge
Brightens the bottom color to reflect the top color.
Similar to screen, but it's easier to create a fully lit color (the top color only needs to be as light as the inverse of the bottom color).
color-burn
Darkens the backdrop color to reflect the source color.
Similar to multiply, but it's easier to create a black color (the top color only needs to be as dark as the inverse of the bottom color).
hard-light
Multiplies or screens the colors, depending on the top color value. Similar to shining a harsh spotlight on the bottom color.
soft-light
Darkens or lightens the colors, depending on the top color value. Similar to shining a diffused spotlight on the bottom color.
difference
Substracts the darker color from the lighter color.
- Painting with white inverts the bottom color
- Painting with black produces no change
exclusion
Like difference, but with less contrast.
- Painting with white inverts the bottom color
- Painting with black produces no change
hue
Use the hue (H) of the top color, and the saturation and luminosity of the bottom color.
saturation
Use the saturation (S) of the top color, and the hue and luminosity of the bottom color.
color
Use the hue and saturation (HS) of the top color, and the luminosity of the bottom color.
luminosity
Use the luminosity (L) of the top color, and the hue and saturation of the bottom color.
References
- <blend-mode> (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/blend-mode
- Compositing and Blending Level 2: Editor’s Draft, 3 December 2019 » CSS Properties » The mix-blend-mode property (fxtf.org) — https://drafts.fxtf.org/compositing/#ltblendmodegt
- PDF Blend Modes: Addendum (Adobe) — https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/blend_modes.pdf