Declaration
compose().luminanceToAlpha()nonisolated func luminanceToAlpha() -> some ViewReturn value
A view with the luminance to alpha effect applied.
Discussion
Use this modifier to create a semitransparent mask, with the opacity of each part of the modified view controlled by the luminance of the corresponding part of the original view. Regions of lower luminance become more transparent, while higher luminance yields greater opacity.
In particular, the modifier maps the red, green, and blue components of
each input pixel's color to a grayscale value, and that value becomes
the alpha component of a black pixel in the output. This modifier
produces an effect that's equivalent to using the feColorMatrix
filter primitive with the luminanceToAlpha type attribute, as defined
by the Scalable Vector Graphics (SVG) 2
specification.
The example below defines a Palette view as a series of rectangles,
each composed as a Color with a particular white value,
and then displays two versions of the palette over a blue background:
struct Palette: View {
var body: some View {
HStack(spacing: 0) {
ForEach(0..<10) { index in
Color(white: Double(index) / Double(9))
.frame(width: 20, height: 40)
}
}
}
}
struct LuminanceToAlphaExample: View {
var body: some View {
VStack(spacing: 20) {
Palette()
Palette()
.luminanceToAlpha()
}
.padding()
.background(.blue)
}
}The unmodified version of the palette contains rectangles that range
from solid black to solid white, thus with increasing luminance. The
second version of the palette, which has the luminanceToAlpha()
modifier applied, allows the background to show through in an amount
that corresponds inversely to the luminance of the input.
See also
More in Visual effects.
- allowedDynamicRangeReturns a new view configured with the specified allowed dynamic range.
- backgroundExtensionEffectAdds the background extension effect to the view. The view will be duplicated into mirrored copies which will be placed around the view on any edge with available safe area. Additionally, a blur effect will be applied on top to blur out the copies.
- blurApplies a Gaussian blur to this view.
- brightnessBrightens this view by the specified amount.
- clippedClips this view to its bounding rectangular frame.
- colorEffectReturns a new view that applies shader to self as a filter effect on the color of each pixel.
- colorInvertInverts the colors in this view.
- colorMultiplyAdds a color multiplication effect to this view.
- compositingGroupWraps this view in a compositing group.
- contrastSets the contrast and separation between similar colors in this view.
- distortionEffectReturns a new view that applies shader to self as a geometric distortion effect on the location of each pixel.
- drawingGroupComposites this view's contents into an offscreen image before final display.