Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With swift, is it possible to access the invert colors function that’s in accessibility?

Tags:

xcode

ios

swift

The function that apple has already put in the phone that is in general>accessibility>invert colors, can I somehow use that in my program so for say when the user touches the screen the colors invert?

like image 839
Jacob Peterson Avatar asked Feb 10 '23 11:02

Jacob Peterson


1 Answers

I don't know of a way to do this automatically, but you could invert colors yourself using an extension on UIColor and accessing the subviews?

extension UIColor {
    var inverted: UIColor {
        var r: CGFloat = 0.0, g: CGFloat = 0.0, b: CGFloat = 0.0, a: CGFloat = 0.0
        self.getRed(&r, green: &g, blue: &b, alpha: &a)
        return UIColor(red: (1 - r), green: (1 - g), blue: (1 - b), alpha: a) // Assuming you want the same alpha value.
    }
}

And then if you want to update specific properties of the views you could do something like this:

  view.subviews.map { $0.backgroundColor = $0.backgroundColor.invertedColor }
  // And so on to change things like the tint color, text color, etc.

Sorry, I don't know a way to do this directly but till then this is better than nothing I guess.

like image 188
Manav Gabhawala Avatar answered May 14 '23 09:05

Manav Gabhawala