0

One way to determine if a terminal is predominately dark or light is to get the value if its background color, and compare that with the grey color.

However in order to do this you need to know what the numeric RGB value is for white (to take half of that).

For some terminals it is 0xff + 0xff + 0xff, while on others it is 0xffff + 0xffff + 0xffff.

Thoughts on how to determine which of these two possibilities (and others if that exists) inside a shell script?

Note: this is similar to common environment variable to set dark or light terminal background but this question is more narrow.

0

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:                          

       black                rgb:0/0/0                                       
       red                  rgb:ffff/0/0                                    
       green                rgb:0/ffff/0                                    
       blue                 rgb:0/0/ffff                                    
       yellow               rgb:ffff/ffff/0                                 
       magenta              rgb:ffff/0/ffff                                 
       cyan                 rgb:0/ffff/ffff                                 
       white                rgb:ffff/ffff/ffff

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: xterm with query-colors.pl

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.