Python curses question: How to separate foreground and background colors from the value returned by inch()?

pjfarley3 at earthlink.net pjfarley3 at earthlink.net
Sat Apr 3 14:24:57 EDT 2021


> -----Original Message-----
> From: pjfarley3 at earthlink.net <pjfarley3 at earthlink.net>
> Sent: Saturday, April 3, 2021 1:25 AM
> To: python-list at python.org
> Subject: Python curses question: How to separate foreground and background
> colors from the value returned by inch()?
> 
> The window.inch(([y, x])) function returns the character and attributes of
> the character at the current or specified window position.  But how does
one
> separate the foreground and background colors from the resulting value?
> 
> colors = window.inch(0.0) & A_ATTRIBUTES
> 
> That should return the combined color + attributes value, but how to
> separate the foreground and background color values from that result?
> Should the following reliably work?
> 
> fg, bg = curses.pair_content (curses.pair_number(window.inch(0,0) &
> A_ATTRIBUTES))
> 
> Peter

Following up my own question because I experimented and found my own answer.
The snippet below reliably extracts the foreground and background colors on
both Ubuntu 20.04 (WSL2) and in a Win 10 console or Terminal window
(addstr() displays added to show intermediate values):

    y, x = stdscr.getyx()
    attr = stdscr.inch(6, 0)
    stdscr.move(y, x)
    stdscr.addstr("inch(6,0)={:08X}={}\n".format(attr, attr))
    pair = curses.pair_number(attr)
    if platform.system() == "Windows":
        pair = pair >> 16
    stdscr.addstr("pair(6,0)={:08X}={}\n".format(pair, pair))
    fg, bg = curses.pair_content (pair)
    stdscr.addstr("color(6,0) fg={:08X}={},bg={:08X}={}\n".format(fg, fg,
bg, bg))

The same sequence (minus the getyx() and move() operations) also works for
the window.getbkgd() function:

    attr = stdscr.getbkgd()
    stdscr.addstr("scrbkgd={:08X}={}\n".format(attr, attr))
    pair = curses.pair_number(attr)
    if platform.system() == "Windows":
        pair = pair >> 16
    fg, bg = curses.pair_content (pair)
    stdscr.addstr("color(scrbkgd) fg={:08X}={},bg={:08X}={}\n".format(fg,
fg, bg, bg))

Windows snippet results:

inch(6,0)=5A000030=1509949488 
pair(6,0)=0000005A=90 
color(6,0) fg=00000059=89,bg=00000000=0
scrbkgd=00000020=32
color(scrbkgd) fg=00000007=7,bg=00000000=0

Ubuntu results:

inch(6,0)=00005A30=23088
pair(6,0)=0000005A=90
color(6,0) fg=00000059=89,bg=-0000001=-1
scrbkgd=00000000=0
color(scrbkgd) fg=-0000001=-1,bg=-0000001=-1

Test environments were:

1.	Ubuntu 20.04 (WSL2), python 3.8.5 and 3.9.0+, ncurses6/focal,now
6.2-0ubuntu2 amd64
2.	Win10 console and Terminal windows, python 3.8.7, windows-curses
2.2.0

ISTM that maybe I ought to figure out how to file a documentation bug /
enhancement request.  The official documentation of the returned values of
these functions is woefully incomplete, including that ncurses returns -1
values for "default" colors in the *ix environment while the Windows version
returns actual color values for default values.  Obviously an implementation
difference between ncurses and PDCurses, but worth noting somewhere.

I'm not sure if the necessity of shifting the "pair" result for Windows is a
necessary documentation addition or an actual bug in the underlying code.
Without the shift, the code aborts with these errors in Windows:

Traceback (most recent call last):
  File "C:\Users\MyUser\test\curses-color.py", line 126, in <module>
    curses.wrapper(main)
  File "C:\Python38\lib\curses\__init__.py", line 105, in wrapper
    return func(stdscr, *args, **kwds)
  File "C:\Users\MyUser\test\curses-color.py", line 72, in main
    fg, bg = curses.pair_content (pair)
OverflowError: signed short integer is greater than maximum

Guidance on how to file requests for documentation corrections /
clarifications would be appreciated.  I do already have a generic module
that reliably shows the results using both of the above snippets.

Peter
--




More information about the Python-list mailing list