Last active
February 9, 2021 21:32
-
-
Save JaggerJo/070b21681a97946f077a89f23e89da1e to your computer and use it in GitHub Desktop.
Tinting Images (SF Symbols, App Kit, 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
[<AutoOpen>] | |
module NSImageExtensions = | |
open AppKit | |
type NSImage with | |
static member GetSystemSymbolTinted (symbolName: string, accessibilityDescription: string, tint: NSColor) : NSImage = | |
let original = NSImage.GetSystemSymbol(symbolName, accessibilityDescription) | |
NSImage.ImageWithSize(original.Size, false, (fun bounds -> | |
let ctx = NSGraphicsContext.CurrentContext.CGContext | |
// using `original.CGImage` does return null. use `AsCGImage` instead in this case. | |
let mask = original.AsCGImage(ref bounds, NSGraphicsContext.CurrentContext, null) | |
ctx.ClipToMask(bounds, mask) | |
ctx.SetFillColor (tint.CGColor) | |
ctx.FillRect(bounds) | |
// optional | |
ctx.Flush() | |
true | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment