I'm trying to print a color gradient. When running, nothing is displayed, but when I select the entire window area with the mouse cursor, the colors appear but not a color gradient.
Function
smooth
takes the color and dimension information and starts the ncurses environment.Colors are defined using the
init_color
function for and combined in pairs usinginit_pair
to create custom color pairs.Pixels are displayed using the
mvaddch
functions to place an empty character (' ') on the screen in the appropriate positions. Each column is divided into three equal parts, and each part is displayed with a different pair of colors.
The code:
#include <ncurses.h>
struct Color {
int red, green, blue;
};
void smooth(int rows, int cols, struct Color leftColor, struct Color rightColor) {
initscr();
start_color();
cbreak();
noecho();
curs_set(0);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int r, g, b;
r = leftColor.red + ((float)j / cols) * (rightColor.red - leftColor.red);
g = leftColor.green + ((float)j / cols) * (rightColor.green - leftColor.green);
b = leftColor.blue + ((float)j / cols) * (rightColor.blue - leftColor.blue);
init_color(COLOR_BLACK, 0, 0, 0);
init_color(COLOR_RED, r * 1000 / 255, 0, 0);
init_color(COLOR_GREEN, 0, g * 1000 / 255, 0);
init_color(COLOR_BLUE, 0, 0, b * 1000 / 255);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
attron(COLOR_PAIR(1));
mvaddch(i, j, ' ');
attroff(COLOR_PAIR(1));
attron(COLOR_PAIR(2));
mvaddch(i, j + cols, ' ');
attroff(COLOR_PAIR(2));
attron(COLOR_PAIR(3));
mvaddch(i, j + 2 * cols, ' ');
attroff(COLOR_PAIR(3));
}
}
refresh();
getch();
endwin();
}
int main() {
int y, x;
struct Color left, right;
printf("Enter the RGB values for the left color: ");
scanf("%d %d %d", &left.red, &left.green, &left.blue);
printf("Enter the RGB values for the right color: ");
scanf("%d %d %d", &right.red, &right.green, &right.blue);
printf("Height: ");
scanf("%d", &y);
printf("Width: ");
scanf("%d", &x);
smooth(y, x, left, right);
return 0;
}