2014-09-19 204 views
1

我想創建一個自定義UIColor從綠色到紅色取決於百分比值,但我不知道我該怎麼做這樣的事情?顏色從綠色到紅色與百分比

任何想法?

+0

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

回答

3

沿着這些線路的東西應該工作,如果你只是想一個線性組合:

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

相關問題