Skip to content

Instantly share code, notes, and snippets.

@yvesroos
Created March 5, 2015 21:53
Show Gist options
  • Save yvesroos/5a16bd53e64234a66747 to your computer and use it in GitHub Desktop.
Save yvesroos/5a16bd53e64234a66747 to your computer and use it in GitHub Desktop.
Create an array of gradient colors
func degradeForPosition(topColor: UIColor, bottomColor: UIColor, totalCount: Int, position: Int) -> UIColor{
var rTop: CGFloat=0, gTop: CGFloat=0, bTop: CGFloat=0, aTop: CGFloat=0
var rBottom: CGFloat=0, gBottom: CGFloat=0, bBottom: CGFloat=0, aBottom: CGFloat=0
topColor.getRed(&rTop, green: &gTop, blue: &bTop, alpha: &aTop)
bottomColor.getRed(&rBottom, green: &gBottom, blue: &bBottom, alpha: &aBottom)
let redConst : CGFloat! = (rTop - rBottom)/CGFloat(totalCount-1)
let blueConst : CGFloat! = (bTop - bBottom)/CGFloat(totalCount-1)
let greenConst : CGFloat! = (gTop - gBottom)/CGFloat(totalCount-1)
let redColor : CGFloat! = (rBottom + (redConst*CGFloat(position)));
let greenColor : CGFloat! = (gBottom + (greenConst*CGFloat(position)));
let blueColor : CGFloat! = (bBottom + (blueConst*CGFloat(position)));
let color: AnyObject = UIColor(red: redColor, green: greenColor, blue: blueColor, alpha: 1.0)
return color as UIColor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment