Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created March 31, 2017 20:58
Show Gist options
  • Save AquaGeek/289a9e59bc1525b54e0c18ad86844dc1 to your computer and use it in GitHub Desktop.
Save AquaGeek/289a9e59bc1525b54e0c18ad86844dc1 to your computer and use it in GitHub Desktop.
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