Search code examples
iosiphonexcodeswiftuicolor

UIColor Made of Variables


I'm trying to make a Swift application in iOS that has three sliders, one for each red, green, and blue. Each time a slider is moved the current value is assigned to a variable. My problem is that I would like to set the background color to the variables that were set. The syntax that I'm familiar with is,

view.backgroundColor = UIColor(red: 102/255.0, green: 204/255.0, blue: 255/255.0, alpha: 1.0)

So what I'm trying to do in my head looks like,

view.backgroundColor = UIColor(red: returnedValueOfRedSlider/255.0, green: returnedValueOfGreenSlider/255.0, blue: returnedValueOfBlueSlider/255.0, alpha: 1.0)

but it has been made apparent that this is a big no no. So what can I do to use variables to make a UIColor to assign to the backgroundColor?

Thank you,


Solution

  • To make it clean / readable, why not something like:

    let red   = CGFloat(returnedValueOfRedSlider/255)
    let green = CGFloat(returnedValueOfGreenSlider/255)
    let blue  = CGFloat(returnedValueOfBlueSlider/255)
    view.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)