Is there a way to make a gradient based on an object or a image texture?
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.