C: Change color of letters (multiple colors on one screen)

Asked

Viewed 12,278 times

4

For example, I want print on the same screen:

printf(".___. .___. .___.\n");

printf("._1_. ._2_. ._3_.\n");

printf("Digite 0 p/ sair ou outro número p/ continuar: ");

but I want the letters of each printf stay with different colors, for example: first blue, then green and then yellow... I tried to use the following code:

system("color A");
printf(".___. .___. .___.\n");
system("color E");  
printf("._1_. ._2_. ._3_.\n");
system("color 7");   
printf("Digite 0 p/ sair ou outro número p/ continuar: ");

But it didn’t work; he took only the last color, or rather, he was fast and only performed visibly the last color... I tried too:

#include <conio.h>
textcolor(blue);
printf(".___. .___. .___.\n");
textcolor(red); 
printf("._1_. ._2_. ._3_.\n");
textcolor(yellow);  
printf("Digite 0 p/ sair ou outro número p/ continuar: ");

But it says that textcolor was not declared.

  • Boy, conio is obsolete too many... How about that answer in the Soen?

  • didn’t work, just printed a lot of strange character, this I think is for Unix (Linux)

  • ops, windows right here.

  • It worked, but it’s kind of complicated kk because I can’t use function, I’ll have to use the system("color XX") same, but thanks to the tip, in other projects I use this

  • Try it if your screen does "ANSI escape sequences": printf("\x1b[32mHello\n");

  • If you’re on Windows, maybe try ansicon work (never tried)

  • These only work on Linux, and ansicon only changes the cursor etc

Show 2 more comments

2 answers

1

An easy way to do this in C is to use the ANSI code of the colors. At first you create a constant to store the ANSI for each color:

#define ANSI_COLOR_RED      "\x1b[31m" //cores em ANSI utilizadas 
#define ANSI_COLOR_GRAY     "\e[0;37m"

And to use, passes these constants in the printf:

printf(ANSI_COLOR_RED "Hello world" ANSI_COLOR_RESET); 

Since the first is the color you want for this text, and at the end you can use the Reset to return to the default.

Here there’s an example where I used this in a game.

0

Since you are looking for a specific C solution for windows, I would recommend using the function SetConsoleTextAttribute() of API Win32. You have to generate one Handler to the terminal and then feed it the correct attributes.

Follow a simple example:

/* Mudar a cor do texto do terminal, e depois de volta ao normal. */
#include <stdio.h>
#include <windows.h>

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
    WORD saved_attributes;

    /* Salvar estado atual */
    GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
    saved_attributes = consoleInfo.wAttributes;

    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
    printf("This is some nice COLORFUL text, isn't it?");

    /* Voltando ao estado original */
    SetConsoleTextAttribute(hConsole, saved_attributes);
    printf("Back to normal");

    return 0;
}

For more information (in English), look here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.