Shell – Is it possible to use named colors in Zsh beyond ANSI names

colorsshellzsh

I know there are ways to use ANSI color names in Zsh (such as red), but Zsh supports 256 colors by number. I'm curious if there's any way to refer to the non-ANSI colors by a name? (Without just sticking them in a variable)

Best Answer

The first 16 colors have been standard for a long time (and have mostly standard hues). 256 colors are a more recent extension defined by xterm and compatible terminals. The xterm documentation has this to say about colors 16–255:

These specify the colors for the 256-color extension. The default resource values are for colors 16 through 231 to make a 6x6x6 color cube, and colors 232 through 255 to make a grayscale ramp.

The colors can be changed from within the terminal; see the ctlseqs file. For example print '\e]4;42;taupe\a' changes color 42 to be taupe (the color names are available in /etc/X11/rgb.txt or some other distribution-dependent location).

If you're content to assume that the colors above 16 have their default values, you could extend the $color array with names from rgb.txt. You'll need to do a bit of arithmetic to find the closest approximation of 8-bit colors in lg(6)-bit colors.

Related Question