[英]Custom RGB colors with Python curses
我正在使用標准庫中的curses
模塊在 Python 中編寫程序。
我希望我的程序在不能使用我用 RGB 三元組指定的自定義顏色時退出。
所以我有一些看起來像的啟動代碼:
import curses
def main(stdscr):
if not curses.can_change_color():
raise Exception('Cannot change color')
curses.init_color(curses.COLOR_BLACK, 999, 0, 0)
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
stdscr.addstr('hello', curses.color_pair(1))
stdscr.addstr(' world', curses.color_pair(2))
stdscr.getch()
curses.wrapper(main)
我得到的結果是:
我預計黑色會被紅色取代。
我誤解了文檔嗎? 我怎樣才能得到curses
來尊重我想使用的自定義 RGB 顏色? 或者至少失敗並告訴我終端不支持它?
此處的 curses 文檔似乎表明,在失敗時它將返回一個錯誤,並且 CPython 源似乎非常忠實地傳播了 curses 錯誤。
如果相關,我在 OS X 10.11 上,我正在用 Homebrew 安裝的 Python3 上進行測試。 但是我用 OS X 的內置 Python 解釋器也能得到同樣的效果。
編輯:
稍微修改示例代碼以顯示顏色內容:
import curses
def main(stdscr):
if not curses.can_change_color():
raise Exception('Cannot change color')
stdscr.addstr(1, 0, repr(curses.color_content(curses.COLOR_BLACK)))
curses.init_color(curses.COLOR_BLACK, 999, 0, 0)
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
stdscr.addstr(0, 0, 'hello', curses.color_pair(1))
stdscr.addstr(' world', curses.color_pair(2))
stdscr.addstr(2, 0, repr(curses.color_content(curses.COLOR_BLACK)))
stdscr.getch()
curses.wrapper(main)
截圖可能是Terminal.app; 在快速檢查中,它不遵守用於更改顏色的轉義序列。 另一方面,iTerm2 確實使用了這些轉義序列。
如果您使用 Terminal.app,將TERM
設置為 xterm-256color 毫無意義,因為與 xterm 存在大量差異。 終端數據庫中有作為評論的討論,您可能會覺得有趣。
initc
用來決定終端是否可以改變顏色的相關功能是ccc
和initc
。 這些功能未在nsterm
終端描述中定義。 終端描述使用xterm+256setaf
構建塊而不是xterm+256color
, infocmp
顯示的是不同的:
comparing xterm+256setaf to xterm+256color. comparing booleans. ccc: F:T. comparing numbers. comparing strings. initc: NULL, '\E]4;%p1%d;rgb\:%p2%{255}%*%{1000}%/%2.2X/%p3%{255}%*%{1000}%/%2.2X/%p4%{255}%*%{1000}%/%2.2X\E\\'. oc: NULL, '\E]104\007'. op: '\E[39;49m', NULL.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.