Magic Wand¶
| Fact | Value |
|---|---|
| Status | Implemented and tested |
| Group | Color and tone |
| Engine | Browser |
| Code | web/src/core/selection/wand.ts, magicWand |
Overview¶
The Magic Wand (W) selects the area of similar color around the pixel you click. It is quick for flat areas such as a sky, a wall or a plain background.
How to use it¶
- Choose the Magic Wand and click a color.
- Raise the tolerance to select more colors, lower it to select fewer.
- Turn Contiguous off to select every similar pixel in the image, also where it is not connected.
- Shift-click adds, Alt-click subtracts, both intersect.
How it works¶
- Average the color in a 1x1, 3x3 or 5x5 square around the click.
- Measure the color distance of each pixel to it, in RGB, HSV or Lab on a 0 to 255 scale.
- Select pixels whose distance is at most the tolerance: only connected ones, or all of them.
- With anti-alias on, give each pixel the mean of its 3x3 neighborhood.
- Feather, then combine with the current selection by the mode.
Mathematical foundation¶
The wand compares colors as points in a color space, scaled so that black to white is 255 in each:
- RGB: the plain Euclidean distance of the stored values.
- Lab: the CIE 1976 color difference \(\Delta E^*_{ab}\) times 2.55. Lab follows how people see color differences better than RGB.
- HSV: the Euclidean distance in the HSV cone, with the point \((255\, s v \cos h,\ 255\, s v \sin h,\ 255\, v)\) for hue \(h\), saturation \(s\) and value \(v\).
A pixel \(p\) is selected when
\[
\lVert C(p) - C_0 \rVert \le T
\]
where \(C(p)\) is the color of \(p\) in the chosen space, \(C_0\) the sampled color, and \(T\) the tolerance. This is an exact computation. With Contiguous on, a flood fill from the click keeps only the selected pixels connected to it through their four side neighbors.
With anti-aliasing, each pixel then takes the mean of its 3x3 neighborhood. Pixels inside or outside a large area keep their value. Pixels on the edge become partly selected.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Tolerance tolerance |
32 | 0 to 255 | Largest color distance from the sampled color that is still selected. |
Contiguous contiguous |
on | On or off | Select only matching pixels connected to the clicked pixel. |
Sample size sampleSize |
3 by 3 average | Point sample, 3 by 3 average, 5 by 5 average | How many pixels around the click make the sampled color. |
Color space colorSpace |
RGB | RGB, HSV, Lab | Where the color distance is measured. |
Anti-alias antiAlias |
on | On or off | Softens the edge by averaging each pixel with its 3 by 3 neighbors. |
Feather feather |
0 | 0 to 250 px | Softens the edge of the new selection. |
Limitations¶
- The decision is hard: a pixel is in or out. Anti-alias and feather soften only the edge. For a soft selection by color, use Color Range.
- Noise makes the edge ragged and leaves holes. Use a larger sample, or Quick Selection.