Last active
April 16, 2024 19:15
-
-
Save usagimaru/ca28d5284c2b6a63b07396c0c6971775 to your computer and use it in GitHub Desktop.
Dim NSImage
This file contains hidden or 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
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