# hex-color-to-color-name

> Converts Hex Codes to Color Names

Latest version **2.0.9** (published 2026-11-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install hex-color-to-color-name
pnpm add hex-color-to-color-name
yarn add hex-color-to-color-name
bun add hex-color-to-color-name
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.9 |
| Published | 2026-11-16 |
| First published | 2020-08-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 60.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 22 |
| Author | Jefferin Joseph |
| Maintainers | jeff3754 |
| Keywords | HexColor, HexCode, HexColorCode, HexColorCodeToColorName, ColorName |

## Links

- npm: https://www.npmjs.com/package/hex-color-to-color-name
- Repository: https://github.com/jeff3754/HexColorToColorName
- Homepage: https://github.com/jeff3754/HexColorToColorName#readme
- Issues: https://github.com/jeff3754/HexColorToColorName/issues
- npm.io page: https://npm.io/package/hex-color-to-color-name

## Alternatives

- [postcss-color-hex-alpha](https://npm.io/package/postcss-color-hex-alpha.md) — 6.4M weekly downloads
- [randomcolor](https://npm.io/package/randomcolor.md) — 348.0K weekly downloads
- [bows](https://npm.io/package/bows.md) — 1.3K weekly downloads
- [ep_prefer_color_scheme](https://npm.io/package/ep_prefer_color_scheme.md) — 260 weekly downloads
- [coc-yank](https://npm.io/package/coc-yank.md) — 61 weekly downloads

## Recent versions

- 2.0.9 (latest) — 2026-11-16
- 2.0.8 — 2026-10-06
- 2.0.7 — 2026-09-11
- 2.0.6 — 2026-08-28
- 2.0.5 — 2026-08-15
- 2.0.4 — 2026-08-07
- 2.0.3 — 2026-08-07
- 2.0.2 — 2026-08-03
- 2.0.1 — 2026-02-10
- 2.0.0 — 2026-02-10
- 1.0.2 — 2020-08-13
- 1.0.1 — 2020-08-12
- 1.0.0 — 2020-08-12

## README

# 🎨 HexColorToColorName

[![npm version](https://img.shields.io/npm/v/hex-color-to-color-name.svg)](https://www.npmjs.com/package/hex-color-to-color-name)
[![NPM Weekly Downloads](https://img.shields.io/npm/dw/hex-color-to-color-name)](https://www.npmjs.com/package/hex-color-to-color-name)
[![npm provenance](https://img.shields.io/badge/npm-provenance-blue)](https://www.npmjs.com/package/hex-color-to-color-name?activeTab=code)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Test Status](https://github.com/jeff3754/HexColorToColorName/actions/workflows/test.yml/badge.svg)](https://github.com/jeff3754/HexColorToColorName/actions)

A lightweight, zero-dependency TypeScript library that maps hex codes to the closest human-readable color names. Perfect for accessibility, UI/UX tools, and design systems.

## ✨ Features
- **Accurate Matching**: Uses mathematical distance in RGB and HSL color spaces to find the closest color name if an exact match isn't found.
- **Flexible Input**: Supports 3, 4 (RGBA - alpha ignored), and 6-digit hex codes, with or without the `#` prefix.
- **TypeScript Ready**: Full type definitions included out of the box, ensuring robust and predictable usage.
- **Lightweight**: Zero external runtime dependencies.
- **Fast**: Optimized lookup with pre-processed color data for high-performance applications.

---

## 🚀 Installation

Install via npm:

```bash
npm install hex-color-to-color-name
```

Or via yarn:

```bash
yarn add hex-color-to-color-name
``` 

## 🛠 Usage

### `GetColorName(code: string)`: Basic Color Name Lookup

Pass a hex string (with or without the # symbol, including 4-digit RGBA) to get the most accurate human-readable name. If no exact match is found, it returns the closest known color name.

```ts
import { GetColorName } from "hex-color-to-color-name";

// Exact matches
const color1 = GetColorName("#000000"); // returns "Black"
const color2 = GetColorName("ff0000"); // returns "Red"
const color3 = GetColorName("#f5f5dc"); // returns "Beige"

// Closest matches for non-exact hex codes
const approximateColor1 = GetColorName("#1a1a1b"); // returns "Shark"
const approximateColor2 = GetColorName("1E90F0"); // returns "Dodger Blue"

// 4-digit hex codes (alpha ignored)
const rgbaColor = GetColorName("#F00A"); // returns "Red" (alpha channel 'A' is ignored)
const rgbaColorNoHash = GetColorName("000F"); // returns "Black" (alpha channel 'F' is ignored)
```

### `GetColorPacket(code: string)`: Detailed Color Information

Use `GetColorPacket` to retrieve a comprehensive `ColorPacket` object, which includes the resolved `colorName` (exact or closest match), the normalized `hexCode`, and its `rgb` and `hsl` components. Even for invalid inputs, it returns a `ColorPacket` with an "Invalid Color" name and default `rgb/hsl` values.

```ts
import { GetColorPacket, ColorPacket } from "hex-color-to-color-name";

// For an exact match
const blackPacket: ColorPacket = GetColorPacket("#000000");
/*
{
  colorName: "Black",
  hexCode: "000000",
  rgb: { r: 0, g: 0, b: 0 },
  hsl: { h: 0, s: 0, l: 0 }
}
*/

// For a closest match
const approximatePacket: ColorPacket = GetColorPacket("#1a1a1b");
/*
{
  colorName: "Shark",
  hexCode: "1A1A1B",
  rgb: { r: 26, g: 26, b: 27 },
  hsl: { h: 240, s: 2, l: 10 }
}
*/

// For a 4-digit hex code
const rgbaPacket: ColorPacket = GetColorPacket("#F00A");
/*
{
  colorName: "Red",
  hexCode: "FF0000",
  rgb: { r: 255, g: 0, b: 0 },
  hsl: { h: 0, s: 100, l: 50 }
}
*/

// For an invalid hex code
const invalidPacket: ColorPacket = GetColorPacket("not-a-color");
/*
{
  colorName: "Invalid Color: not-a-color",
  hexCode: "not-a-color", // Original invalid input is preserved
  rgb: { r: 0, g: 0, b: 0 },
  hsl: { h: 0, s: 0, l: 0 }
}
*/
```

## 🤝 Contributing

Pull requests are welcome. Please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## ☕ Support

If you find this library useful, consider supporting the development by buying me a [Ko-Fi](https://ko-fi.com/jefferinjoseph)! ☕❤️

## 📄 License
[MIT](https://choosealicense.com/licenses/mit/)

Maintained by [Jefferin Joseph](https://github.com/jeff3754)

---
_Source: https://npm.io/package/hex-color-to-color-name · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
