Skip to content

Instantly share code, notes, and snippets.

@usagimaru
Last active April 16, 2024 19:15
Show Gist options
  • Save usagimaru/ca28d5284c2b6a63b07396c0c6971775 to your computer and use it in GitHub Desktop.
Save usagimaru/ca28d5284c2b6a63b07396c0c6971775 to your computer and use it in GitHub Desktop.
Dim NSImage
import Cocoa
extension NSImage {
/// Return dimmed NSImage with level (0.0–1.0)
func dimmed(_ level: CGFloat) -> NSImage {
NSImage(size: self.size, flipped: false) { rect in
let imageRect = NSRect(.zero, self.size)
// Draw image
self.draw(in: imageRect)
// Draw transparent black color over the image
NSColor(deviceWhite: 0, alpha: level).set()
imageRect.fill(using: .sourceAtop)
return true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment