I don't know what kind of image you want to convert.
You could simply use rainbow gradient, if it is block or a simple shape.
There is a script called "path blend" that allows to render gradients between 2 paths.
Here's a routine I came up with, to convert a conventional 2-color image into rainbow colors.
Original

1. Colors->Desaturate: Luminosity
2. Colors->Auto: Equalize, or Normalize.

3. Switch to Blend/Gradient Tool. (In 2.8 Gradient is called Blend)
4. Pick a rainbow gradient. Note that I am using custom rainbow gradient, because I don't like how it turns with full spectrum gradient.
5. Colors->Map: Gradient Map.
