1

更新

color_content经过一些进一步的实验,ncurses函数似乎没有报告正确的颜色分量值。我编写了以下程序来测试它:

#include <stdlib.h>
#include <string.h>

#include <ncurses.h>

int main(int argc, char *argv[])
{
    initscr();
    start_color();
    noecho();

    char *buffer = calloc(COLS + 1, sizeof(*buffer));
    int  offset = 0;
    bool running = true;

    while (running) {
        // Print current offset at top of output
        snprintf(buffer, COLS, "Offset: %i", offset);
        mvchgat(0, 0, -1, A_NORMAL, 0, 0);
        mvaddnstr(0, 0, buffer, COLS);

        // Print each colour with intensities
        for (int y = 1; y < LINES; ++y) {
            short colour = y + offset;
            short r, g, b;

            color_content(colour, &r, &g, &b);

            // Clear line
            memset(buffer, ' ', COLS);
            mvaddnstr(y, 0, buffer, COLS);

            // Render a string with the current colour's intensities
            snprintf(buffer, COLS, "%d\tR: %d\tG: %d\tB: %d", colour, r, g, b);
            mvaddnstr(y, 0, buffer, COLS);

            // Set background of line to the current colour
            init_pair(y, COLOR_WHITE, colour);
            mvchgat(y, 0, -1, A_NORMAL, y, 0);
        }

        // Scroll down and up with the 'j' and 'k' keys and quit with the 'q' key
        int input = getch();

        if (input == 'j' && offset < COLORS - 1)
            ++offset;
        else if (input == 'k' && offset > 0)
            --offset;
        else if (input == 'q')
            running = false;
    }

    endwin();

    return 0;
}

我从上述程序得到的输出清楚地表明没有获得正确的强度值:

演示 ncurses color_content 函数输出不正确强度值的程序输出

为什么会这样?ncurses 是否有可能无法在程序开始时检索颜色内容,而只能检索使用 设置的颜色init_color

原始问题 - 关闭 ncurses 时如何恢复终端颜色

我正在开发一个init_color用于设置自定义颜色的 ncurses 程序。为了在程序结束时恢复终端颜色,我习惯于color_content在程序开始时存储颜色。但是,使用从中检索的值color_content作为 for 的参数init_color似乎并没有设置一些不正确的颜色。

为了测试这一点,我编写了一个小测试程序,它简单地用于color_content获取颜色,然后立即用于init_color设置颜色:

#include <ncurses.h>

int main(int argc, char *argv[])
{
    initscr();
    start_color();

    short r, g, b;

    for (int i = 0; i < COLORS; ++i) {
        color_content(i, &r, &g, &b);
        init_color(i, r, g, b);
    }

    endwin();

    return 0;
}

而且这个程序不能正确恢复颜色。

前:

程序执行前的终端颜色

后:

程序执行后的终端颜色

有没有办法使用 ncurses 正确地做到这一点?

4

0 回答 0