.. zsh-module-meta::
   :name: zsh/nearcolor
   :summary: Map colours to the nearest colour in the available palette.

The ``zsh/nearcolor`` module replaces colours specified as hex triplets
with the nearest colour in the 88 or 256 colour palettes that are widely
used by terminal emulators.  By default, 24-bit true colour escape codes
are generated when colours are specified using hex triplets.  These are
not supported by all terminals.  The purpose of this module is to make
it easier to define colour preferences in a form that can work across a
range of terminal emulators.

Aside from the default colour, the ANSI standard for terminal escape
codes provides for eight colours. The bright attribute brings this to
sixteen. These basic colours are commonly used in terminal applications
due to being widely supported. Expanded 88 and 256 colour palettes are
also common and, while the first sixteen colours vary somewhat between
terminals and configurations, these add a generally consistent and
predictable set of colours.

In order to use the ``zsh/nearcolor`` module, it only needs to be
loaded. Thereafter, whenever a colour is specified using a hex triplet,
it will be compared against each of the available colours and the
closest will be selected. The first sixteen colours are never matched in
this process due to being unpredictable.

The use of a hex triplet will automatically load the ``zsh/nearcolor``
module if the terminal doesn't support 24-bit colour. This behaviour can
be modified via the ``.term.extensions`` parameter -- see
:zshsectref:`Terminal Extensions <zshzle>`.

The module can also be loaded manually. It is recommended to be
selective in doing so. For example, the following checks the
``COLORTERM`` environment variable:

.. code-block:: zsh
   :class: example

    [[ $COLORTERM = *(24bit|truecolor)* ]] || zmodload zsh/nearcolor

Note that some terminals accept the true color escape codes but map
them internally to a more limited palette in a similar manner to the
``zsh/nearcolor`` module.
