Coloring a PNG image

I’m looking to manage the color of a PNG file by code. I have a folder black PNG file and I want to change it’s color by code red, blue, etc…

If it’s a black line PNG with a transparent background this should do it:

  1. Draw png into same size picture with white background
  2. Draw picture with the colour you want the line to be
  3. Apply the first png to the second png with ApplyMask

The second picture should now be your lineicon in the desired colour.

Sorry @Tim_Parnell I don’t really understand your solution.

My image is like this

Can you wrote some code to change the black color to blue, red, etc. ?

Here’s a sample project to help you get started.
Note that this will only work with black source icons.

Colourizer.xojo_xml_project (7kb)

You can use rgbsurface.transform to do the job too.
It allows for a bit of flex

dim rmap(255) as integer
dim bmap(255) as integer
dim gmap(255) as integer

for y as integer = 0 to 255
  rmap(y) = y
  gmap(y) = y
  bmap(y) = y

dim newcolor as color = &cff1010
      rmap(0) =
      bmap(0) =
      gmap(0) =

//if you have greys at the edges, you might extend this to 
      'rmap(1) =
     ' bmap(1) =
     ' gmap(1) =

      'rmap(2) =
     ' bmap(2) =
      'gmap(3) =

//swap black for red
copyofblackimage.rgbsurface.transform rmap(),gmap(),bmap()

And don’t forget the simple but slow way:

dim rgb as rgbsurface
dim newcolor as color = &cFF0000
rgb = theimage.rgbsurface
for y as integer = 0 to theimage.height
for x as integer = 0 to theimage.width
if rgb.pixel(x,y).value < 0.1 then rgb.pixel(x,y) = newcolor

I usually do it with picture.systemImage.

var p as picture = picture.systemImage("", dblSize, picture.systemImageWeights.regular, color.RGB(255, 0, 0), yourPicture)
  • dblSize is the size of the picture, try 20 or 100 to see if there is a difference in the result
  • color.RGB(255, 0, 0) is the color you want to have as the result
  • yourPicture is your Picture you want to change

It seems to be an iOS solution only

Thank you everybody for your help ^^

Here is my final solution

Var iconMask As Picture = ImgFolder
Var coloredIcon As New Picture(iconMask.Width, iconMask.Height)

// Your desired line color
coloredIcon.Graphics.DrawingColor = &cff5555
coloredIcon.Graphics.FillRectangle(0, 0, iconMask.Width, iconMask.Height)

// Apply the mask

// Draw Picture
g.DrawPicture(coloredIcon, 0, (g.Height - 16) \ 2, 16, 16, 0, 0, coloredIcon.Width, coloredIcon.Height)

As you can see I store my image in the my project and name it ImgFolder.

I thing you can close this topic