title: set image color of a template image
body: <p>I have an image like this:
  <a href="https://i.stack.imgur.com/6pQeS.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6pQeS.png" alt="enter image description here"></a></p>
  
  <p>(Rendered as a template image)</p>
  
  <p>I tried this code:</p>
  
  <pre><code>@IBOutlet weak var imgAdd: NSImageView!
  imgAdd.layer?.backgroundColor = CGColor.white
  </code></pre>
  
  <p>Which only changes the background color of course.</p>
  
  <p>Is there a way to change the color of this image programmatically?</p>
  
  <hr>
  
  <p>So far I've tried the code below which doesn't work. (The image color doesn't change.)</p>
  
  <pre><code>func tintedImage(_ image: NSImage, tint: NSColor) -&gt; NSImage {
      guard let tinted = image.copy() as? NSImage else { return image }
      tinted.lockFocus()
      tint.set()
  
      let imageRect = NSRect(origin: NSZeroPoint, size: image.size)
      NSRectFillUsingOperation(imageRect, .sourceAtop)
  
      tinted.unlockFocus()
      return tinted
  }
  
  imgDok.image = tintedImage(NSImage(named: "myImage")!, tint: NSColor.red)
  </code></pre>