Ceci est une ancienne révision du document !
Transparency – or its counterpart, opacity – is such a fundamental concept in Inkscape that it was one of the earliest topics covered in this series. Back in Part 3 you were introduced to the Opacity spin-box on the status bar, which gives you the ability to set a single transparency level for a whole object, or even a group of objects.
If you want a single object to have varying opacity – such as completely transparent at one end, and opaque at the other – you can use a gradient for the fill and stroke. But what about doing the same for a group of objects? You could set suitable gradients on each individual item in the group separately to get the desired effect, but if your group contains lots of objects, then that approach becomes tedious very quickly.
The solution to this problem is masking. This is a technique that uses a grayscale image as a means of specifying the opacity of another object or group. Any white sections in the image cause that part of the masked object to appear opaque. Black sections cause that part to appear transparent. And grays in-between result in varying levels of opacity.
Let's look at an example. In this image (below) I've placed a solid red circle on top of a solid green background. The green is there mainly to show the effect more clearly. On top of the circle is a rectangle with a white-black-white gradient. Using this rectangle as a mask for the circle gives the result of a clipped circle that fades from opaque to transparent and back to opaque.
Note that anything outside the mask is clipped. In that respect, masking could even be used as an alternative to the clip paths that were introduced in the previous part of this tutorial. Draw your mask entirely in white – or in solid shades of white and black – and any parts of the masked object which lie outside the mask, or which are colored black, will be clipped. Usually I recommend using a clip path in preference to a mask if you simply want a hard clipped edge, but, as we will see later in this article, there are times when it's easier to use a mask.
You may recall that the fill and stroke of a clip path are irrelevant to the final effect on the clipped object. With masks, however, the opposite is true – the color of each individual pixel is taken into account when calculating its effect on the masked object, so fills and strokes have significance.
Did I say pixel? In a vector format? In practice, there are few cases where an SVG file is actually used as a purely vector format. If you want to display an SVG file on a computer screen, or print it to an inkjet or laser printer, it is ultimately converted to pixels. Masking takes place during this final output step, so although the mask may be made up of vector objects, and the item being masked is also a vector, the final result is really generated only at the point that those vectors are converted to individual pixels for display or printing.
Using a thick-stroked rectangle and strictly black or white pixels, it's easy to see the difference between a clip path and the use of a mask to simulate clipping. In the top pair of images, I've used the rectangle as a clip path, and you can see that the result is aligned with the center line of the stroke. The second and third pairs of images show the rectangle being used as a mask, with only white parts remaining and black parts becoming transparent. It's especially clear in the last pair that the stroke has an effect.
By now it should be clear that clipping and masking are similar operations, so it's not really surprising that Inkscape exposes them with the same user interface. Just as with clipping, the masking object must be on top of the object to be masked, in terms of the z-index. You then select both items and choose Set Mask from the context menu, or Object > Mask > Set from the main menu. Releasing a masked object is as simple as selecting it (it will be described as “Masked” in the status bar) then choosing Release Mask from the context menu, or Object > Mask > Release from the main menu. Just as with clip paths, it is possible to edit masks using the node tool without releasing them first, but generally there's little benefit to doing so as you can edit only the outline shape of the path, not its color.
As you've seen, using a grayscale gradient as a mask gives you some control over the opacity of the masked object. You may also recall that Inkscape offers a selection of black and white patterns in its Fill & Stroke dialog, plus a few grayscale bitmap fill patterns. These may previously have seemed rather limiting, but their monochrome nature makes them perfect for use as a mask. The checkerboard pattern is a good example of a situation where it's easier to use a mask than a clip path. This same effect could be achieved by clipping with a complex path, but it's probably not worth the extra effort. Looking closely at the checkerboard, you'll notice that the fill isn't made up of black and white squares, but rather white and transparent. Using white and transparent can sometimes make it easier to see what parts of an object you are masking: white areas will remain visible, and transparent areas will be clipped. A translucent white area will have the same effect as a shade of gray. It may seem counter-intuitive to cover the parts of the object that you want to keep, and expose the parts that will be hidden, but that's the way that masking has been defined in the SVG specification, so that's the way it works in Inkscape.
Because clip paths are defined by a boundary and a concept of what's inside and outside that boundary, it's possible to use only a single path or object to define a clipping shape. Masks, on the other hand, are concerned with only the color of pixels, so it doesn't really matter whether your mask is made up of a single object or a whole load of objects inside a group. This allows you to create complex arrangements of colors and patterns which would be impossible to reproduce using a single path.
This example uses a group made up of a few random objects as a mask. You can see that the repeating radial gradient of the rectangle, made up of a white-to-transparent transition, results in a similar pattern being visible from the masked red circle. But by adding extra black and white features to the mask group, you can “override” the pattern with explicit sections of transparency and opacity. One thing to note is the place where the black line crosses the white one: because the black line is on top, the pixels at that position are black, so that part of the masked object is transparent. It doesn't matter what's going on within the group, all that matters is the color of the final pixels. This may not be the best piece of abstract art in the world, but it would have been a lot harder to draw without using masks.
Although you can use masks to create complex arrangements of clipping and transparency, probably the most common use is simply to add “feathering” to the edges of a group so that it fades into the background. In that case all you need for your mask design is a strongly blurred white ellipse, as demonstrated in this wholly unseasonal greeting card design.
Throughout this article I've referred to masks as being grayscale images. That's not strictly true – a mask can be any combination of colors that you want. However, anything other than white, black or gray will be converted into a grayscale image before it's used as a mask. This can make it difficult to predict the exact effect of a color on the resultant opacity, so I recommend using pure grayscales when drawing your masks in almost all cases.