颜色从绿色到红色与百分比
答
沿着这些线路的东西应该工作,如果你只是想一个线性组合:
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。
类似[this](http://*.com/questions/1560081/how-can-i-create-a-uicolor-from-a-hex-string/3532264#3532264)。 – Avis 2014-09-19 22:42:57