【问题标题】:Unexpected curses fore/background 256 color init_pair'ing意外的诅咒前/背景 256 色 init_pair'ing
【发布时间】:2015-06-15 05:21:28
【问题描述】:

在适当的诅咒/窗口初始化后,我可以 color_pair 默认颜色对,例如使用前景、-1 和 -1、背景颜色,但是当我开始使用自定义对 #s 和 bg/fg 颜色值 > 0x8 自定义对时,我得到了意外或错误的结果。

术语的环境 $TERM == 'xterm-256color'

Python 的 curses.COLORS == 256

Python 的 curses.COLOR_PAIRS == 32767

Python 的版本 == 2.7.7,curses == 2.2

#!/usr/bin/env python
"""Dumbed down code to follow:"""

import curses

# init
window = curses.initscr()
curses.start_color()
curses.use_default_colors()

# assign 'default' pairs, pairs are assigned +1 MORE than the color value!
for each in range(curses.COLORS):
    curses.init_pair(each + 1, each, -1)
for each in range(curses.COLORS):
    curses.init_pair(each + 1 + curses.COLORS, -1, each)

# custom/non-default pair
curses.init_pair(1 + 2*curses.COLORS, 0x0f, 0x15)  # white on cobalt according to colors above ???
curses.init_pair(4321, 0xd5, 0x81)  # hot pink on violet according to colors above ???

# setup
curses.meta(1)
curses.noecho()
curses.cbreak()
window.leaveok(1)
window.scrollok(0)
window.keypad(1)
window.refresh()

# print all pairs in their colors
for each in range(1 + 2*curses.COLORS):
    window.addstr(hex(each).join('  '), curses.color_pair(each))  # these are all perfect
window.addstr(hex(1 + 2*curses.COLORS).join('  '), curses.color_pair(1 + 2*curses.COLORS))  # nope: this prints 0,-1: black on default ???
window.addstr(hex(4321).join('  '), curses.color_pair(4321))  # nope: this prints 0xe1,-1: pinkish on default ???

# update
window.noutrefresh()
curses.doupdate()

# pause
window.getch()

# teardown
window.leaveok(0)
window.scrollok(1)
window.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()

检查上面的“???”。我错过了什么概念?我想为每个单色 256 色加上一些自定义 fg/bg 有一个颜色对?

【问题讨论】:

  • 我不是一路走来,但我是一个不耐烦的人......看来我的误解与COLOR_PAIRS最密切相关。我阅读文档的每一个迹象都告诉我,我最多可以定义这么多对。相反,您似乎最多只能定义 curses.COLORS-1 对,所以在我的例子中,0x01 到 0xff。 32767 (0x7ff) 的其他位用于自动应用的属性(突出显示、下划线等)。所以我似乎无法定义我想要的所有颜色(基本上每种颜色 fg、bg 以及一些自定义)......但仅限于 254 种。

标签: python terminal curses python-curses


【解决方案1】:

使用 ncurses5,您只能拥有 256 个颜色对,因为这些值存储在 8 位字段中。那是在 ncurses 常见问题解答Why not make "xterm" equated to "xterm-256color"?

【讨论】:

    猜你喜欢
    • 2013-10-26
    • 2010-10-03
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 2012-04-08
    • 2014-04-23
    相关资源
    最近更新 更多