📜  c++ print colourful - C++ (1)

📅  最后修改于: 2023-12-03 15:13:54.718000             🧑  作者: Mango

C++ Print Colorful

Introduction

In this guide, we will explore how to print colorful output using C++. Printing in colors can enhance the readability and visual appeal of your program's output, making it easier to understand and more visually appealing.

Table of Contents
  1. Prerequisites
  2. Printing in Basic Colors
  3. Customizing Color Output
  4. Advanced Color Features
  5. Conclusion

Prerequisites

Before we start, ensure that you have a basic understanding of programming concepts and have C++ compiler installed on your system.

Printing in Basic Colors

C++ provides a way to print output in basic colors using ANSI escape sequences. These sequences are special character sequences that instruct the terminal to apply different display attributes, such as color, formatting, etc.

Here's an example of printing text in different basic colors:

#include <iostream>

#define RESET_COLOR "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"

int main() {
    std::cout << RED << "This text is red!" << RESET_COLOR << std::endl;
    std::cout << GREEN << "This text is green!" << RESET_COLOR << std::endl;
    std::cout << YELLOW << "This text is yellow!" << RESET_COLOR << std::endl;
    std::cout << BLUE << "This text is blue!" << RESET_COLOR << std::endl;
    std::cout << MAGENTA << "This text is magenta!" << RESET_COLOR << std::endl;
    std::cout << CYAN << "This text is cyan!" << RESET_COLOR << std::endl;
    std::cout << WHITE << "This text is white!" << RESET_COLOR << std::endl;

    return 0;
}

In the above example, we define some macros to represent different color escape sequences. The RESET_COLOR macro is used to reset the color to the default. Each color is applied to the output using these escape sequences.

Customizing Color Output

Apart from the basic colors, you can customize the foreground and background colors to create visually appealing output. You can use the RGB color model to specify the desired colors.

Here's an example of printing text with custom colors:

#include <iostream>

#define RESET_COLOR "\033[0m"
#define SET_COLOR(foreground, background) "\033[38;2;" \
    << foreground.r << ";" << foreground.g << ";" << foreground.b << \
    "m\033[48;2;" << background.r << ";" << background.g << \
    ";" << background.b << "m"

struct Color {
    int r, g, b;
};

int main() {
    Color foreground = {255, 0, 0};  // Red
    Color background = {0, 0, 255};  // Blue
    
    std::cout << SET_COLOR(foreground, background) << "Hello, World!" << RESET_COLOR << std::endl;

    return 0;
}

In the above example, we define a Color struct to represent RGB color values. The SET_COLOR macro takes in the foreground and background colors and sets the text color accordingly using the provided RGB values.

Advanced Color Features

C++ provides some libraries that offer advanced color features for printing. One such library is ncurses, which allows you to create interactive text-based UI applications with support for colors, cursor movement, etc.

To use ncurses, you need to install the library and include the appropriate header files in your C++ program. Here's a basic example:

#include <ncurses.h>

int main() {
    initscr();  // Initialize screen
    start_color();  // Enable color support
    
    // Define color pairs
    init_pair(1, COLOR_RED, COLOR_BLACK);
    init_pair(2, COLOR_GREEN, COLOR_BLACK);
    
    attron(COLOR_PAIR(1));  // Activate color pair 1
    printw("This text is red!");
    attroff(COLOR_PAIR(1));  // Deactivate color pair 1
    
    attron(COLOR_PAIR(2));  // Activate color pair 2
    printw("This text is green!");
    attroff(COLOR_PAIR(2));  // Deactivate color pair 2
    
    refresh();  // Refresh screen
    getch();  // Wait for user input
    
    endwin();  // End window

    return 0;
}

In this example, we initialize the screen, enable color support, define color pairs using init_pair, activate color pairs using attron, print colored text using printw, and deactivate the color pairs using attroff. Finally, we refresh the screen, wait for user input, and end the window properly.

Conclusion

Printing colorful output in C++ can greatly enhance the visual appeal and readability of your program. We covered the basics of printing in basic colors using ANSI escape sequences and also explored advanced color features provided by libraries like ncurses. Feel free to experiment with different colors and explore more possibilities to make your program's output visually pleasing.