We have got two samples how the wanted colorfulness ranking method should work: Colorless BW photo should give a low number and a rainbow which has some colors which at least on a normal sRGB screen look quite bright and saturated should give a big number.
You have already got a hint to redirect your activity to a scientific website and to use CIELAB color system which is designed to be a math model of how colors are seen. RGB and its alternative formulations HSL and HSV (=HSB in Photoshop) present how colors are produced in computer screens.
The suggestion is not bad because most of us have quite thin understanding of the mathematical theory of color perception. We are more effective in making and mangling images and layouts.
But you will get a colorfulness ranking system. The next method calculates in Photoshop how colorful a RGB image is when it's compared to the maximum color producing capacity of the RGB system. It's not perceptual, it shows how high percentage of the color producing capacity is used.
It gives 100% if the image happens to have only maximally bright 100% saturated colors such as RGB=255,0,0 or RGB=255,255,0 or RGB=0,0,255.
Both reduced saturation and reduced brightness reduces the colorfulness in this ranking system. BW image has zero colorfulness.
These are achieved by multiplying the saturation with the brightness in every pixel and by calculating the average over the whole image.
This image has four equally sized areas. Red and yellow have both 100% saturation and brightness. So, they are both "100% colorful". The brighter cyan area has 100% brightness but only 50% saturation. The darker cyan area has 100% saturation, but only 50% brightness. So both cyan areas are "50% colorful". The average colorfulness is 75%.
Photoshop has HSB/HSL filter which extracts hue, saturation and brightness or luminance and places them to R,G and B channels of the image. NOTE: In legacy versions of Photoshop the filter is an optional download.
We extract with that filter H, S and B of our test image:
The color looks weird, but that's meaningless because R channel has hue, G has saturation and B has brightness. Saturation and brightness are copied to the clipboard one channel at a time and pasted as new layers on the top. See the disabled pasted layers in the layers panel.
WARNING: Turn the color management off (=none) in the Edit > Color settings dialog. Otherwise everything fails because the channels are handled as grayscale images and pasting them to a RGB image changes the brightness substantially if grayscale images are color-managed like they should be for printing.
The next step is to multiply the saturation and the brightness pixel by pixel:
The top layer has blending mode multiply. This image already shows the pixelwise colorfulness. The left half has 100% and the right half has 50% colorfulness.
Merging the layers and applying Filter > Blur > Average calculates the overall colorfulness. Picking the color with the eyedropper gives R=G=B=192 or as well the brightness=75% as expected:
I'm not a programmer but I guess programming this method to a script is an easy piece for programmers.
As said, this ranking method is not perception based. But in Lab color mode one in theory could present a subjectively more truthful colorfulness ranking method. A simple mode change doesn't work because Photoshop hasn't HCL (=the polar version of Lab color system "hue, chroma, luminance")nor channel decomposition filter for it. The job needs a programmer or a workaround must be found. I'll return and insert it if I find one.