Xojo Template Images (dynamic icon colours)

I created the simple function below to use in place of g.DrawPicture to allow me to draw my icons in different colours depending on the new isDarkMode setting in Xojo 2018 R3.
I don’t know if this is the correct place to post this but hope it helps someone else who was looking for a quick way to adapt icon colours from a single image.

It takes a black and transparent only image and changes it to the colour set as g.forecolor. Half transparency etc works also.

This code should also work cross platform but I haven’t tried on other platforms and works the same way as g.DrawPicture.

Public Sub DrawTemplateImage(extends g As Graphics, Image As Picture, x As Integer, y As Integer, w1 As Integer = -10000, h1 As Integer = -10000, sx As Integer = 0, sy As Integer = 0, w2 As Integer = -10000, h2 As Integer = -10000)
  //Set g.forecolor to color you want icon to draw.
  //Icon must contain only black.
  //Transparency is allowed.
  //Use this function in place of g.DrawPicture, it works exactly the same way.
  Dim nPic As Picture = new Picture(Image.Width,Image.Height)
  Dim newsurf As RGBSurface = npic.RGBSurface
  Dim surf As RGBSurface = Image.RGBSurface
  dim rmap(255), gmap(255), bmap(255) As integer
  rmap(0) = g.ForeColor.Red
  gmap(0) = g.ForeColor.Green
  bmap(0) = g.ForeColor.Blue
  newsurf.Transform(rmap, gmap, bmap)
  g.DrawPicture(nPic, x,y,w1,h1, sx,sy,w2,h2)
End Sub

Or you could use native template images like in https://xojo.gitbook.io/add-ons/cstruecolors or I guess http://www.ohanaware.com/retinakit/ too. (And of course MBS)