Reputation: 4060
I am working on the background colour of Views. Each view has different intensity level from 0 to 1. Now I have to set background colour as red based on Views intensity. I have written below code for red colour based on intensity
view.backgroundColor = UIColor(colorLiteralRed: Float(1.0 - cellAlpha), green: 255.0, blue: 255.0, alpha: 1) //UIColor.red intensity based on views
But I am unable to set red colour intensity using above?
Can anyone suggest how to set the intensity of a colour based on the float to vary from 0 to 1.? Please let me know If I have to explain my question.
Upvotes: 0
Views: 588
Reputation: 22946
Assuming cellAlpha
is between 0 and 1:
UIColor(colorLiteralRed: CGFloat(255.0 - cellAlpha * 255.0), green: 255.0, blue: 255.0, alpha: 1)
Note that it's CGFloat
, not Float
. But better to save you all these 255.0
's:
UIColor(red: CGFloat(1.0 - cellAlpha), green: 1.0, blue: 1.0, alpha: 1.0)
However, it sounds as if you want to change the redness only. For this you'd need to use:
UIColor(hue: 0.0, saturation: CGFloat(1.0 - cellAlpha), brightness: 1.0 , alpha: 1.0)
I just put your CGFloat(1.0 - cellAlpha)
as saturation; you probably need to adjust/correct that.
Upvotes: 1
Reputation: 2738
Notice how the other values go up to 255. You need to scale your value up to that as well.
let redScale = 255.0 * (1.0 - Float(cellAlpha))
view.backgroundColor = UIColor(colorLiteralRed: redScale, green: 255.0, blue: 255.0, alpha: 1)
Upvotes: 0