Skip to content

Commit f49541d

Browse files
authored
Merge pull request #599 from leocb/patch-1
Update image.md with information about support for blend modes
2 parents d869112 + 1ba5445 commit f49541d

35 files changed

+164
-1
lines changed

docs/concepts/blend-modes.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
title: Bitmap Blend Modes
3+
description: CONCEPTS
4+
---
5+
6+
import BlendModeCat from '/img/concepts/blend/Cat.jpg';
7+
import BlendModeOverlayColor from '/img/concepts/blend/Overlay-Color.png';
8+
9+
import BlendModeOverlay from '/img/concepts/blend/Overlay.png';
10+
import BlendModePlus from '/img/concepts/blend/Plus.png';
11+
import BlendModeSaturation from '/img/concepts/blend/Saturation.png';
12+
import BlendModeScreen from '/img/concepts/blend/Screen.png';
13+
import BlendModeSoftLight from '/img/concepts/blend/SoftLight.png';
14+
import BlendModeColor from '/img/concepts/blend/Color.png';
15+
import BlendModeColorBurn from '/img/concepts/blend/ColorBurn.png';
16+
import BlendModeColorDodge from '/img/concepts/blend/ColorDodge.png';
17+
import BlendModeDarken from '/img/concepts/blend/Darken.png';
18+
import BlendModeDifference from '/img/concepts/blend/Difference.png';
19+
import BlendModeExclusion from '/img/concepts/blend/Exclusion.png';
20+
import BlendModeHardLight from '/img/concepts/blend/HardLight.png';
21+
import BlendModeHue from '/img/concepts/blend/Hue.png';
22+
import BlendModeLighten from '/img/concepts/blend/Lighten.png';
23+
import BlendModeLuminosity from '/img/concepts/blend/Luminosity.png';
24+
import BlendModeMultiply from '/img/concepts/blend/Multiply.png';
25+
import BlendModeNothing from '/img/concepts/blend/Nothing.png';
26+
27+
import BlendModeA from '/img/concepts/blend/A.png';
28+
import BlendModeB from '/img/concepts/blend/B.png';
29+
30+
import BlendModeDestination from '/img/concepts/blend/Destination.png';
31+
import BlendModeDestinationAtop from '/img/concepts/blend/DestinationAtop.png';
32+
import BlendModeDestinationIn from '/img/concepts/blend/DestinationIn.png';
33+
import BlendModeDestinationOut from '/img/concepts/blend/DestinationOut.png';
34+
import BlendModeDestinationOver from '/img/concepts/blend/DestinationOver.png';
35+
import BlendModeSource from '/img/concepts/blend/Source.png';
36+
import BlendModeSourceAtop from '/img/concepts/blend/SourceAtop.png';
37+
import BlendModeSourceIn from '/img/concepts/blend/SourceIn.png';
38+
import BlendModeSourceOut from '/img/concepts/blend/SourceOut.png';
39+
import BlendModeSourceOver from '/img/concepts/blend/SourceOver.png';
40+
import BlendModeXor from '/img/concepts/blend/Xor.png';
41+
42+
When rendering bitmaps graphics on screen, Avalonia supports specifying what blend mode to use while rendering. Blend modes changes the calculations performed when drawing new pixels (source) over existing pixels (destination).
43+
44+
Currently Avalonia Composite modes and Pixel Blend modes are located in a single enum called `BitmapBlendingMode`.
45+
46+
Composite modes enums mainly describes how the new pixels interact with the current on-screen pixels according to the alpha channel, this can be used to create, for example: "cookie cutters", exclusion zones or masks.
47+
48+
Pixel Blend modes on the other hand, specifies how the new colors will interact with the current colors. These modes can be used for example: on special effects, change color hues or other more complex image compositions.
49+
50+
See the [Wikipedia page](https://en.wikipedia.org/wiki/Blend_modes) on blend modes for examples of how they work and the math behind them.
51+
52+
:::info
53+
Currently Avalonia only supports Blend Modes when using the Skia renderer. Trying to use these modes with the D2D renderer will result in the same behavior as the default mode.
54+
:::
55+
56+
## Default Behavior
57+
58+
The default blend mode is `SourceOver`, meaning replacing all pixels values by the new values, dictated by the alpha channel. This is the standard way most applications overlay two images.
59+
60+
## How to use it
61+
62+
In XAML, you can specify what blend mode to use when rendering a Image control. The following example will render a color overlay over the picture of a very cute cat:
63+
64+
```xml
65+
<Panel>
66+
<Image Source="./Cat.jpg"/>
67+
<Image Source="./Overlay-Color.png" BlendMode="Multiply"/>
68+
</Panel>
69+
```
70+
71+
If you're creating a Custom User control and want to render a bitmap with code using one of these modes, you can do so by setting the `BitmapBlendingMode` in the control context render options:
72+
73+
``` csharp
74+
// Inside the "Render" method, draw the bitmap like this:
75+
76+
using (context.PushRenderOptions(RenderOptions with { BitmapBlendingMode = BitmapBlendingMode.Multiply }))
77+
{
78+
context.DrawImage(source, sourceRect, destRect);
79+
}
80+
```
81+
82+
## Bitmap Blend Mode Gallery
83+
84+
Avalonia supports several bitmap blend modes that can be applied to rendering:
85+
86+
### Pixel Blend Modes
87+
88+
Pixel blend modes affect only the color without taking into consideration the alpha channel.
89+
90+
These are the images used in the examples:
91+
92+
| Cute Cat base image (destination) | Color Wheel overlay image (source) |
93+
|:---:|:---:|
94+
| <img src={BlendModeCat} alt="" width="180"/> | <img src={BlendModeOverlayColor} alt="" width="180"/> |
95+
96+
Below are all the values currently supported by Avalonia
97+
98+
| Preview | Enum | Description |
99+
|---|---|---|
100+
| <img src={BlendModeNothing} alt="" width="180"/> | `Unspecified` | or `SourceOver` - Default Behavior. |
101+
| <img src={BlendModePlus} alt="" width="180"/> | `Plus` | Display the sum of the source image and destination image. |
102+
| <img src={BlendModeScreen} alt="" width="180"/> | `Screen` | Multiplies the complements of the destination and source color values, then complements the result. |
103+
| <img src={BlendModeOverlay} alt="" width="180"/> | `Overlay` | Multiplies or screens the colors, depending on the destination color value. |
104+
| <img src={BlendModeDarken} alt="" width="180"/> | `Darken` | Selects the darker of the destination and source colors. |
105+
| <img src={BlendModeLighten} alt="" width="180"/> | `Lighten` | Selects the lighter of the destination and source colors. |
106+
| <img src={BlendModeColorDodge} alt="" width="180"/> | `ColorDodge` | Darkens the destination color to reflect the source color. |
107+
| <img src={BlendModeColorBurn} alt="" width="180"/> | `ColorBurn` | Multiplies or screens the colors, depending on the source color value. |
108+
| <img src={BlendModeHardLight} alt="" width="180"/> | `HardLight` | Darkens or lightens the colors, depending on the source color value. |
109+
| <img src={BlendModeSoftLight} alt="" width="180"/> | `SoftLight` | Subtracts the darker of the two constituent colors from the lighter color. |
110+
| <img src={BlendModeDifference} alt="" width="180"/> | `Difference` | Produces an effect similar to that of the Difference mode but lower in contrast. |
111+
| <img src={BlendModeExclusion} alt="" width="180"/> | `Exclusion` | The source color is multiplied by the destination color and replaces the destination|
112+
| <img src={BlendModeMultiply} alt="" width="180"/> | `Multiply` | Creates a color with the hue of the source color and the saturation and luminosity of the destination color. |
113+
| <img src={BlendModeHue} alt="" width="180"/> | `Hue` | Creates a color with the hue of the source color and the saturation and luminosity of the destination color. |
114+
| <img src={BlendModeSaturation} alt="" width="180"/> | `Saturation` | Creates a color with the saturation of the source color and the hue and luminosity of the destination color. |
115+
| <img src={BlendModeColor} alt="" width="180"/> | `Color` | Creates a color with the hue and saturation of the source color and the luminosity of the destination color. |
116+
| <img src={BlendModeLuminosity} alt="" width="180"/> | `Luminosity` | Creates a color with the luminosity of the source color and the hue and saturation of the destination color. |
117+
118+
### Composition Blend modes
119+
120+
Composition blend modes affect only the alpha channel without messing with the colors.
121+
122+
These are the images used in the examples:
123+
124+
| "A" base image (destination) | "B" overlay image (source) |
125+
|:---:|:---:|
126+
| <img src={BlendModeA} alt="" width="180"/> | <img src={BlendModeB} alt="" width="180"/> |
127+
128+
Below are all the values currently supported by Avalonia. Please note that this demo is sensitive to the alpha channel and therefore the website background bleed through the images.
129+
130+
| Preview | Enum | Description |
131+
|---|---|---|
132+
| <img src={BlendModeSource} alt="" width="180"/> | `Source` | Only the source will be present. |
133+
| <img src={BlendModeSourceOver} alt="" width="180"/> | `SourceOver` | or `Unspecified` - Default behavior, Source is placed over the destination. |
134+
| <img src={BlendModeSourceIn} alt="" width="180"/> | `SourceIn` | The source that overlaps the destination, replaces the destination. |
135+
| <img src={BlendModeSourceOut} alt="" width="180"/> | `SourceOut` | Source is placed, where it falls outside of the destination. |
136+
| <img src={BlendModeSourceAtop} alt="" width="180"/> | `SourceAtop` | Source which overlaps the destination, replaces the destination. |
137+
| <img src={BlendModeXor} alt="" width="180"/> | `Xor` | The non-overlapping regions of source and destination are combined. |
138+
| <img src={BlendModeDestination} alt="" width="180"/> | `Destination` | Only the destination will be present. |
139+
| <img src={BlendModeDestinationOver} alt="" width="180"/> | `DestinationOver` | Destination is placed over the source. |
140+
| <img src={BlendModeDestinationIn} alt="" width="180"/> | `DestinationIn` | Destination which overlaps the source, replaces the source. |
141+
| <img src={BlendModeDestinationOut} alt="" width="180"/> | `DestinationOut` | Destination is placed, where it falls outside of the source. |
142+
| <img src={BlendModeDestinationAtop} alt="" width="180"/> | `DestinationAtop` | Destination which overlaps the source replaces the source. |

docs/reference/controls/image.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ description: REFERENCE - Built-in Controls
44

55
import ImageUnscaledScreenshot from '/img/reference/controls/image/image-unscaled.png';
66
import ImageUniformToFillScreenshot from '/img/reference/controls/image/image-uniform-to-fill.png';
7+
import BlendModeMultiply from '/img/concepts/blend/Multiply.png';
78

89
# Image
910

@@ -15,13 +16,17 @@ The image can display raster images from a specified image source. The source ca
1516

1617
Images can be used to compose the content of another control. For example, you can create a graphical button using image controls.
1718

19+
The image can be rendered in a few different blend modes, which changes the way the image interacts with what's behind it. see the [Bitmap Blend Modes](../../concepts/blend-modes.md) page for a list of all supported blend modes and an example gallery.
20+
1821
The image displayed can be resized and scaled. The default settings for scaling (uniform stretch in both directions) will result in the image being fitted to the size (width and/or height) given.
1922

2023
:::info
2124
The scaling settings for an image are the same as for the view box, see the reference [here](./viewbox.md).
2225
:::
2326

24-
## Example
27+
## Examples
28+
29+
### Basic
2530

2631
This example shows a bitmap asset loaded into an image control where the height and width have been restricted, but the scaling settings remain defaulted. The image itself is not square, but the image width and height are set to the same value. The rectangle is included to give an idea of how the image has been scaled:
2732

@@ -35,6 +40,8 @@ This example shows a bitmap asset loaded into an image control where the height
3540

3641
<img src={ImageUnscaledScreenshot} alt="" />
3742

43+
### Stretch
44+
3845
In this next example, introducing the stretch setting `UniformToFill` fits in all the height of the image, but crops the width because it would otherwise be wider than specified. The image is not distorted by this treatment.
3946

4047
```xml
@@ -48,6 +55,19 @@ In this next example, introducing the stretch setting `UniformToFill` fits in al
4855

4956
<img src={ImageUniformToFillScreenshot} alt="" />
5057

58+
### BlendMode
59+
60+
This example is using two images, where the second image is using the `Multiply` Blend mode. For more information, read the [Bitmap Blend Modes](../../concepts/blend-modes.md) page.
61+
62+
```xml
63+
<Panel>
64+
<Image Source="./Cat.jpg"/>
65+
<Image Source="./Overlay-Color.png" BlendMode="Multiply"/>
66+
</Panel>
67+
```
68+
69+
<img src={BlendModeMultiply} alt="" width="350"/>
70+
5171
## More Information
5272

5373
:::info

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ const sidebars = {
523523
],
524524
},
525525
'concepts/image-interpolation',
526+
'concepts/blend-modes',
526527
'concepts/templated-controls',
527528
'concepts/themes',
528529
'concepts/ui-composition',

static/img/concepts/blend/A.png

11.5 KB
Loading

static/img/concepts/blend/B.png

9.48 KB
Loading

static/img/concepts/blend/Cat.jpg

240 KB
Loading
353 KB
Loading
306 KB
Loading
319 KB
Loading
327 KB
Loading

0 commit comments

Comments
 (0)