Last active
August 6, 2018 08:47
-
-
Save dariukas/a166d947dce801ee1b82452cd9d61d4b to your computer and use it in GitHub Desktop.
The extension to return the complement colour or two complement colours of a colour in Swift.
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
extension UIColor { | |
convenience init(red: Int, green: Int, blue: Int) { | |
let newRed = CGFloat(red)/255 | |
let newGreen = CGFloat(green)/255 | |
let newBlue = CGFloat(blue)/255 | |
self.init(red: newRed, green: newGreen, blue: newBlue, alpha: 1.0) | |
} | |
var complement: UIColor { | |
return self.withHueOffset(0.5) | |
} | |
var splitComplement0: UIColor { | |
return self.withHueOffset(150 / 360) | |
} | |
var splitComplement1: UIColor { | |
return self.withHueOffset(210 / 360) | |
} | |
private func withHueOffset(_ offset: CGFloat) -> UIColor { | |
var h: CGFloat = 0 | |
var s: CGFloat = 0 | |
var b: CGFloat = 0 | |
var a: CGFloat = 0 | |
self.getHue(&h, saturation: &s, brightness: &b, alpha: &a) | |
return UIColor(hue: fmod(h + offset, 1), saturation: s, brightness: b, alpha: a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment