颜色从绿色到红色与百分比

问题描述:

我想创建一个自定义UIColor从绿色到红色取决于百分比值,但我不知道我该怎么做这样的事情?颜色从绿色到红色与百分比

任何想法?

+0

类似[this](http://*.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string/3532264#3532264)。 – Avis 2014-09-19 22:42:57

沿着这些线路的东西应该工作,如果你只是想一个线性组合:

func mixGreenAndRed(greenAmount: Float) -> UIColor { 
    return UIColor(red: (1.0 - greenAmount), green: greenAmount, blue: 0.0, alpha: 1.0) 
} 

那一个将通过RGB混合(0.5,0.5,0),虽然,是一种丑陋的橙所以你可能要做到这一点,而不是,这将只是调整红色和绿色的色调,通过黄,让你改变饱和度/亮度,以你的口味:

func mixGreenAndRed(greenAmount: Float) -> UIColor { 
    // the hues between red and green go from 0…1/3, so we can just divide percentageGreen by 3 to mix between them 
    return UIColor(hue: greenAmount/3, saturation: 1.0, brightness: 1.0, alpha: 1.0) 
} 

在这两种情况下greenAmount应该是0.0之间的值-1.0,而不是0-100。

+0

谢谢 - 工作正常! – derdida 2014-09-19 23:54:03

+0

百分比绿色是什么? – 2ank3th 2017-07-04 22:55:03

+0

纠正错误,谢谢 – 2017-07-05 03:30:02