Search code examples
swiftswiftuiscreenpixelbrightness

Find out screen brightness at touch point


Are there ways to find out the brightness of the pixel at the touch point, for example if I want to know the brightness of the pixels within each of these shapes on the screen?

struct ContentView: View {

@State var brightness = 0.0

var body: some View {
    
    VStack {
        
        Rectangle()
            .frame(width: 100,height: 100)
            .foregroundColor(.black)
        
        Circle()
            .frame(width: 100,height: 100)
            .foregroundColor(.gray)
        
        RoundedRectangle(cornerRadius: 30)
            .frame(width: 100,height: 100)
            .foregroundColor(.gray.opacity(0.5))
        
        Text("brightness = \(brightness)")
            .font(.largeTitle)
    }
  }
}

Solution

  • As far as I know, there is currently no way to do so. However you can try it with Core Graphics, but you will have to convert your Shapes as a CGImage. I suggest you take a look at the Core Graphics documentation Core Graphics