ANSI Color Code Converter

Convert any hex color to the nearest ANSI 256-color terminal code and escape sequence.

Ad placeholder (leaderboard)

Map any hex color to the closest terminal color

The ANSI Color Code Converter finds the nearest entry in the ANSI 256-color palette for any hex color and gives you the escape sequences to use it. It is the quick way to translate a brand color or design swatch into something your shell scripts, CLI tools and terminal UIs can actually display.

How it works

The 256-color palette is three regions with precise RGB definitions:

  • Codes 0–15: the fixed system and bright colors (black, red, green, … white).
  • Codes 16–231: a 6×6×6 cube where index = 16 + 36·r + 6·g + b and each channel level maps to 0 for level 0 or 55 + 40·level otherwise.
  • Codes 232–255: a grayscale ramp where each step is 8 + 10·(index − 232).

The converter generates the exact RGB for all 256 codes, then computes the squared distance dr² + dg² + db² between your color and each palette entry, returning the code with the smallest distance. A distance of zero means an exact match.

Example

The color #3366ff is not on the palette grid, so the tool returns the nearest cube code and reports it as a close approximation, showing both swatches side by side. To use it as text color you would write the escape sequence \e[38;5;Nm before your text and \e[0m after to reset.

Notes

Because the palette is a coarse grid, expect approximations for most arbitrary colors. If your terminal supports 24-bit “true color”, you can sidestep the palette entirely with the \e[38;2;R;G;Bm sequence and your exact RGB values.

Ad placeholder (rectangle)