Is there a way to make a gradient based on an object or a image texture? I made this image with a monkey head and I want to be able to make something like the second image with nodes procedurally [i made this just by duplicating a cutout]


I want to be able to take a spherical gradient, or the result of the vector math length function, then use an object or a image like the first one above and make a gradient that has a value of zero at the center and 1 at the edge of the object/mask image. Making the rainbow pattern procedurally is not part of the question btw that's just easier then trying to draw the gradient.
You can use the Dilate / Erode node in the Compositor Editor. Link to the docs
First setup your Object as needed and render the image.

Now you can play with several dilate / erode nodes to get the desired effect. It basically creates a dilated mask for each subsequent dilation and we're adding them , then dividing by the number of dilations, then plugging it into a Combine HSVA node to get rainbow colors. You can tweak it to get desired colors too.

There is a Distance and a Step mode in the Dilate / Erode node. I found the Distance mode to be more pleasing, but the compositing time will skyrocket on high values.