-1

I am aware of the fact that we can print colors in the terminal as mentioned here: stdlib and colored output in C and here: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

Is it possible to output more custom color with the 24-bit RGB code (r,g,b) where r,g and b goes from 0 to 255 (ideally portable but at least for a MacOS 11.6.1)? The following solutions enables 255 colors but still not with rgb coding. What I would like to be able to do is to tune 3 parameters (r, g and b) to have the desired color

// inspired from http://jafrog.com/2013/11/23/colors-in-terminal.html
#include <stdio.h>

int main(void)
{
  int i;
  for (i = 0; i < 256; i++) 
  {
    printf("\e[38;5;%dm %3d\e[m", i, i);
  }
  return 0;
}


ecjb
  • 5,169
  • 12
  • 43
  • 79
  • Possible duplicate: [ANSI Color Specific RGB Sequence Bash](https://stackoverflow.com/questions/15682537/ansi-color-specific-rgb-sequence-bash) – Weather Vane Mar 04 '23 at 10:16
  • Thank you for your comment @WeatherVane. But I think that the question you mention is related to bash but not C. What would be the C translation? – ecjb Mar 04 '23 at 10:24
  • 2
    What you output to the terminal has nothing to do with the language you use to do it. The answers are showing how to use escape codes (as you are doing). – Weather Vane Mar 04 '23 at 10:25
  • 1
    I’m voting to close this question because you can easily write and test if your mac terminal supports any of the multicolour modes (it would take 5 minutes to write). The link in your question contains all information needed. I do not think that anyone will write it for you and you did not ask any specific question related to the code using those 8 or 24 bits ESC codes. Code in the question is far not sufficient to be considered as your effort (and it is not your code anyway) – 0___________ Mar 04 '23 at 11:02
  • Alright @0___________. I wrote some code and made the question more specific but still cannot tune r,g and b – ecjb Mar 04 '23 at 12:22

1 Answers1

0

Here is the code that worked for me:

#include <stdio.h>

#define ANSI_FONT_COL_RESET     "\x1b[0m"
#define FONT_COL_CUSTOM_RED     "\e[38;2;200;0;0m" // where rrr;ggg;bbb in 38;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define FONT_COL_CUSTOM_GREEN   "\e[38;2;0;200;0m" // where rrr;ggg;bbb in 38;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define FONT_COL_CUSTOM_BLUE    "\e[38;2;0;0;200m" // where rrr;ggg;bbb in 38;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define BCKGRD_COL_CUSTOM_RED   "\e[48;2;200;0;0m" // where rrr;ggg;bbb in 48;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define BCKGRD_COL_CUSTOM_GREEN "\e[48;2;0;200;0m" // where rrr;ggg;bbb in 48;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define BCKGRD_COL_CUSTOM_BLUE  "\e[48;2;0;0;200m" // where rrr;ggg;bbb in 48;2;rrr;ggg;bbbm can go from 0 to 255 respectively

int main (int argc, char const *argv[]) {

  printf(FONT_COL_CUSTOM_RED     "This font color is CUSTOM_RED!"           ANSI_FONT_COL_RESET "\n");
  printf(FONT_COL_CUSTOM_GREEN   "This font color is CUSTOM_GREEN!"         ANSI_FONT_COL_RESET "\n");
  printf(FONT_COL_CUSTOM_BLUE    "This font color is CUSTOM_BLUE!"          ANSI_FONT_COL_RESET "\n");
  printf(BCKGRD_COL_CUSTOM_RED   "This background color is CUSTOM_RED!"     ANSI_FONT_COL_RESET "\n");
  printf(BCKGRD_COL_CUSTOM_GREEN "This background color is CUSTOM_GREEN!"   ANSI_FONT_COL_RESET "\n");
  printf(BCKGRD_COL_CUSTOM_BLUE  "This background color is CUSTOM_BLUE!"    ANSI_FONT_COL_RESET "\n");
  printf(FONT_COL_CUSTOM_GREEN BCKGRD_COL_CUSTOM_RED "This font color is CUSTOM_GREEN with background CUSTOM_RED!"    ANSI_FONT_COL_RESET "\n");
  printf(                        "This font color is NORMAL!\n");

  return 0;
}


with following output:

enter image description here

ecjb
  • 5,169
  • 12
  • 43
  • 79