Terminal emulators use ANSI escape sequences to --amongst other things like controlling the cursor’s position-- read the desired text and background color when printing output.
$ echo -e "\033[31mred\033[m" # Prints "red" in red.
While most terminal emulators, including Apple’s Terminal.app, support true color now, most utilities use one of the main sixteen colors (black, red, green, yellow, blue, magenta, cyan, white, and a high intensity or bright version of each). For example, git shows diffs with additions in green and deletions in red, and most testing frameworks print green dots and red "F"s for failures.
Black (0) |
Red (1) |
Green (2) |
Yellow (3) |
Blue (4) |
Purple (5) |
Cyan (6) |
White (7) |
Bright black (8) |
Bright red (9) |
Bright green (10) |
Bright yellow (11) |
Bright blue (12) |
Bright purple (13) |
Bright cyan (14) |
Bright white (15) |
By using relative values, it’s up to the terminal emulator to decide which color it prints as "red". Most even make them configurable through themes. This separates the intent (print this error in red) from the styling (#c23621 is a nice shade of red).
Beyond the standard and high intensity colors, we arrive in absolute color territory. The color codes from 16 to 255 are used to print 8-bit colors, from which 232 to 255 are grayscales from black to white. Then, there’s 32-bit color, also named true color, which has 16.777.216 different color values.
$ echo -e "\033[38;5;205mhotpink\033[m" # prints "hotpink" in hotpink (#205) $ echo -e "\033[38;2;243;134;48mgiant goldfish\033[0m\n" # prints "giant goldfish" in #F38630