Zip Carolbar Women's Boots Grey Western Concise Short Flat Buckle Casual rraIpqxSolid regions of color have their place, but for many graphics, you want to add a little bit of subtlety. Transparency is one way to do so, allowing you to paint a shape without completely obscuring the content from previous layers of the drawing.
Transparency is in some ways another facet of color, and in some ways a distinct and much more complex topic. This is reflected in the different ways you can define transparency, which we’ll compare and contrast in this chapter.
One thing is constant when talking about transparency and web design: you don’t talk about transparency, you talk about opacity. The two concepts are direct opposites: when something is fully opaque, it is not at all transparent, and when something has zero opacity, it is fully transparent and therefore invisible.
Closed AgooLar Pull Boots PU on High Beige Solid Toe Women's Round Heels 16qc1CR8wBoot Fashion Fox Mid Calf Sole Yellow Faux Winter Cuff Fur Snow Chunky Womens Boots Ladies Shoes Clode® Leather q5wESySVG uses three distinct properties to control opacity of basic shapes and text:
stroke-opacity. All of these can be set with presentation attributes or with CSS style rules.
CSS Color Module Level 3 extended the
Pascal Maroon Velvet Red Martens Dr Boots Ankle opacity property to apply to all content. Pearl Metallic Angkorly Boots Shoes high Women's Fashion Block Heel Booty Platform Grey 9 cm Ankle 80r0UxIslipper Hellblau 626 women's design Italian slippers 6w5vnIIn addition, rather than introduce
*-opacity properties for every facet of CSS painting, this module introduced new color functions. Partially transparent color can be defined using the
hsla() color functions, and then used anywhere in CSS where a color value is needed.
Opacity on the Web is always expressed as a decimal number between 0.0 (invisible) and 1.0 (solid, no transparency). These numbers are also known as alpha values, particularly when discussing opacity as an intrinsic part of colors or images. The
hsla() refers to the alpha channel. These functions take four values, instead of the usual three, with the fourth being the alpha value between 0 and 1.
transparent, which formerly had a special meaning for CSS backgrounds, has been redefined as a named color. Equivalent to
rgba(0,0,0,0), it can be used like any other color keyword in browsers that support CSS 3 colors.
opacity property applies to the element it is set on—even if that is a
element—and is not inherited. It takes the final drawn result for that element, including all its child content, and makes it uniformly more transparent.
opacity value is applied after determining the final color at every point where two shapes—or fill and stroke of the same shape—overlap.
Blink Women’s Boots Tobi Black 01 Black Ankle 66aqnT7r1Black top Boots Low Solid Heels Kitten Lace Women's PU AgooLar up xv4HfSetting
opacity to less than 1 creates a stacking context, flattening and containing all child content. In CSS layout, this can significantly affect the position of elements. It has no similar effect in SVG 1.1 content, but in SVG 2 it will affect
z-index stacking and will flatten all 3D transformations.
In contrast, when you set
fill-opacity, or when you use
hsla color functions, the transparent effect is applied at the time each shape is drawn, to that colored section only. The
fill-opacity properties are both inherited by default.
Figure 4-1 demonstrates the difference, using a figure-8 path with a thick green stroke and a yellow fill, partially overlapping a blue-and-purple ellipse whose opacity does not change. The green and yellow shape is shown at full opacity (top left) and set to half opacity using
fill-opacity (top right),
opacity on a
element (bottom left), or
rgba colors for the stroke and fill (bottom right). Example 4-1 gives the code.
The reused shapes are predefined in a
section. The background ellipse has all its painting properties defined as presentation attributes.
has a specified
stroke-width but will otherwise inherit its painting styles.
block is used to style the text labels and prevent cropping of the nested
elements. Note that the 7px
font-size will be interpreted within the local coordinate system, and so will not create unusually small type.
re-creates the same local coordinate system in a different quadrant of the main graphic, so that the reused graphics can be positioned in the same way.
The background element is reused as is within each sample, and then the foreground layered on top. In this first example, the foreground is given solid fill and stroke colors.
elements position the content in the other quadrants of the graphic, and use different presentation attributes on the foreground
opacity setting, it will apply directly on the
element as a combined group, instead of inheriting to the reused graphic.
Figure 4-1 also demonstrates some of the concepts we discussed in Chapter 2. The strokes are centered over the edge of each shape. When the stroke is partially transparent (due to
stroke-opacity or an
rgba color), this creates a two-toned color effect, with the fill visible under the inside part of the stroke.
Another feature to note is that the overlapping sections of stroke do not have a different color tone, compared to areas where the stroke only passes once. Conceptually, we like to think of strokes as if they were made by a marker or paint brush tracing out the lines. If that were true, these double-stroked regions would have twice as intense color, the way it would if you used translucent watercolor paint and passed your brush over the same region twice. However, the computer works by caculating out the total area of the stroke and then applying the coloring evenly, as if it was cut out of a sheet of semitransparent plastic.
With all these different ways of controlling opacity, what happens when you combine them? The effect is multiplied. For example, the following circle has its fill made partially transparent in two different ways:
Red Ankle Dr Boots Pascal Maroon Velvet Martens
The effect would be exactly the same with the following code, because 0.5×0.6=0.3:
It gets a little more complicated with
opacity, because the transparency is applied after combining the other colors. Nonetheless, an
opacity: 0.5 property still causes the alpha value of each pixel in the shape to be cut in half (multiplied by 0.5), after applying the opacity levels from all other properties.
opacity property is applied using a simple mathematical adjustment of each pixel value, it can be efficiently implemented by the graphical processing unit (GPU) of most video cards. Changes in opacity can often be animated quite smoothly as a result, although not all browsers take advantage of this for SVG content.
Partially transparent colors,
Maroon Dr Pascal Boots Velvet Martens Ankle Red fill-opacityStone Beige Jane Ballet 221 285 Beige Flats Klain Women’s 793 q8g8THZw, and
stroke-opacity do not create the same optimization, because they only affect parts of an element. However, if you are not animating the opacity, these properties can be more performant because they do not force the creation of a stacking context.
Women’s Sue 415 8894 Buffalo Boots 01 Brown Tan vtgp6qwSide Zipper Black 109 EKS suede Ankle Dress Women's Shoes Heel J High Boot Platform Hidden pOzCqwvAs mentioned in Chapter 3 (“Mixing and Matching”), the color created by a partially opaque object is calculated using the sRGB model. According to the specifications, it should be affected by the
color-interpolation mode, but sRGB is the default and the only mode supported in most software used to view SVG.
The final color displayed when layers overlap is generated by, first, scaling both colors according to the sRGB model, then taking a weighted average between the background and foreground—where the alpha value is the weight of the foreground—and finally reversing the sRGB scale. This calculation works regardless of whether the background color was created from a single element or from blending multiple partially transparent ones.
This method of blending colors is known as simple alpha compositing. In many graphics programs, it is also known as the “normal” blending mode.
Heels Boots Low Zipper Women's Toe AgooLar PU Black Solid Round T6wIxqUThe colors created when using simple alpha blending directly correspond to colors in an sRGB gradient between the background color and the solid color of the object. The alpha value determines the distance along the gradient. If alpha is zero, and the object is completely transparent, the initial value of the gradient is used—in other words, the background color. If alpha is 0.5, the midpoint of the gradient is used. If alpha is 1 (the object is completely opaque), the displayed color is the same as the end point of the gradient—the foreground object’s own color.
Figure 4-2 demonstrates this relationship by comparing a smooth gradient from red (
#F00) to blue (
#00F) with the result of layering blue squares overtop of a solid red rectangle, where the squares vary in their fill-opacity values.
It may appear to you as if the squares in Figure 4-2 were also filled with gradients, transitioning in the opposite direction. This is a well-recognized optical illusion. Your eyes and brain enhance the contrast between colors on either side of an edge. When a solid-colored element has different contrasting colors on each side—or a gradient on one side—the contrast enhancement is perceived as a gradient.
To confirm that you really are looking at a solid color, use pieces of paper (all the same color) to cover up the adjacent sections of the figure, until you only see a single, solid-color square.
Chapter 6 will introduce the
elements used in Example 4-2 and describe how they can be adjusted to create different gradient effects. But first, Chapter 5 will explore what it means in SVG to fill one element with a URL reference to another.