2017-03-31 1 views

Antwort

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) 
    } 
} 

Das funktioniert, weil th Der Getter gibt HSB im erweiterten sRGB-Farbraum zurück, und der Initialisierer von UIColor nutzt HSB auch im erweiterten sRGB-Farbraum. Wir müssen also keine Farbraumkonvertierung durchführen.

0

Es sollte einfach sein, vorausgesetzt, Sie können die Normalized Primary Matrix (NPM) von DCI-P3 berechnen: die mittlere Reihe des NPM repräsentiert die Luminanzfaktoren. Ich werde zeigen diese Colour verwenden, es wird davon ausgegangen, dass Sie lineare Werte verwenden:

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 

Mit Y die Helligkeit des gegebenen Farbe darstellt, wenn man bei 50% bis desaturate wollte man so etwas tun könnte:

Lerp (RGB, [Y, Y, Y], 0,5)

+0

Gibt es eine Möglichkeit, dies in iOS zu tun? Vielen Dank. –

+0

Sehr wahrscheinlich, was Sie wirklich brauchen, ist ein Punktprodukt und eine lineare Interpolation. –

+0

BTW, Python signalisiert einen Fehler, der besagt, dass Farbe in der Importfarbe nicht gefunden wird. Welches MacPorts-Modul muss ich installieren? Ich benutze Python 3.6. Ich habe bereits numpy installiert, weil das auch einen Fehler gab. Vielen Dank. –

Verwandte Themen