swift - How to guarantee that an NSColor has enough saturation? -
i need guarantee color has enough saturation.
i made extension nscolor tests give me weird values: tempcolor seems have lots of saturation.
so function returns self?
where's mistake?
i've tried without converting nscalibratedrgbcolorspace didn't , input color can of different type anyway.
extension nscolor { func withminimumsaturation(minimumsaturation: cgfloat) -> nscolor { // color hue/rgb/other convert rgb if var tempcolor = self.colorusingcolorspacename(nscalibratedrgbcolorspace) { // prepare values var hue: cgfloat = 0.0 var saturation: cgfloat = 0.0 var brightness: cgfloat = 0.0 var alpha: cgfloat = 0.0 // populate values tempcolor.gethue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) // if color not enough saturated if saturation < minimumsaturation { // return same color more saturation return nscolor(calibratedhue: hue, saturation: minimumsaturation, brightness: brightness, alpha: alpha) } } // if detection fails, return same color return self } } usage:
let source = mycolorfromalistofcolors // nscolor let guaranteed = source.withminimumsaturation(0.15) edit: works. i've accepted aaron's answer because helped me understand situation (i comparing colors redcomponents , similar).
your method seems fine.
for colors low saturation, returns different color:

if increase minimum saturation 0.15 more significant, 0.45, change more significant:

so, method works expect would. i'm guessing need tweak 0.15 input results want.
it may log old , new saturations debugging:
if saturation < minimumsaturation { // return same color more saturation println("new saturation: \(minimumsaturation)") return nscolor(calibratedhue: hue, saturation: minimumsaturation, brightness: brightness, alpha: alpha) } else { println("discarding saturation: \(saturation)") }
Comments
Post a Comment