256 种颜色,前景和背景

发布于 2024-07-12 03:20:07 字数 487 浏览 5 评论 0原文

这是两个脚本的故事,与上一个问题相关。

这两个脚本位于 http://gist.github.com/50692。 ansi.rb 脚本在所有 256 种背景颜色上显示所有 256 种颜色。 ncurses.rb 脚本显示所有 256 种前景色,但背景显示基本的 16 种颜色,然后似乎在闪烁和反向视频等各种属性之间循环。

那么什么给出呢? 这是 ncurses 中的错误,它使用有符号整数作为颜色对吗? (即“tput颜色”说256,但“tput对”说32767而不是65536)看起来如果是这种情况,颜色对的前半部分将正确显示,但后半部分将重复或进入属性作为int包裹。

This is a tale of two scripts and is related to a previous question.

The two scripts are at http://gist.github.com/50692. The ansi.rb script displays all 256 colors on all 256 background colors. The ncurses.rb script displays all 256 foreground colors but the background displays the basic 16 and then seems to cycle through various attributes like blinking and reverse video.

So what gives? Is this the bug in ncurses that it uses a signed integer for color pairs? (ie 'tput colors' says 256 but 'tput pairs' says 32767 instead 65536) It seems like if that were the case the first half of the colors pairs would display properly but the second half would repeat or get into the attributes as the int wraps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见终念 2024-07-19 03:20:07

原因是因为 ABI 6 之前的 ncurses(包括大多数发行版(包括 Debian 和 Amazon AMI)使用的当前版本)COLOR_PAIR(n) 无法引用任何高于 256 的定义对。这是因为 COLOR_PAIR(n) 的参数code>COLOR_PAIR(n) 的类型为 chtype 。 较低的 8 位选择颜色对,其余的则被视为特定终端属性的“或”位标志。 这就是为什么当您尝试超出示例代码中的对号 255 时,您会看到闪烁、反转、下划线等。

这是非常不幸的,我计划在程序中使用队列来使用 init_pair() 动态定义颜色,并作为妥协覆盖最近最少使用的对。 我写信给 ncurses 维护者 Thomas Dickey,询问是否有办法暂时跳出 ncurses 编写原始终端代码,然后返回 ncurses。 这是他的回复:

本质上,你必须忘记屏幕优化,而只是做
您自己的绘图使用 terminfo(3) 中列出的函数,例如,
tigetstr、mvcur、tputs。

您可以暂时退出 ncurses,但随后必须重新绘制
屏幕。 否则 ncurses 会对屏幕上的内容感到困惑
以及光标所在的位置。

这个解决方案似乎存在太多陷阱,但如果您在 ncurses 程序中绝对需要超过 256 个同时对(不包括您可以使用反向属性伪造的对),那么这就是您必须做的。

The reason is because ncurses pre ABI 6, which includes the current version that most distributions including Debian and Amazon AMI use, COLOR_PAIR(n) cannot reference any defined pairs above 256. This is because the argument to COLOR_PAIR(n) is of type chtype . The lower 8 bits choose the color pair, and the rest are treated as bitflags ORed in for specific terminal attributes. This is why you see blink, inverse, underline, etc. when you attempt to go beyond pair number 255 in your sample code.

This is pretty unfortunate, and I plan to use a queue in my program to define colors on the fly using init_pair() and just overwrite the least recently used pairs as a compromise. I wrote the ncurses maintainer Thomas Dickey asking if there is a way to step outside of ncurses temporarily to write raw terminal codes and then return to ncurses. This was his reply:

Essentially you'd have to forget about screen-optimization and just do
your own drawing using the functions listed in terminfo(3), e.g.,
tigetstr, mvcur, tputs.

You can exit temporarily from ncurses, but then have to repaint the
screen. Otherwise ncurses will be confused about what is on the screen
and where the cursor is.

This solution seems to present too many pitfalls, but if you absolutely need more than 256 simultaneous pairs in an ncurses program (not counting pairs you could fake with the inverse attribute), then this is what you'll have to do.

寂寞陪衬 2024-07-19 03:20:07

我根本不了解 Ruby,因此无法提供工作示例,但 ncurses.rb 脚本应该告诉您一些信息,因为它比 ansi.rb 脚本短得多。

您没有设置颜色,因此它只是循环默认的 16 种调色板,其中包含闪烁下划线、粗体等属性提供的变体。

您需要使用 init init_color(short color,short r,short g,short b) 使用 RGB 值 (0 - 1000) 初始化颜色索引,然后使用 int init_pair(shortpair,shortf,shortb)<设置用于显示的颜色对< /code> 在调用 COLOR_PAIR(n) 之前。

为了可移植性,您应该检查 bool has_colors(void)bool can_change_color(void)

在我的系统上,ncurses 的手册页非常宝贵。

I don't know Ruby at all, so can't provide a working example, but the ncurses.rb script should tell you something in that it's so much shorter than the ansi.rb script.

You're not setting up the colours, so it's just looping the default 16 colour palette with variations provided by the attributes such as blink underline, bold, etc.

You need to use int init_color(short color, short r, short g, short b) to initialize a color index with the RGB values (0 - 1000) and then set the colour pairs to use for display with int init_pair(short pair, short f, short b) before calling COLOR_PAIR(n).

For portability you should check bool has_colors(void) and bool can_change_color(void)

On my system the man pages for ncurses are invaluable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文