Created
March 31, 2017 20:58
-
-
Save AquaGeek/289a9e59bc1525b54e0c18ad86844dc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NSColor { | |
// Derived from logic in TextMate | |
func soften(color inColor: NSColor, factor: CGFloat) -> NSColor { | |
guard let color = inColor.usingColorSpace(NSColorSpace.sRGB) else { | |
return inColor | |
} | |
var red = color.redComponent | |
var green = color.greenComponent | |
var blue = color.blueComponent | |
let alpha = color.alphaComponent | |
let isDark = 0.3 * red + 0.59 * green + 0.11 * blue < 0.5 | |
if isDark { | |
red = 1 - factor * (1 - red) | |
green = 1 - factor * (1 - green) | |
blue = 1 - factor * (1 - blue) | |
} else { | |
red *= factor | |
green *= factor | |
blue *= factor | |
} | |
return NSColor(colorSpace: color.colorSpace, components: [red, green, blue, alpha], count: 4) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment