Skip to content

Instantly share code, notes, and snippets.

@seivan
Created June 17, 2014 19:44
Show Gist options
  • Save seivan/fdf5fccc6b674ba01333 to your computer and use it in GitHub Desktop.
Save seivan/fdf5fccc6b674ba01333 to your computer and use it in GitHub Desktop.
// Use Swift operator overloading to blend two UIColors with the addition operator
import UIKit
@infix func + (left: UIColor, right: UIColor) -> UIColor {
var leftRGBA = CGFloat[](count: 4, repeatedValue: 0.0)
var rightRGBA = CGFloat[](count: 4, repeatedValue: 0.0)
left.getRed(&leftRGBA[0], green: &leftRGBA[1], blue: &leftRGBA[2], alpha: &leftRGBA[3])
right.getRed(&rightRGBA[0], green: &rightRGBA[1], blue: &rightRGBA[2], alpha: &rightRGBA[3])
return UIColor(
red: (leftRGBA[0] + rightRGBA[0]) / 2,
green: (leftRGBA[1] + rightRGBA[1]) / 2,
blue: (leftRGBA[2] + rightRGBA[2]) / 2,
alpha: (leftRGBA[3] + rightRGBA[3]) / 2
)
}
UIColor.yellowColor() + UIColor.redColor()
UIColor.greenColor() + UIColor.blueColor()
UIColor.purpleColor() + UIColor.orangeColor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment