Skip to content

Background Colors in 256 color mode #47

Open

Description

@cmang

Background colors only work in 16 color mode, and do not work in 256 color mode. This is because Python curses is unable to set color pairs past 256 pairs. This is enough pairs for all of the 16 FG and 16 BG color combinations, but not 256 FG and 256 BG color combinations.

Python has a method, curses.has_extended_color_support(), which is supposed to return True if more than 256 color pairs are supported. In this case, using Python 3.10, 3.11, and (I think) 3.12, the method returns True in macOS and the Linux systems I've tried. Even if the Python builds are compiled against Ncurses 5... which is a problem, because extended color pairs only work with the Ncurses 6 ABI.

So, when you try to set or use color pairs past 256, they do not work. No errors are returned. But they seem to wrap back around to write or read from color pair 1. For example, trying to read color pair 257 instead reads pair 1, pair 258 instead reads pair 2, etc. Writing pair 257 either overwrites pair 1, or seems to disappear into /dev/null. I think this is a bug in Python's extended color support.

I have seen this behavior in macOS and Linux systems. Python bug report #91132 seems to hint at the cause, which is that Python for macOS is compiled against Ncurses 5.9. This implies that it's using the Ncuress 5 ABI, which means that curses.has_extended_color_support() should be False, even though it isn't. (Python bug?)

I can think of 3 possible ways to fix this:

1: Only generate the color pairs that are needed, and hope the user never needs more than 256 pairs. This is not great, though, because Durdraw displays the entire 256 color palette on the screen at once. This means that all 256 color pairs are already in use. As an artist, I like seeing the entire palette at once and don't want to trade away that feature. So I am not crazy about this idea. (It might work nicely for RGB colors, though.)

2: Use a Python build that actually works with extended color pairs. I have not been able to get my hands on one yet, but this is the direction I have been leaning. We can wait for OS vendors and Python core developers to dsitribute Python binaries compiled correctly against the Ncurses 6 ABI, and fix the bugs with extended color pair reporting. This would be nice, but we don't seem to be there yet.

3: Steer away from using Ncurses all together for generating color escape codes. They might be able to be injected with puts() or something, but this breaks ncurses convention and may break across different ncurses versions. Or, ncurses might be replaced with another library completely. This would be a lot of work with refactoring and rewriting Durdraw code. It might also break some portability. I don't like this option, either. :)

Activity

  1. changed the title [-]Background Colos in 256 color mode[/-] [+]Background Colors in 256 color mode[/+] on Feb 8, 2024
  2. cmang commented on May 20, 2024

    @cmang
    MemberAuthor

    After more research, I'm pretty sure that extended color pairs in Python are currently just broken.

    I have filed a bug report with cpython about the extended color pairs not displaying correctly. If that can get fixed, then we should be able to get extended background colors working in Durdraw.

  3. added
    blocked by upstream
    on Jun 9, 2024
  4. charlescochran commented on Apr 3, 2026

    @charlescochran

    Any update on this?

  5. cmang commented on Jul 13, 2026

    @cmang
    MemberAuthor

    Any update on this?

    Yes. The bug in Python Ncurses that was blocking this feature has been fixed in Python 3.16.

    I think we have a few potential ways forward.

    1: Use the extended color pairs to allow 256 background colors in Durdraw, for Python 3.16 and higher. This is probably the fastest way to get there, and might appear in Durdraw 0.30.

    2: Abandon ncurses in favor of a "modern" TUI toolkit, eg: Textual or Notcurses. This might increase portability to Windows (I'm not sure), but has the potential to decrease portability elsewhere. I am not in favor of increasing dependencies here, so I'm not as into this idea.

    3: Abandon ncurses in favor of a custom rendering engine. This would give us full control over all the escaped codes, and opens up possibilities like RGB colors, Sixel previews, no color pair limits, and increased portability to Windows, Micropython, more colors in older Python versions, etc. This is more work (especially on the input side), might happen long term. Probably not in 0.30.

  6. removed
    blocked by upstream
    on Jul 19, 2026
  7. God-damnit-all commented on Jul 22, 2026

    @God-damnit-all

    Any update on this?

    Yes. The bug in Python Ncurses that was blocking this feature has been fixed in Python 3.16.

    I think we have a few potential ways forward.

    1: Use the extended color pairs to allow 256 background colors in Durdraw, for Python 3.16 and higher. This is probably the fastest way to get there, and might appear in Durdraw 0.30.

    2: Abandon ncurses in favor of a "modern" TUI toolkit, eg: Textual or Notcurses. This might increase portability to Windows (I'm not sure), but has the potential to decrease portability elsewhere. I am not in favor of increasing dependencies here, so I'm not as into this idea.

    3: Abandon ncurses in favor of a custom rendering engine. This would give us full control over all the escaped codes, and opens up possibilities like RGB colors, Sixel previews, no color pair limits, and increased portability to Windows, Micropython, more colors in older Python versions, etc. This is more work (especially on the input side), might happen long term. Probably not in 0.30.

    What about crossterm? https://github.com/crossterm-rs/crossterm

    Cross-platform compatibility is a core feature and it has few dependencies. It's often used along with things like viuer for displaying images, which will automatically use whatever image protocol that the terminal has access to.

  8. cmang commented on Jul 24, 2026

    @cmang
    MemberAuthor

    What about crossterm? https://github.com/crossterm-rs/crossterm

    Cross-platform compatibility is a core feature and it has few dependencies. It's often used along with things like viuer for displaying images, which will automatically use whatever image protocol that the terminal has access to.

    It looks like an interesting library (tons of users), and displaying images is definitely something that can be useful to Durdraw (evidenced by the Sixel thumbnail feature in commit b107398).

    But it has 2 issues:

    1: It appears to be written in Rust, for Rust, and does not have hooks for Python. Durdraw is written in Python, so it doesn't really make sense unless I want to also maintain Python -> Rust wrappers.

    2: More generally, Durdraw does not want to rely on hard dependencies for core functionality like the UI. Dependencies for extra drop-in features are fine. Durdraw should "just run" with the standard Python library. This is described a bit more in item 2 of the contributing guidelines. It would be nice to stick to that philosophy for 256 background colors., if possible.

  9. God-damnit-all commented on Jul 24, 2026

    @God-damnit-all

    My bad, for some reason I misremembered what language durdraw is written in. I was thinking of icy_draw, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions