ncurses 65536颜色对

发布于 2025-01-21 22:34:16 字数 2143 浏览 3 评论 0 原文

我已经用 - enable-ext-colors and - enable-widec flags编译了Ncurses 6.3。

然后,我遍及所有背景颜色,对于每种前景颜色:

void initColorPairs()
{
    for (int foregroundId = 0; foregroundId < 255; foregroundId++)
    {
        for (int backgroundId = 0; backgroundId < 255; backgroundId++)
        {
            init_extended_pair(
                getColorPairId(foregroundId, backgroundId),
                foregroundId,
                backgroundId);
        }
    }
};

将Macro Color_pairs设置为65536。

...
std::cout << "initialized color pairs: " << COLOR_PAIRS << std::endl; //displays `initialized color pairs: 65536`
...

这可以很好地工作。

我对此功能定义的每个颜色对都有一个唯一的ID:

int getColorPairId(int foregroundId, int backgroundId)
{
    return foregroundId + (backgroundId << 8);
};

这一切似乎都可以很好地工作。 但是,当试图渲染每种可能的颜色对并彼此之间打印它们时:

// visualize
std::string testText = "a";
for (int foregroundId = 0; foregroundId < 255; foregroundId++)
{
    for (int backgroundId = 0; backgroundId < 255; backgroundId++)
    {
        attr_on(COLOR_PAIR(getColorPairId(foregroundId, backgroundId)), NULL);
        mvprintw(foregroundId, backgroundId, testText.c_str());
    }
}

而不是将每个前景颜色的屏幕上的每个背景颜色都放在每个背景颜色上,我最终得到了:

它应该用每个字符改变背景颜色,但似乎只会改变前景。

奇怪的是,如果Colorid结构被逆转(前景在8位而不是背景上移动),则 background 迭代而不是前景!

int getColorPairId(int foregroundId, int backgroundId)
{
    return backgroundId + (foregroundId << 8);
};

I have compiled NCurses 6.3 with the --enable-ext-colors and --enable-widec flags.

I then iterate across all background colors, for each foreground color:

void initColorPairs()
{
    for (int foregroundId = 0; foregroundId < 255; foregroundId++)
    {
        for (int backgroundId = 0; backgroundId < 255; backgroundId++)
        {
            init_extended_pair(
                getColorPairId(foregroundId, backgroundId),
                foregroundId,
                backgroundId);
        }
    }
};

This sets the macro COLOR_PAIRS to 65536.

...
std::cout << "initialized color pairs: " << COLOR_PAIRS << std::endl; //displays `initialized color pairs: 65536`
...

This is working perfectly.

I have a unique id for every color pair defined by this function:

int getColorPairId(int foregroundId, int backgroundId)
{
    return foregroundId + (backgroundId << 8);
};

This all appears to work perfectly.
However, when trying to render each of these possible color pairs and printing them next to each other:

// visualize
std::string testText = "a";
for (int foregroundId = 0; foregroundId < 255; foregroundId++)
{
    for (int backgroundId = 0; backgroundId < 255; backgroundId++)
    {
        attr_on(COLOR_PAIR(getColorPairId(foregroundId, backgroundId)), NULL);
        mvprintw(foregroundId, backgroundId, testText.c_str());
    }
}

Instead of ending up with a screen full of each foreground color against each background color I end up with this:
block of text of the letter "A", with only the foreground color changing across characters

It should be changing the background color with each character, but it only seems to change the foreground.

Strangely, if the colorId construction is reversed (foreground shifted over 8 bits instead of background), the background iterates instead of the foreground!

int getColorPairId(int foregroundId, int backgroundId)
{
    return backgroundId + (foregroundId << 8);
};

enter image description here

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

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

发布评论

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

评论(1

痕至 2025-01-28 22:34:16

不使用库接口中的扩展颜色,颜色对仅限于32767(签名16位编号)。

attr_on(COLOR_PAIR(getColorPairId(foregroundId, backgroundId)), NULL);

Extensions 手册页的部分

  • 对于修改颜色的函数,例如 wattr_set ,如果Opts为
    将其视为指向INT的指针,并用于设置颜色
    对而不是短对参数。

但是在扩展答案时,我看到这些调用被忽略了(可能是因为颜色对不是这些呼叫的明确参数,而是函数正文内的派生值):

其余功能选择但不操纵颜色,例如, wattr_on wattr_off 是除了检查它们是null外,但不使用此实现。

The ncurses-examples (demo and test-programs) include a few using attr_on (see

  • 来源code>setcchar, using the macro
    #define set_extended_pair(opts, color_pair) \
        if ((opts) != NULL) { \
            color_pair = *(const int*)(opts); \
        }

在其他功能中使用的(允许使用/没有功能的有条件地编译它们) :

new_pair.h:71:#define set_extended_pair(opts, color_pair) \                     
new_pair.h:78:#define set_extended_pair(opts, color_pair) \                     
base/lib_chgat.c:64:    set_extended_pair(opts, color_pair);                    
base/lib_colorset.c:54:    set_extended_pair(opts, color_pair);                 
base/lib_slkatr_set.c:58:    set_extended_pair(opts, color_pair);               
widechar/lib_cchar.c:63:    set_extended_pair(opts, color_pair);                
widechar/lib_vid_attr.c:101:    set_extended_pair(opts, color_pair);

对于 wattr_on.c

if_EXT_COLORS({
    if (at & A_COLOR)
        win->_color = PairNumber(at);
});

(从属性参数中提取颜色对)看起来像

if_EXT_COLORS({
    if (at & A_COLOR) {
        win->_color = PairNumber(at);
        set_extended_pair(opts, win->_color);
    }
});

wattr_set 可以正确处理:(

#define wattr_set(win,a,p,opts) \                                               
        (NCURSES_OK_ADDR(win) \                                                 
         ? ((void)((win)->_attrs = ((a) & ~A_COLOR), \                          
                   (win)->_color = (opts) ? *(int *)(opts) : (p)), \            
            OK) \                                                               
         : ERR)

wattr_off 不需要更改, 当然)。 X/Open Curses假设 wattr_on 的属性参数 不包括颜色对的位。 ncurses确实这样做(因为它在狭窄/宽库中使用相同的布局来用于属性),因此为 opts 参数提供了使用 - ncurses是一致的。

Without using the extended colors in the library interface, color pairs are limited to 32767 (signed 16-bit numbers).

That NULL in

attr_on(COLOR_PAIR(getColorPairId(foregroundId, backgroundId)), NULL);

should be used to pass the value of an integer (more than 16-bits) for the color pair, as mentioned in the Extensions section of the manual page, e.g.,

  • For functions which modify the color, e.g., wattr_set, if opts is
    set it is treated as a pointer to int, and used to set the color
    pair instead of the short pair parameter.

But in expanding the answer, I see that these calls are overlooked (probably because the color-pair is not an explicit parameter to these, but is a derived value within the body of the function):

The remaining functions which have opts, but do not manipulate color, e.g., wattr_on and wattr_off are not used by this implementation except to check that they are NULL.

The ncurses-examples (demo and test-programs) include a few using attr_on (see README), but those calls aren't using this particular extension (dots_xcurses would be a good place to do that – optionally of course).

picsmap uses the feature via setcchar:

    #define set_extended_pair(opts, color_pair) \
        if ((opts) != NULL) { \
            color_pair = *(const int*)(opts); \
        }

That macro is used in other functions (to allow those to be conditionally compiled with/without the feature):

new_pair.h:71:#define set_extended_pair(opts, color_pair) \                     
new_pair.h:78:#define set_extended_pair(opts, color_pair) \                     
base/lib_chgat.c:64:    set_extended_pair(opts, color_pair);                    
base/lib_colorset.c:54:    set_extended_pair(opts, color_pair);                 
base/lib_slkatr_set.c:58:    set_extended_pair(opts, color_pair);               
widechar/lib_cchar.c:63:    set_extended_pair(opts, color_pair);                
widechar/lib_vid_attr.c:101:    set_extended_pair(opts, color_pair);

For wattr_on.c, the chunk

if_EXT_COLORS({
    if (at & A_COLOR)
        win->_color = PairNumber(at);
});

(which extracts a color pair from the attribute parameter) would look like

if_EXT_COLORS({
    if (at & A_COLOR) {
        win->_color = PairNumber(at);
        set_extended_pair(opts, win->_color);
    }
});

wattr_set is handled properly:

#define wattr_set(win,a,p,opts) \                                               
        (NCURSES_OK_ADDR(win) \                                                 
         ? ((void)((win)->_attrs = ((a) & ~A_COLOR), \                          
                   (win)->_color = (opts) ? *(int *)(opts) : (p)), \            
            OK) \                                                               
         : ERR)

(No change would be needed for wattr_off, of course). X/Open Curses assumes that the attribute parameter at of wattr_on does not include bits for a color pair. ncurses does do this (because it uses the same layout for the attributes in narrow/wide libraries), so providing a use for the opts parameter is consistent – for ncurses.

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