Presumably you are using something like the query described in XTerm Control Sequences:
OSC Ps ; Pt BEL
OSC Ps ; Pt ST
Ps = 4 ; c ; spec -> Change Color Number c to the color
specified by spec. This can be a name or RGB specification as
per XParseColor. Any number of c/spec pairs may be given.
The color numbers correspond to the ANSI colors 0-7, their
bright versions 8-15, and if supported, the remainder of the
88-color or 256-color table.
If a "?" is given rather than a name or RGB specification,
xterm replies with a control sequence of the same form which
can be used to set the corresponding color. Because more than
one pair of color number and specification can be given in one
control sequence, xterm can make more than one reply.
xterm uses four digits (e.g.,
ffff) because that is the preferred form in the X manpage, which says:
The eight primary colors can be represented as:
If I had to make a script which detects the number of digits returned by some random terminal which responds to that control sequence, I would look at the red, green and blue components of colors 1, 2 and 4, because even with some distortion, those will still use the same number of digits as "white".
Here is a screenshot with xterm showing the results from a script which does that:
I checked a few other terminal emulators; those that did respond used the RGB components where the largest corresponded to ANSI (though two of the three that did respond, did not actually report ANSI colors -ymmv).