Skip to content

Instantly share code, notes, and snippets.

Last active August 6, 2018 08:47
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
The extension to return the complement colour or two complement colours of a colour in Swift.
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