Last active
September 16, 2021 23:01
-
-
Save zakbarlow1995/eb399beedc38a699c961453b227b6d3e to your computer and use it in GitHub Desktop.
UIColor extension to add .random & .inverted static and computed variables, respectively.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// Extension adding class var random & computed property inverted to UIColor | |
extension UIColor { | |
static var random: UIColor { | |
UIColor(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1), alpha: .random(in: 0...1)) | |
} | |
static var randomSolid: UIColor { | |
UIColor(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1), alpha: 1.0) | |
} | |
var inverted: UIColor { | |
var a: CGFloat = 0.0, r: CGFloat = 0.0, g: CGFloat = 0.0, b: CGFloat = 0.0 | |
return getRed(&r, green: &g, blue: &b, alpha: &a) ? UIColor(red: 1.0-r, green: 1.0-g, blue: 1.0-b, alpha: a) : .black | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage, changing a
view
's background color:view.backgroundColor = .blue.inverted //Results in yellow background color
*view.backgroundColor = .random //Results in random background color
*
.blue.inverted
may need to be prefixed withUIColor
, depending on Xcode version