Skip to content

Instantly share code, notes, and snippets.

@JaggerJo
Last active February 9, 2021 21:32
Show Gist options
  • Save JaggerJo/070b21681a97946f077a89f23e89da1e to your computer and use it in GitHub Desktop.
Save JaggerJo/070b21681a97946f077a89f23e89da1e to your computer and use it in GitHub Desktop.
Tinting Images (SF Symbols, App Kit, NSImage)
[<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