Instance Method

luminanceToAlpha()

Adds a luminance to alpha effect to this view.

Declaration

func luminanceToAlpha() -> some View

Return Value

A view that applies a luminance to alpha effect to this view.

Discussion

The luminanceToAlpha() modifier creates a semitransparent mask out of the view to which you apply it. The dark regions in a view become transparent, and the bright regions become opaque black. Medium brightness regions become a partially opaque gray color.

The example below shows two views: the first is a red square with an overlaid green circle; the second is a copy of the first view with where the luminanceToAlpha() modifier’s masking operation transforms the view’s brightness levels into an equivalent grayscale version of the first view.

struct LuminanceCircleView: View {
    var body: some View {
        Circle()
            .fill(Color.green)
            .frame(width: 40, height: 40, alignment: .center)
    }
}

struct LuminanceToAlpha: View {
    var body: some View {
        HStack {
            Color.red.frame(width: 60, height: 60, alignment: .center)
                .overlay(LuminanceCircleView(), alignment: .center)
                .padding()

            Color.red.frame(width: 60, height: 60, alignment: .center)
                .overlay(LuminanceCircleView(), alignment: .center)
                .luminanceToAlpha()
        }
    }
}
The example below shows two views: the first is a red square with an overlaid green circle; the second is a copy of the first view with where the luminanceToAlpha() modifier transforms the view’s brightness levels into an equivalent grayscale version of the view.

See Also

Applying Graphical Effects to a View

func blur(radius: CGFloat, opaque: Bool) -> View

Applies a Gaussian blur to the view.

func opacity(Double) -> View

Sets the transparency of the view.

func brightness(Double) -> View

Brightens the view by the specified amount.

func contrast(Double) -> View

Sets the contrast and separation between similar colors in this view.

func colorInvert() -> View

Inverts the colors in this view.

func colorMultiply(Color) -> View

Adds a color multiplication effect to this view.

func saturation(Double) -> View

Adjusts the color saturation of this view.

func grayscale(Double) -> View

Adds a grayscale effect to this view.

func hueRotation(Angle) -> View

Applies a hue rotation effect to this view.