Skip to content

Instantly share code, notes, and snippets.

@johnfmorton
Last active August 29, 2015 14:13
Show Gist options
  • Save johnfmorton/3c509174abd9dccecb41 to your computer and use it in GitHub Desktop.
Save johnfmorton/3c509174abd9dccecb41 to your computer and use it in GitHub Desktop.
Simply way to set background color for a view using RGB color in 0-255
private func setBackgroundColor(redColor: Int, greenColor: Int, blueColor: Int) -> Bool{
// colors should be from 0 to 255, like you get when picking colors in Photoshop
if redColor < 0 || greenColor < 0 || blueColor < 0 {
println("At least one color value not in range. Must be at least 0")
return false
}
if redColor > 255 || greenColor > 255 || blueColor > 255 {
println("At least one color value not in range. Must be at less than 255")
return false
}
self.view.backgroundColor = UIColor(
red: CGFloat( Float(redColor+1) / Float(256) ),
green: CGFloat( Float(greenColor+1) / Float(256) ),
blue: CGFloat( Float(blueColor+1) / Float(256) ),
alpha: 1.0
)
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment