ANSI Color Code Converter

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

Free ANSI color code converter. Enter a hex color and get the closest ANSI 256-color terminal code, with ready-to-use foreground and background escape sequences. Searches the full palette — 16 system colors, the 6×6×6 cube and the grayscale ramp — by RGB distance. Runs entirely in your browser. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is the ANSI 256-color palette?

It is the standard extended color set most terminals support. Codes 0 to 15 are the system and bright colors, codes 16 to 231 form a 6×6×6 RGB cube, and codes 232 to 255 are a 24-step grayscale ramp. Together they give 256 selectable colors.

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 divided into three distinct regions, each with precise RGB definitions:

  • Codes 0–15: the 16 fixed system colors — 8 standard (black, red, green, yellow, blue, magenta, cyan, white) and 8 bright variants. These are defined by the terminal, not the spec, so they vary slightly across emulators.
  • Codes 16–231: a 6×6×6 color cube where index = 16 + 36·r + 6·g + b. Each channel level maps to 0 for level 0 or 55 + 40·level otherwise, giving values 0, 95, 135, 175, 215, 255.
  • Codes 232–255: a 24-step grayscale ramp where each step value is 8 + 10·(index − 232), running from near-black (code 232) to near-white (code 255).

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

Example: finding the nearest code for a blue

Enter #2563eb (a common bright blue from Tailwind’s palette). The palette grid does not include this exact value, so the tool scans all 256 entries and returns the cube code with the closest RGB match, showing a swatch comparison so you can judge the difference visually.

To use the returned code in a shell script:

# Foreground (text) color — replace N with the code
printf '\e[38;5;Nm%s\e[0m\n' "hello in your color"

# Background color — use 48 instead of 38
printf '\e[48;5;Nm%s\e[0m\n' "highlighted text"

The \e[0m at the end resets all attributes so subsequent output is not stained with the color.

When to use 24-bit true color instead

If your terminal emulator supports 24-bit “true color” (most modern ones do — check $COLORTERM for truecolor), you can skip the 256-palette entirely and use the exact hex values:

# 24-bit foreground: ESC[38;2;R;G;Bm
printf '\e[38;2;37;99;235mExact blue\e[0m\n'

This is always more accurate than the nearest palette approximation. Use the 256-color codes when you need to support older terminals, SSH sessions to legacy systems, or environments where TERM=xterm-256color but 24-bit support is uncertain.

Common use cases

  • Styling CLI tool output to match a product’s brand palette
  • Writing terminal UI components (using curses, blessed, or similar) that need specific colors
  • Generating colored diff or status output in CI logs
  • Checking which palette code a terminal theme uses for a given visual color