Created
March 11, 2015 21:08
-
-
Save mikekavouras/79264e35b6b125b72ced to your computer and use it in GitHub Desktop.
Get the color at a specific % between 2 colors
This file contains hidden or 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
| class func colorAtPercentage(color1: UIColor, color2: UIColor, perc: CGFloat) -> UIColor { | |
| let firstComp = CGColorGetComponents(color1.CGColor) | |
| let secondComp = CGColorGetComponents(color2.CGColor) | |
| let red1 = firstComp[0] | |
| let red2 = secondComp[0] | |
| let newRed = numbers(red1, num2: red2, perc: perc) | |
| let green1 = firstComp[1] | |
| let green2 = secondComp[1] | |
| let newGreen = numbers(green1, num2: green2, perc: perc) | |
| let blue1 = firstComp[2] | |
| let blue2 = secondComp[2] | |
| let newBlue = numbers(blue1, num2: blue2, perc: perc) | |
| return UIColor(red: newRed, green: newGreen, blue: newBlue, alpha: 1.0) | |
| } | |
| class private func numbers(num: CGFloat, num2: CGFloat, perc: CGFloat) -> CGFloat { | |
| let floor = min(num, num2) | |
| let ceil = max(num, num2) | |
| let val = (ceil - floor) * perc | |
| return num > num2 ? ceil - val : floor + val | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment