Declaration
compose().grayscale(amount: number)nonisolated func grayscale(_ amount: Double) -> some ViewParameters
amount*numberThe intensity of grayscale to apply from 0.0 to less than 1.0. Values closer to 0.0 are more colorful, and values closer to 1.0 are less colorful.
Return value
A view that adds a grayscale effect to this view.
Discussion
A grayscale effect reduces the intensity of colors in this view.
The example below shows a series of red squares with their grayscale effect increasing from 0 (reddest) to 99% (fully desaturated) in approximate 20% increments:
struct Saturation: View {
var body: some View {
HStack {
ForEach(0..<6) {
Color.red.frame(width: 60, height: 60, alignment: .center)
.grayscale(Double($0) * 0.1999)
.overlay(Text("\(Double($0) * 0.1999 * 100, specifier: "%.4f")%"),
alignment: .bottom)
.border(Color.gray)
}
}
}
}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.