Search code examples
c++colorsvt100

Can you use hex color values with vt100 escape codes c++


I just learned how to use vt100 escape code do change background and text colors in the terminal (\033[30mand\033[40m). I was wondering if there was a way to use hex color codes instead of being limited to the 8 colors that you get for using 30 - 37 or 40 - 47. Something like:\033[#48FF1Fm. I wouldn't be surprised if this was impossible but I thought it was worth asking.


Solution

  • VT-100 is an old terminal and I was surprised that it had escape codes for colors!

    See also this stack overflow List of ANSI color escape sequences which has some good answers.

    The following section from ANSI/VT100 Terminal Control Escape Sequences provides an example.

    Set Attribute Mode  <ESC>[{attr1};...;{attrn}m
    Sets multiple display attribute settings. The following lists standard attributes:
    0   Reset all attributes
    1   Bright
    2   Dim
    4   Underscore  
    5   Blink
    7   Reverse
    8   Hidden
    
        Foreground Colours
    30  Black
    31  Red
    32  Green
    33  Yellow
    34  Blue
    35  Magenta
    36  Cyan
    37  White
    
        Background Colours
    40  Black
    41  Red
    42  Green
    43  Yellow
    44  Blue
    45  Magenta
    46  Cyan
    47  White
    

    However it looks like not only the standard colors you have found, depending on the device support there are also what looks to be a color palette mechanism.

    However see this article Bash tips: Colors and formatting (ANSI/VT100 Control Sequences for a much more intensive list with additional links. This web page also has a terminal compatibility chart showing some of the escape code processing differences between several different VT-100 terminal emulators.

    For 256 foreground colors the escape sequence is ”<Esc>[38;5;ColorNumberm” where the color number, ColorNumber, is from the provided table. Looks like the 'm' is a required character after the color number.

    A bash code example from the page is echo -e "\e[38;5;82mHello \e[38;5;198mWorld" which will print "Hello" in a green color and "World" in a purple color.

    For 256 background colors the escape sequence is ”<Esc>[48;5;ColorNumberm”.

    The web page also describes how to combine multiple attributes to achieve effects such as both foreground and background colors by separating the attributes with a semicolon. A bash code example provided is echo -e "\e[1;31;42m Yes it is awful \e[0m" which displays red text on a green background with the text bolded.