How to color seperate parts of an SVG

I have a list of different colors and a SVG.
I’d like to pick a color and when i click on a specific part of the SVG, have only that part colored.
EXAMPLE:
colors: https://i.stack.imgur.com/77PTF.png,
original svg: https://i.stack.imgur.com/QMG4O.png,
colored svg: https://i.stack.imgur.com/XXFHr.png.

(1, 2, 3 are just to highlight the different parts to you, the coloring was done on Paint3D.)
(StackOverflow considers the links as code so i had to quote them, sorry for the inconvenience.)

I’m not sure how to go about it, i’ve found some PHP libraries such as imagick, imagickdraw and php-svg but none seem to do the trick.

Any help will be appreciated, doesn’t have to be in PHP, i’m open to any solution.
Thanks.