2017-03-31 71 views

回答

0
extension UIColor {  
    // Calling this with 0.5 as argument returns a color whose saturation is 50% of that of the receiver. 
    func desaturatedBy(fraction: CGFloat) -> UIColor { 
    var hue: CGFloat = 0 
    var saturation: CGFloat = 0 
    var brightness: CGFloat = 0 
    var alpha: CGFloat = 0 
    let success = getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) 
    assert(success) 

    saturation *= fraction 

    return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: alpha) 
    } 
} 

這工作,因爲第e getter在擴展的sRGB色彩空間中返回HSB,而UIColor的初始化程序也在擴展的sRGB色彩空間中獲取HSB。所以我們不需要做任何色彩空間轉換。

0

考慮到您可以計算DCI-P3的歸一化主矩陣(NPM),它應該很簡單:NPM的中間一行代表亮度因子。我將舉例說明這一點使用Colour,它假設你正在使用線性值:

import numpy as np 
import colour 

# Computing the sRGB Luminance Equation, you should be familiar with the 
# resulting Luminance factors. 
print(colour.RGB_luminance_equation(
    colour.sRGB_COLOURSPACE.primaries, 
    colour.sRGB_COLOURSPACE.whitepoint)) 
# Y = 0.212639005872(R) + 0.715168678768(G) + 0.0721923153607(B) 

# Computing the DCI-P3 Luminance Equation. 
print(colour.RGB_luminance_equation(
    colour.DCI_P3_COLOURSPACE.primaries, 
    colour.DCI_P3_COLOURSPACE.whitepoint)) 
# Y = 0.209491677913(R) + 0.721595254161(G) + 0.0689130679262(B) 

# Computing Luminance of given RGB colour, this is assuming it is representing linear values. 
DCI_P3_LUMINANCE_FACTORS = np.array([0.209491677913, 0.721595254161, 0.0689130679262]) 
RGB = np.array([1.0, 0.0, 0.8]) 
Y = np.dot(RGB, DCI_P3_LUMINANCE_FACTORS) 
print(Y) 
# 0.264622132254 

隨着Ÿ代表您給出顏色的亮度,如果你想在50%飽和度,你可以做這樣的事情:

線性插值(RGB,[Y,Y,Y],0.5)

+0

有沒有辦法在iOS中做到這一點?謝謝。 –

+0

很可能,你真正需要的是能夠做點積和線性插值。 –

+0

順便說一句,Python發出一個錯誤,指出在導入顏色中找不到顏色。我必須安裝哪些MacPorts模塊?我正在使用Python 3.6。我已經安裝了numpy,因爲那也是一個錯誤。謝謝。 –

相關問題