Programmer2120
Programmer2120

Reputation: 27

How to change the color of text to ANY color in C++

So I need to change the color of text to, pink as the foreground and black as the background. I've searched google but I can't find any thing that says how to change the text color to pink (or any other colors aside from blue/red/green etc.). I know about

system("Color *back**fore*")

But that doesn't have pink. So can anyone help me? Thanks

Upvotes: 0

Views: 1409

Answers (1)

Jim
Jim

Reputation: 473

I can only answer for linux based terminals.

#include <iostream>

using std::cout;
using std::endl;

int main(){

  char pinkish[] = { 0x1b, '[', '3', '8',';','5',';','2','1','2','m',0 };
  char normal[] = { 0x1b, '[', '0', ';', '3', '9', 'm', 0 };

  cout << pinkish << "Hello" << normal << endl;

}

Using the ANSI Color Codes, scroll down to the 256 bit color table.

{ 0x1b, '[', '3', '8',';','5',';','2','1','2','m',0 };

FOREGROUND COLOR: ESC[38;5;

BACKGROUND COLOR: ESC[48;5;

256 bit color value: 212

terminal character (I'm guessing): m

Hope this helps, it was interesting to research it. There is lot's of good info out there - way lots, but hopefully I summarized down to just what you asked for.

A couple of references:

Using-ANSI-Color-Codes-to-Colorize-Your-Bash-Prompt-on-Linux

ansi-color-specific-rgb-sequence-bash

color for text

Upvotes: 1

Related Questions