ansi-styles
ANSI escape codes for styling strings in the terminal with colors and modifiers.
Repository Health
Technical Analysis
ansi-styles is the low-level styling primitive behind chalk: a tiny, dependency-free ES module that maps human-readable style names to the raw ANSI escape codes terminals understand. Each style exposes an open and close sequence you concatenate around your text, covering modifiers (bold, dim, italic, underline), the standard 16 foreground and background colors, and their bright variants.
Beyond the named palette, it ships conversion helpers to move between RGB, hex, 256-color, and 16-million-color (truecolor) escape sequences, gracefully degrading when a terminal supports fewer colors. With over 600 million weekly downloads it is one of the most depended-upon packages in the JavaScript ecosystem, powering terminal color across the entire CLI tooling landscape.
What You Get
- A complete map of ANSI modifiers, foreground colors, and background colors, each with open/close escape codes
- Bright color variants and gray/grey aliases for the extended 16-color palette
- Color-conversion helpers between RGB, hex, 256-color, and 16-million-color escapes
- Exported name arrays (modifierNames, foregroundColorNames, backgroundColorNames, colorNames) for input validation
- A zero-dependency, tree-shakeable ES module with bundled TypeScript definitions
Common Use Cases
- Building the color layer of a higher-level styling library or logger
- Emitting colored output from a CLI tool without pulling in a full formatting framework
- Converting arbitrary RGB or hex colors into terminal-safe escape sequences with automatic downgrade
Under The Hood
Architecture The package is a single index.js ES module. A nested styles object groups codes under modifier, color, and bgColor, each entry a [open, close] numeric pair. Small factory functions (wrapAnsi16, wrapAnsi256, wrapAnsi16m) build the escape-sequence templates, and an assembleStyles() routine walks the groups to attach open/close string properties and a lookup Map, then bolts on the color-conversion helpers. Background variants reuse foreground codes offset by a constant (ANSI_BACKGROUND_OFFSET = 10).
Tech Stack Pure JavaScript authored as an ESM ("type": "module", exports: "./index.js"), targeting Node >=12, with hand-written TypeScript definitions in index.d.ts. No runtime dependencies. Dev tooling is ava for tests, xo for linting, and tsd for type testing.
Code Quality The implementation is compact and consistent, using shared constants and factory functions to avoid repetition across dozens of style entries. It ships both an ava test suite (test/) and type tests (index.test-d.ts), and the exported name arrays double as a self-documenting contract.
API Design The style.open / style.close convention is intentionally minimal — callers compose their own strings, which is exactly what makes it a reusable primitive for higher-level libraries. Named colors read naturally, aliases (gray/grey) accommodate spelling preferences, and the conversion helpers have predictable, descriptive names (rgbToAnsi256, hexToRgb). It is deliberately lower-level than chalk, so most application developers reach for chalk instead, but the surface here is tiny and easy to reason about.
Used by 2 apps in this directory
melty
Developer Tools · AI Code Assistants · Code Editors
The AI code editor where every chat message is a git commit you can revert, branch, or squash
Void
AI Code Assistants · Code Editors · Automation
Open-source AI code editor with direct LLM integration and data privacy