Pages

How To add colors for texts in C++

Ncurses In C++ (Lesson - 2)


Hai There reader, today i am going to tell you how to add colors to C++ texts using Ncurses. Before you read this, if you don't have any basic knowledge about Ncurses, Please see my 1st Lesson about Ncurses

So, first of all we need to add start_color() command after the initscr() to start color codings. also,

init_pair(1.COLOR_YELLOW, COLOR_BLUE) - use this to define the colors

attrset(COLOR_PAIR(1)) - use this to assign colors to texts


Sample Programme


#include<ncurses.h>
int main()
{
    initscr();
    start_color();
    
    init_pair(1.COLOR_YELLOW, COLOR_BLUE); //define colors
    init_pair(2.COLOR_GREEN, COLOR_WHITE);//define colors


    attrset(COLOR_PAIR(2)); //assign colors to texts

    move(4,20);
    printw("This is colored text");


    getch();
    endwin();


return 0;

}
    

Here is the color pallet used in Ncurses

Color Codes
COLOR_BLACK           0
COLOR_RED                1
COLOR_GREEN          2
COLOR_YELLOW       3
COLOR_BLUE             4
COLOR_MAGENTA   5
COLOR_CYAN            6
COLOR_WHITE         7